0,]']ÚHeï}WÿÍu¡â}8·d%qy)6o;]ÉA¦ösï÷ôôô”°ÂVl†3,-- title: Tokdle -- author: archargelod -- desc: wordle clone in toki pona lang -- license: MIT License -- version: 1.4 -- script: lua -- input: mouse llist={ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" } wlist={} wllist={{ "a", "e", "o" },{ "en", "jo", "ko", "la", "li", "ma", "mi", "mu", "ni", "pi", "pu", "tu" },{ "ala", "ale", "ali", "anu", "ijo", "ike", "ilo", "jan", "ken", "kin", "kon", "len", "lon", "mun", "oko", "ona", "pan", "sin", "tan", "uta", "wan" },{ "anpa", "ante", "awen", "esun", "insa", "jaki", "jelo", "kala", "kama", "kasi", "kili", "kule", "kute", "lape", "laso", "lawa", "leko", "lete", "lili", "lipu", "loje", "luka", "lupa", "mama", "mani", "meli", "mije", "moku", "moli", "musi", "mute", "nasa", "nena", "nimi", "noka", "olin", "open", "pake", "pali", "pana", "pini", "pipi", "poka", "poki", "pona", "powe", "sama", "seli", "selo", "seme", "sewi", "sike", "sina", "sona", "suli", "suno", "supa", "suwi", "taso", "tawa", "telo", "toki", "tomo", "unpa", "walo", "waso", "wawa", "weka", "wile" },{ "akesi", "alasa", "apeja", "kiwen", "linja", "lukin", "monsi", "nanpa", "nasin", "pilin", "tenpo", "tonsi", "utala" },{ "kalama", "kipisi", "kulupu", "majuna", "namako", "pakala", "palisa", "pimeja", "sijelo", "sinpin", "soweli" },{ "kepeken", "linluwi", "monsuta", "sitelen" }} w=240 h=136 touch_timer=20 flags={ false, false, false, true , false, false, false, true , --accept only toki pona words false, --hidesettings } dropsettval=0 dropsettmax=14 for i=1,9 do flags[i]=false if pmem(i)==1 then flags[i]=true end end score=0 highscore=pmem(10) streak=0 function setup() win,lose=false wlist={} --set flag 4 if no flags local flagsum=0 for i=1,7 do if flags[i] then flagsum=flagsum+1 end end if flagsum==0 then flags[4]=true pmem(4,1) end --populate word list for i=1,7 do if flags[i] then for j,val in ipairs(wllist[i]) do table.insert(wlist,val) end end end answer=wlist[math.random(1,#wlist)] a_table={} for i=1,#answer do table.insert(a_table,string.sub(answer,i,i)) end cx=w-22*(#answer+1) cy=h-22 guess={"_","_","_","_","_","_","_"} g_index=1 history={} history_col={} end setup() function TIC() bg() drawguess() drawhistory() drawindex() drawkeypad() drawstats() drawflags() move() type() touch() --for i=0,8 do print(pmem(i),100+i*6,100,2) end end function drawguess() local sz=11*2 for j=1,5 do for i=1,#answer do rect(cx+i*sz-2,cy-j*sz-2,sz-2,sz-1,0) end end for i=1,#answer do local letter=string.sub(g_string(),i,i) local col=12 if win then col=5 end if lose then col=1 guess=a_table end rect(cx+i*sz-2,cy-2,sz-2,sz-1,0) rectb(cx+i*sz-2,cy-2,sz-2,sz-1,col) custom_print(letter,cx+i*sz,cy,false) end end function drawkeypad() keys={"a","e","i","j","k","l","m","n","o","p","s","t","u","w"} local index=1 local sz=136/5 local ox=0 local oy=0 for j=0,4 do for i=0,2 do local alt=iskeyred(keys[index]) rect(ox+i*sz,oy+j*sz,sz,sz,8) rectb(ox+i*sz,oy+j*sz,sz,sz,13) custom_print(keys[index],ox+i*sz+5,oy+j*sz+6,alt) index=index+1 end end end function iskeyred(lett) if lett==nil then return false end if #history<1 then return false end local onlyred=false for i=1,#history do for j=1,#history[i] do if history[i][j]==lett then if history_col[i][j]==5 then return false end if history_col[i][j]==3 then return false end if history_col[i][j]==2 then onlyred=true end end end end return onlyred end function touch() local x,y,lmk=mouse() local sz=136/5 local tilex=math.floor(x/sz) local tiley=math.floor(y/sz) local ind_x=x-17 local ind_y=y-1 local ind_tilex=math.floor(ind_x/22) local ind_tiley=math.floor(ind_y/22) local flg_x=x+1 local flg_tilex=math.floor(flg_x/7) local flg_tiley=math.floor(y/7) local sett_x=x-1 local sett_tilex=math.floor(sett_x/7) local sett_tiley=math.floor(y/7) local col=2 local letter=keys[tiley*3+tilex+1] touch_timer=touch_timer-1 --keypad buttons if tilex<3 and tiley<5 then if lmk and touch_timer<0 then if letter==nil then submit() touch_timer=10 return end guess[g_index]=letter move(1) sfx(4) touch_timer=10 end rectb(tilex*sz,tiley*sz,sz,sz,9) end --guess index buttons if ind_tilex>9-#answer and ind_tilex<10 and ind_tiley==5 then if lmk and touch_timer<0 then g_index=ind_tilex-2-(7-#answer) touch_timer=10 end rectb(ind_tilex*22+17,ind_tiley*22+1,22,23,10) end --disable touch during animation if not(dropsettval==0 or dropsettval==dropsettmax) then return end --toggle settings button local sett_ylimit=0 if not(flags[9]) then sett_ylimit=2 end if sett_tilex==33 and sett_tiley==sett_ylimit then if lmk and touch_timer<0 then sw_flag(9) touch_timer=10 end --local t=1 if flags[9] then local t=0 end rect(sett_tilex*7+3,sett_tiley*7+2,4,4,2) end if flags[9] then return end --letter flag buttons if flg_tilex>26 and flg_tilex<34 and flg_tiley==0 then if lmk and touch_timer<0 then sw_flag(flg_tilex-26) touch_timer=10 end rectb(flg_tilex*7+1,flg_tiley*7,8,8,5) end --"tp only" flag button if flg_tilex==16 and flg_tiley==0 then if lmk and touch_timer<0 then sw_flag(8) touch_timer=10 end rectb(flg_tilex*7-1,flg_tiley*7,8,8,5) end end function custom_print(lett,x,y,alt) if alt then altnum=96 else altnum=0 end local dict={1,5,9,10,11,12,13,14,15,16,19,20,21,23} local sprts={256,258,260,262,264,266,268,270,288,290,292,294,296,298} local sprite=320 for i,val in ipairs(dict) do if lett==llist[val] then sprite=sprts[i] end end spr(sprite+altnum,x,y,0,1,0,0,2,2) end function drawhistory() local sz=11*2 for j=#history,1,-1 do local jj=#history-j+1 for i=1,#answer do local letter=history[j][i] local col=history_col[j][i] rect(cx+i*sz-2,cy-jj*sz-2,sz-2,sz-1,0) rectb(cx+i*sz-2,cy-jj*sz-2,sz-2,sz-1,col) custom_print(letter,cx+i*sz,cy-jj*sz,true) end end end function drawindex() local sz=11*2 local wt=11 local ht=2 rect(cx+g_index*sz-2,cy-2+sz,wt*2-2,ht*2,12) end function bg() cls(15) rectb(0,0,w,h,13) end function type() local letter="" for i=1,26 do if keyp(i) then letter=tpize(llist[i]) guess[g_index]=letter move(1) end end if keyp(50) then submit() end if keyp(51) then move(-1) end for i=1,8 do if keyp(27+i) then sw_flag(i) flags[9]=false end end if keyp(27+9) then sw_flag(9) end end function sw_flag(ind) if flags[ind] then flags[ind]=false pmem(ind,0) else flags[ind]=true pmem(ind,1) end end function drawflags() local sz=7 local ox,oy=w-sz*8-1,-dropsettmax+dropsettval local sprite=448 --hide/show settings flag if flags[9] then sprite=481 dropsettval=dropsettval-1 else sprite=480 dropsettval=dropsettval+1 end if dropsettval<0 then dropsettval=0 end if dropsettval>dropsettmax then dropsettval=dropsettmax end spr(sprite,232,dropsettval,0) --letter flags --rect(ox+sz-18,oy,sz*7+19,15,14) --rectb(ox+sz-18,oy,sz*7+19,15,13) rect( 81,oy,w-81,14,14) rectb(81,oy,w-81,15,13) --highscore print("nanpasewi:",ox-100 ,oy+8,12,false,1,true) print(highscore ,81+13*3+1,oy+8, 4,true ,1,true) print("nimi",ox-4,oy+1,12,false,1,true) for i=1,7 do if flags[i] then sprite=449 else sprite=448 end print(i,ox+3+sz*i,oy+8,12,true,1,true) spr(sprite,ox+sz*i,oy,0) end --"tp only" flag --rect(ox-sz*3-31,oy,sz*4-2+13,8,14) --rectb(ox-sz*3-31,oy,sz*4-2+13,8,13) print("tp taso:",ox-100,oy+1,12,false,1,true) if flags[8] then sprite=449 else sprite=448 end spr(sprite,ox-72,oy,0) end function drawstats() local ox=81 local oy=dropsettval rect( ox,oy-1,w-ox,9,14) rectb(ox,oy ,w-ox,8,13) --score=100000000 print("nanpapona:",ox+2 ,oy+1,12,false,1,true) print(score ,ox+13*3+1,oy+1,4,true,1,true) --streak=999 print("nanpa pi nimi pona:",ox+77,oy+1,12,false,1,true) print(streak ,ox+139,oy+1,6,true,1,true) --print("tries:" ,ox+115 ,oy+1,12,true,1,true) --print(#history.."/"..5,ox+113+5*5,oy+1,12,true,1,true) end function move(val) if val==-1 then g_index=g_index-1 end if val==1 then g_index=g_index+1 end if btnp(2) then g_index=g_index-1 end if btnp(3) then g_index=g_index+1 end if g_index>#answer then g_index=1 end if g_index<1 then g_index=1 end end function g_string() local string="" for i=1,#answer do string=string..guess[i] end return string end function submit() if win then streak=streak+1 addscore() setup() return end if lose then streak=0 score=0 setup() return end local gstring=g_string() local valid=false --win if gstring==answer then win=true sfx(2) return end --if guess is valid - advance game and analyze letters to assign colors if isvalid(gstring) then valid=true local tmp_guess={} for i,val in ipairs(guess) do table.insert(tmp_guess,val) end table.insert(history,tmp_guess) table.insert(history_col,assign_colors()) end --lose if too many wrong tries if #history>4 then lose=true sfx(3) return end if valid then sfx(0) else sfx(1) end end function assign_colors() local tmp_answer={} for i,val in ipairs(a_table) do table.insert(tmp_answer,a_table[i]) end local col_table={2,2,2,2,2,2,2} --check for green letters for i=1,#answer do if guess[i]==tmp_answer[i] then tmp_answer[i]="_" col_table[i]=5 end end --check for orange letters for i=1,#guess do if col_table[i]==5 then goto skip end for j=1,#answer do if guess[i]==tmp_answer[j] then tmp_answer[j]="_" col_table[i]=3 end end ::skip:: end --trace(a_table[1]..a_table[2]..a_table[3]..a_table[4]) --trace(tmp_answer[1]..tmp_answer[2]..tmp_answer[3]..tmp_answer[4]) --trace(col_table[1]..col_table[2]..col_table[3]..col_table[4]) return col_table end function isvalid(str) if not(flags[8]) then return true end for i,val in ipairs(wlist) do if str==val then return true end end end function checklett(l,pos) --right letter and place if l==a_table[pos] then return 5 end --right letter for i,val in ipairs(a_table) do if l==a_table[i] then return 3 end end --wrong letter return 2 end function tpize(let) local dict={ 2,3,4,6,7,8,17,18,22,24,25,26} local dict2={"p","s","t","p","j","j","k","l","w","s","j","s"} for i,val in ipairs(dict) do if let==llist[val] then return dict2[i] end end return let end function addscore() local cost=50 local lettcount=0 local lettbox={"a","e","i","j","k","l","m","n","o","p","s","t","u","w"} local multbox={1000,500,400,300,200} for i,val in ipairs(guess) do for j=#lettbox,1,-1 do if lettbox[j]==val then table.remove(lettbox,j) lettcount=lettcount+1 end end end for i,ival in ipairs(history) do for j,jval in ipairs(ival) do for k=#lettbox,1,-1 do if lettbox[k]==jval then table.remove(lettbox,k) lettcount=lettcount+1 end end end end score=score+(multbox[#history+1]+(14-lettcount)*cost) if score>highscore then highscore=score pmem(10,score) end --trace("try: "..#history+1) --trace(multbox[#history+1].." + "..cost.." * "..(14-lettcount)) --trace("score: "..(multbox[#history+1]+(14-lettcount)*cost)) --trace("") end