--!name Mahjong Solitaire
--!icon game
--!category Puzzles
local aF="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefg"local l,af=13,8 local M=l*af local aH={"Dots","Bamboo","Characters"}local T={{name="Bridge",level="easy",map="11.........11".."121.......121".."1211.111.1121".."11..12321..11".."11..12321..11".."1211.111.1121".."121.......121".."11.........11"},{name="Pyramid",level="medium",map="...1111111...".."..111111111..".."..112222211.."..".1112333211..".."..1123332111.".."..112222211..".."..111111111..".."...1111111..."},{name="Fortress",level="hard",map="3211111111123".."21.........12".."1...11111...1".."1...12221...1".."1...12221...1".."1...11111...1".."21.........12".."3211111111123"},}local P,q=1,0 local ai,x,f,G,R,z={},{},{},{},{},{}local n,E={},{}local d,i,k=1,0,0 local J=false local v,B,s="menu",1,nil local K={0,0,0,0,0,0}local function an(t)P=t local aP=T[t].map q=0 z={}for a=1,M do local g=tonumber(aP:sub(a,a))or 0 ai[a],x[a],f[a]=g,q+1,f[a]or 0 for aq=1,g do R[q+aq]=a end q=q+g if g>0 then z[#z+1]=a end end for c=q+1,#G do G[c],R[c]=nil,nil end end local function r(a)local g=f[a]if g==0 then return false end local m=(a-1)%l return(m==0 or f[a-1]<g)or(m==l-1 or f[a+1]<g)end local function O(a)return G[x[a]+f[a]-1]end local function aa(a)local e=O(a)-1 return e%9+1 .." "..aH[e//9+1]end local function al()local w,u={},0 for N,a in ipairs(z)do if r(a)then local e=O(a)local g=(w[e]or 0)+1 w[e]=g if g%2==0 then u=u+1 end end end return u end local function az(aB)local c={}for h=1,#aB do c[h]=("%02x"):format(aB[h])end return table.concat(c)end local function ad()local e={}if J then for h=1,q do e[h]=string.char(96+G[h])end end snail.save(table.concat({"J1",P,d,i,J and 1 or 0,table.concat(K,","),table.concat(e),J and az(n)or"",J and az(E)or""},";"))end local function aO()local A=snail.load()if type(A)~="string"or A:sub(1,3)~="J1;"then return end local C={}for g in(A..";"):gmatch("([^;]*);")do C[#C+1]=g end local h=0 for g in(C[6]or""):gmatch("%d+")do h=h+1 if h<=6 then K[h]=tonumber(g)end end an(math.max(1,math.min(#T,tonumber(C[2])or 1)))local p=C[7]or""if C[5]~="1"or#p~=q then return end for c=1,q do G[c]=p:byte(c)-96 end for a=1,M do f[a]=ai[a]end n,k={},q for m in(C[8]or""):gmatch("%x%x")do local c=tonumber(m,16)local a=R[c]if not a or x[a]+f[a]-1~=c then break end n[#n+1]=c f[a]=f[a]-1 k=k-1 end if#n%2==1 then local a=R[n[#n]]f[a]=f[a]+1 n[#n],k=nil,k+1 end E={}for m in(C[9]or""):gmatch("%x%x")do local c=tonumber(m,16)if c<1 or c>q then E={};break end E[#E+1]=c end J=true d=math.max(1,math.min(M,tonumber(C[3])or 1))i=tonumber(C[4])or 0 if i<1 or i>M or not r(i)then i=0 end end local function ao(c)for h=#c,2,-1 do local o=math.random(h)c[h],c[o]=c[o],c[h]end return c end local function aS()local y,S,D={},{},{}local Z=0 for a=1,M do y[a]=f[a]Z=Z+f[a]end while Z>0 do local u,L,w=0,0,0 for N,a in ipairs(z)do local g=y[a]if g>0 then local m=(a-1)%l if m==0 or m==l-1 or y[a-1]<g or y[a+1]<g then u=u+1 S[u]=a if g>L then L,w=g,1 elseif g==L then w=w+1 end end end end if u<2 then return nil end local h=math.random(u)if math.random(2)==1 then local e=math.random(w)for o=1,u do if y[S[o]]==L then e=e-1 if e==0 then h=o;break end end end end local b=S[h]S[h]=S[u]local j=S[math.random(u-1)]D[#D+1]=x[b]+y[b]-1 D[#D+1]=x[j]+y[j]-1 y[b],y[j]=y[b]-1,y[j]-1 Z=Z-2 end return D end local function ar(p)for N=1,#p>15 and 3 or 12 do local D=aS()if D then ao(p)for o=1,#p do G[D[2*o-1]]=p[o]G[D[2*o]]=p[o]end E=D return true end end return false end local function V(ak)local aY,aZ=(ak-1)%l,(ak-1)//l local U,au=nil,1e9 for N,a in ipairs(z)do if r(a)then local I=math.abs((a-1)%l-aY)+2*math.abs((a-1)//l-aZ)if I<au then U,au=a,I end end end return U or ak end local function ax(t)an(t)for a=1,M do f[a]=ai[a]end local ag={}for e=1,27 do ag[e]=e end ao(ag)local p={}for o=1,q//2 do p[o]=ag[(o-1)//2+1]end if not ar(p)then ao(p)for c=1,q do G[c]=p[(c+1)//2]end E={}end n,k,i,s={},q,0,nil J=true d=V(4*l+7)K[2*t-1]=K[2*t-1]+1 v="play"ad()end local function aD()local w,p={},{}for N,a in ipairs(z)do for aq=1,f[a]do local e=G[x[a]+aq-1]w[e]=(w[e]or 0)+1 if w[e]%2==0 then p[#p+1]=e end end end if ar(p)then s="Shuffled: there is a way through"else s="These can't be shuffled; try Undo"end i=0 d=V(d)ad()end local function aU(b,j)for N,a in ipairs({b,j})do n[#n+1]=x[a]+f[a]-1 f[a]=f[a]-1 end k=k-2 i=0 if k==0 then K[2*P]=K[2*P]+1 else d=V(d)end end local function aV()if#n==0 then s="Nothing to undo";return end for N=1,2 do local c=n[#n]n[#n]=nil local a=R[c]f[a]=f[a]+1 end k=k+2 i=0 s="Took back the last pair"if not r(d)then d=V(d)end end local function aR()for o=1,#E-1,2 do local b,j=E[o],E[o+1]local W,X=R[b],R[j]local aA,aM=b-x[W]<f[W],j-x[X]<f[X]if aA~=aM then return end if aA then if b==x[W]+f[W]-1 and j==x[X]+f[X]-1 and r(W)and r(X)then return W,X end return end end end local function aK()local as,at,Y=nil,nil,-1 for h,b in ipairs(z)do if r(b)then local e=O(b)for o=h+1,#z do local j=z[o]if r(j)and O(j)==e and f[b]+f[j]>Y then as,at,Y=b,j,f[b]+f[j]end end end end return as,at end local function aL()local b,j=aR()if not b then b,j=aK()end if b then i,d=b,j s="Hint: "..aa(b)..", OK takes them"else s="No pairs are open"end end local function aQ(ay,aJ)local av,aw=(d-1)%l,(d-1)//l local U,Y=nil,1e9 for N,a in ipairs(z)do if a~=d and r(a)then local m,ae=(a-1)%l,(a-1)//l local A if ay~=0 then local b=(m-av)*ay if b<=0 then b=b+l end A=b+8*math.abs(ae-aw)else local b=(ae-aw)*aJ if b<=0 then b=b+af end A=4*b+math.abs(m-av)end if A<Y then U,Y=a,A end end end if U then d=U end end local function ac()return J and k>0 end local function aC()local c={}if J then c[#c+1]={k>0 and"Back to the game"or"Back to the cleared board","resume"}end if ac()then c[#c+1]={"Hint: show an open pair","hint"}if#n>0 then c[#c+1]={"Undo the last pair","undo"}end c[#c+1]={"Shuffle the tiles left","shuffle"}end for t,H in ipairs(T)do if not H.n then H.n=0 for I in H.map:gmatch("%d")do H.n=H.n+I end end c[#c+1]={"New "..H.name..": "..H.n.." tiles, "..H.level,t}end c[#c+1]={"How to play","help"}return c end local aG={left={-1,0},right={1,0},up={0,-1},down={0,1}}function key(e)if v=="menu"then local F=aC()if B>#F then B=#F end if e=="up"then B=(B-2)%#F+1 elseif e=="down"then B=B%#F+1 elseif e=="ok"then local b=F[B][2]s=nil if b=="help"then v="help";return end if type(b)=="number"then ax(b);return end v="play"if b=="hint"then aL()elseif b=="undo"then aV();ad()elseif b=="shuffle"then aD()end end return end if v=="help"then if e=="top"or e=="ok"then v="menu"end return end s=nil if e=="top"then v,B="menu",1 return end local I=aG[e]if I then if k>0 then aQ(I[1],I[2])end return end if e~="ok"then return end if k==0 then ax(P);return end if al()==0 then aD();return end if not r(d)then d=V(d);return end if i==0 then i=d elseif i==d then i=0 s="Put it back"elseif O(i)==O(d)then aU(i,d)else s="No match: picked "..aa(d).." instead"i=d end ad()end function touch(aX,b,j)if aX~="board"or v~="play"or not ac()or al()==0 or b<1 or b>l then return end local a=(j-1)//2*l+b if a<1 or a>M or f[a]==0 then return end d=a if r(a)then return key("ok")end s=aa(a).." is blocked"end local function aj(h)return aF:sub(h+1,h+1)end local function aI()local _,L,ab={},{},{}for ae=0,af-1 do for m=1,l do local a=ae*l+m local g=f[a]if g>0 then local e=O(a)-1 local aE=e//9 local ap=a==d and 1 or(a==i and 2 or 0)L[m]=aj(ap*9+e%9)if ap==0 then ab[m]=aj(27+(aE*3+g-1)*2+(r(a)and 0 or 1))else ab[m]=aj((ap==1 and 45 or 54)+aE*3+g-1)end else L[m],ab[m]=" "," "end end _[#_+1]=table.concat(L)_[#_+1]=table.concat(ab)end return table.concat(_,"/")end local function aT(Q)if s then return s end if k==0 then return"Every tile matched. Well done!"end if Q==0 then return"No pairs open. OK shuffles the rest."end if i>0 and i~=d then return"Picked "..aa(i)..": find its twin"end local A=aa(d)if i==d then return"Picked "..A..": find its twin"end return A..", layer "..f[d]..(r(d)and""or" (blocked)")end local function am(t)local a,aW=K[2*t-1],K[2*t]return T[t].name..": won "..aW.." of "..a end function draw()if v=="menu"then snail.title("Mahjong Solitaire")local F=aC()if B>#F then B=#F end for h,aN in ipairs(F)do snail.row(aN[1],h==B)end snail.gap()if ac()then snail.small(T[P].name.." in progress: "..k.." tiles left.")else snail.small("Every deal can be cleared.")end snail.small(am(1)..". "..am(2)..".")snail.small(am(3)..".")snail.hint("BACK leave OK choose")return end if v=="help"then snail.title("How to play")snail.text("Match pairs of identical free tiles to clear the board.")snail.text("A tile is free when nothing is on it and its left or right side is open.")snail.text("Free tiles are white below; blocked ones are shaded. Plates under a tile count its layer.")snail.text(snail.touchscreen and"Tap a free tile, then its twin, to take both."or"Arrows jump between free tiles. OK picks a tile, OK on its twin takes both.")snail.text("BACK opens the menu: hint, undo, shuffle and new deals. Your game is kept.")snail.text("Every deal can be cleared. Hints follow a way through while you keep to it.")snail.hint("BACK back OK back")return end local Q=al()snail.title(T[P].name.."   "..k.." tiles   "..Q..(Q==1 and" pair"or" pairs"))snail.status(aT(Q))local ah if k==0 then ah="Board cleared!"elseif Q==0 then ah="No moves left"end snail.board(aI(),nil,ah)if k==0 then snail.hint("BACK menu OK new deal")elseif Q==0 then snail.hint("BACK menu OK shuffle")elseif i>0 then snail.hint("BACK menu OK match L/R move")else snail.hint("BACK menu OK pick L/R move")end end function start()math.randomseed(os.time())snail.ink("fast")an(1)aO()if ac()then v="play"if not r(d)then d=V(d)end else v="menu"end end
