--!name Mini Crosswords
--!icon game
--!category Puzzles
local an="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function N(a)return an:sub(a+1,a+1)end local au,ap,ar,aq,as,ao=0,27,54,80,81,82 local v={"##two#profalertceaseeaten|A pair|College lecturer, for short|On the lookout|Come to an end|Consumed|Top playing card|Desperate request|Trick or ___|Not as good|Many times"}local at={"QWERTYUIOP<"," ASDFGHJKL?","  ZXCVBNM  "}local k=0 local A,g,l,n={},{},{},{}local e,m,y=1,"A","grid"local x,E=1,1 local I,u="list",1 local j,z={},{}local L,t=nil,1 local function B(a)return A[a]~="#"end local function Y(d)k,t=d,d local o=v[d]local U={}for b in o:sub(27):gmatch("[^|]+")do U[#U+1]=b end for a=1,25 do A[a]=o:sub(a,a);l[a]={}end n={}for aj=1,2 do local s=aj==1 and"A"or"D"for q=0,4 do local H={}for w=0,5 do local a=w<=4 and(aj==1 and q*5+w+1 or w*5+q+1)if a and B(a)then H[#H+1]=a else if#H>=2 then n[#n+1]={cells=H,dir=s}for P,b in ipairs(H)do l[b][s]=#n end end H={}end end end end local X,af=0,{}for a=1,25 do local q,s=l[a].A,l[a].D if B(a)and((q and n[q].cells[1]==a)or(s and n[s].cells[1]==a))then X=X+1 af[a]=X end end for c,h in ipairs(n)do h.num,h.clue=af[h.cells[1]],U[c]or""end local r=z[d]or""for a=1,25 do local b=r:sub(a,a)g[a]=(B(a)and b:match("%l"))and b or""if j[d]and B(a)then g[a]=A[a]end end e,m=n[1].cells[1],n[1].dir y,L="grid",nil x,E=1,1 I="play"end local function Z()if k>0 and not j[k]then local i={}for a=1,25 do i[a]=g[a]~=""and g[a]or"-"end z[k]=table.concat(i)end local ab={}for d=1,#v do ab[d]=j[d]and"1"or"0"end local i={"M1",t,table.concat(ab)}for d,r in pairs(z)do if not j[d]and r:find("%l")then i[#i+1]=d.."="..r end end snail.save(table.concat(i,";"))end local function aC()local o=snail.load()if type(o)~="string"or o:sub(1,3)~="M1;"then return end local r={}for aI in o:gmatch("[^;]+")do r[#r+1]=aI end t=math.max(1,math.min(#v,tonumber(r[2])or 1))for d=1,#v do j[d]=(r[3]or""):sub(d,d)=="1"end for c=4,#r do local d,az=r[c]:match("^(%d+)=(.+)$")if d then z[tonumber(d)]=az end end end local function ae(ac)for c=1,#v do local d=(ac-1+c)%#v+1 if not j[d]then return d end end return ac end local function M()return n[l[e][m]]end local function al(D)for c,b in ipairs(D)do if b==e then return c end end return 1 end local function _()local h=M()local D,G=h.cells,al(h.cells)for c=G+1,#D do if g[D[c]]==""then e=D[c];return end end local aE=l[e][m]for aF=1,#n do local o=n[(aE-1+aF)%#n+1]for P,b in ipairs(o.cells)do if g[b]==""then e,m=b,o.dir;return end end end if G<#D then e=D[G+1]end end local function aw()local V,J=0,0 for a=1,25 do if B(a)then if g[a]==""then V=V+1 elseif g[a]~=A[a]then J=J+1 end end end if V>0 then return end if J==0 then j[k]=true z[k]=nil y="grid"else L=J==1 and"Full, but one letter is wrong"or("Full, but "..J.." letters are wrong")end end local function aG(a,ax,ay)local p,b=(a-1)//5,(a-1)%5 repeat p,b=p+ay,b+ax if p<0 or p>4 or b<0 or b>4 then return nil end until B(p*5+b+1)return p*5+b+1 end local function K(f,F)return at[F]:sub(f,f)end local function aD()local b=K(x,E)if b=="<"then if g[e]~=""then g[e]=""else local h=M()local G=al(h.cells)if G>1 then e=h.cells[G-1];g[e]=""end end elseif b=="?"then if g[e]==A[e]then for P,T in ipairs(M().cells)do if g[T]~=A[T]then e=T;break end end end if g[e]==A[e]then for a=1,25 do if B(a)and g[a]~=A[a]then e=a if not l[a][m]then m=m=="A"and"D"or"A"end break end end end g[e]=A[e]_()else g[e]=b:lower()_()end aw()Z()end local O={left=-1,right=1,up=0,down=0}local S={left=0,right=0,up=-1,down=1}function key(c)if I=="list"then if c=="up"then u=(u-2)%#v+1 elseif c=="down"then u=u%#v+1 elseif c=="ok"then Y(u)end return end L=nil if j[k]then if c=="top"then I,u="list",k;Z()elseif c=="ok"then Y(ae(k))end return end if y=="type"then if c=="top"then y="grid"elseif c=="ok"then aD()elseif c=="left"or c=="right"then local f=x repeat f=(f-1+O[c])%11+1 until K(f,E)~=" "x=f elseif S[c]then local F=(E-1+S[c])%3+1 for ah=0,10 do for P,f in ipairs({x-ah,x+ah})do if f>=1 and f<=11 and K(f,F)~=" "then x,E=f,F;return end end end end return end if c=="top"then Z()I,u="list",k elseif c=="ok"then y="type"elseif O[c]then local s=O[c]~=0 and"A"or"D"if s~=m and l[e][s]then m=s;return end local d=aG(e,O[c],S[c])if d then e=d if not l[e][m]then m=s end end end end function touch(aB,q,w)if aB~="board"or I~="play"or j[k]or q<1 or q>11 then return end if w>=7 and w<=9 then if K(q,w-6)==" "then return end x,E,y=q,w-6,"type"return key("ok")end local p,b=w-1,q-4 if p<0 or p>4 or b<0 or b>4 then return end local a=p*5+b+1 if not B(a)or not(l[a].A or l[a].D)then return end L=nil local ag=m=="A"and"D"or"A"if(a==e or not l[a][m])and l[a][ag]then m=ag end e=a end local function aA(a,Q)if not B(a)then return"#"end local R=g[a]local ad=R~=""and R:byte()-96 or 0 if not j[k]then if a==e then return N(ap+ad)end if Q[a]then return N(au+ad)end end return R~=""and("{"..R:upper().."}")or"o"end local function av()local Q={}for P,b in ipairs(M().cells)do Q[b]=true end local C={}for p=0,4 do local i={"..."}for b=1,5 do i[#i+1]=aA(p*5+b,Q)end i[#i+1]="..."C[#C+1]=table.concat(i)end if j[k]then return table.concat(C,"/")end C[#C+1]="..........."for F=1,3 do local i={}for f=1,11 do local b=K(f,F)local W=y=="type"and f==x and F==E if b==" "then i[f]="."elseif b=="<"then i[f]=N(W and aq or ao)elseif b=="?"then i[f]=W and N(as)or"{?}"else i[f]=W and N(ar+b:byte()-65)or("{"..b.."}")end end C[#C+1]=table.concat(i)end return table.concat(C,"/")end local function aa(h)return h.num..(h.dir=="A"and" Across: "or" Down: ")..h.clue end function draw()if I=="list"then snail.title("Mini Crosswords")local d,ak=#v,13 local am=math.max(1,math.min(u-6,d-ak+1))for a=am,math.min(d,am+ak-1)do local aH=j[a]and" - solved"or(z[a]and z[a]:find("%l")and" - started"or"")snail.row("Puzzle "..a..aH,a==u)end local o=0 for a=1,d do if j[a]then o=o+1 end end snail.small(o.." of "..d.." solved.")snail.hint("BACK leave OK open UP/DN move")return end snail.title("Mini Crossword "..k)local h=M()if j[k]then snail.status("Solved! Well done.")snail.text("Every word is right.")else snail.status(L or(y=="type"and"Typing"or"Grid").." - "..h.num..(h.dir=="A"and" Across"or" Down"))snail.text(aa(h))local ai=l[e][h.dir=="A"and"D"or"A"]if ai then snail.small(aa(n[ai]))end end snail.board(av())if j[k]then snail.hint("BACK puzzles OK next puzzle")elseif y=="type"then snail.hint("BACK grid OK press L/R move UP/DN move")else snail.hint("BACK puzzles OK type L/R move UP/DN move")end end function start()aC()u=j[t]and ae(t)or t if not j[t]and z[t]and z[t]:find("%l")then Y(t)end end
