--!name Mines
--!icon puzzle
--!category Puzzles
local ax="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrst"local ah=25 local ae,ad,ag,af,aw,aA,ay=0,1,2,11,12,13,14 local O={{name="Beginner",w=9,h=9,n=10},{name="Intermediate",w=16,h=16,n=40},{name="Expert",w=16,h=30,n=99},}local p,f,j,D=1,9,9,10 local e,ai={},{}local F,A,i,Q=false,false,nil,0 local H,o,m,q=0,0,1,false local r,L,t,w=0,0,0,false local n,s,u,M="menu",1,nil,false local I={{0,0,0,0},{0,0,0,0},{0,0,0,0}}local function al()local c=os.time()return c~=nil and c>1700000000 end local function Z(l)p=l local x=O[l]f,j,D=x.w,x.h,x.n local as=(l-1)*ah for c=0,ah-1 do ai[c]=ax:sub(as+c+1,as+c+1)end end local function V(b)return("%d:%02d"):format(b//60,b%60)end local function Y()local c={}if F then for a=1,f*j do local d=e[a]c[a]=string.char(65+d%16+10*(d//16))end end local b={}for l=1,3 do b[l]=table.concat(I[l],",")end snail.save(table.concat({"N1",p,m,A and 1 or 0,i=="won"and 2 or(i and 1 or 0),Q,r,t,w and 1 or 0,table.concat(b,","),table.concat(c)},";"))end local function aJ()local b=snail.load()if type(b)~="string"or b:sub(1,3)~="N1;"then return end local E={}for d in(b..";"):gmatch("([^;]*);")do E[#E+1]=d end local h=function(a)return tonumber(E[a])or 0 end Z(math.max(1,math.min(3,h(2))))local k=0 for d in(E[10]or""):gmatch("%d+")do k=k+1 if k<=12 then I[(k-1)//4+1][(k-1)%4+1]=tonumber(d)end end local y=E[11]or""if#y~=f*j then return end H,o=0,0 for a=1,f*j do local K=y:byte(a)-65 local d=K%10+16*(K//10)e[a]=d if d>=32 then o=o+1 elseif d>=16 and d~=25 then H=H+1 end end F=true m=math.max(1,math.min(f*j,h(3)))A=h(4)==1 i=h(5)==2 and"won"or(h(5)==1 and"lost"or nil)Q,r,t,w=h(6),h(7),h(8),h(9)==1 end local function J(a,E)local B,C=(a-1)%f,(a-1)//f for an=-1,1 do local ab=C+an if ab>=0 and ab<j then for am=-1,1 do local aa=B+am if(am~=0 or an~=0)and aa>=0 and aa<f then E(ab*f+aa+1)end end end end end local function aC(g)if e[g]~=9 then e[g]=e[g]+1 end end local function ar(l)Z(l)for a=1,f*j do e[a]=0 end for a=f*j+1,#e do e[a]=nil end m=(j//2)*f+f//2+1 F,A,i,Q,H,o,q=true,false,nil,0,0,0,false r,t,w,u,M=0,0,false,nil,false n="play"Y()end local function aN(av)local ao={[av]=true}J(av,function(g)ao[g]=true end)local h=0 while h<D do local a=math.random(f*j)if not ao[a]and e[a]~=9 then e[a]=9;h=h+1 end end for a=1,f*j do if e[a]==9 then J(a,aC)end end A,w=true,al()r,L=0,os.time()I[p][1]=I[p][1]+1 end local U,v={},0 local function aO(g)if e[g]<16 then v=v+1;U[v]=g end end local function aG(a)v=1 U[1]=a while v>0 do local g=U[v]U[v]=nil v=v-1 local d=e[g]if d<16 then e[g]=d+16 H=H+1 if d==0 then J(g,aO)end end end end local function aK(a)i,Q="lost",a e[a]=25 q=false end local function aR()i,q="won",false for a=1,f*j do if e[a]==9 then e[a]=41 end end o=D local b=I[p]b[2]=b[2]+1 M=false if w and(b[3]==0 or r<b[3])then b[3]=r;M=true end if b[4]==0 or t<b[4]then b[4]=t if not w then M=true end end end local function at(a)if i or e[a]>=16 then return false end if not A then aN(a)end if e[a]==9 then aK(a)else aG(a)end return true end local function _(a)local d=e[a]if d>=32 then e[a]=d-32;o=o-1 elseif d<16 then e[a]=d+32;o=o+1 else u="Flags go on covered cells";return false end return true end local z,S=0,0 local function aE(g)local d=e[g]if d>=32 then z=z+1 elseif d<16 then S=S+1 end end local function aM(g)if e[g]<16 then at(g)end end local function aF(g)if e[g]<16 then _(g)end end local function aD(a)local h=e[a]-16 z,S=0,0 J(a,aE)if h==0 or S==0 then u="Nothing left to open here";return false end if z==h then J(a,aM)elseif z+S==h then J(a,aF)elseif z>h then u="Too many flags round here";return false else u=h-z.." more mine"..(h-z==1 and""or"s").." round here"return false end return true end local function ap()if A and not i and w then local W=os.time()if W>L then r=r+(W-L)end L=W end end local function aq()local c={}if F then c[#c+1]={i and"Back to the last game"or"Back to the game","resume"}end for l=1,3 do local x=O[l]c[#c+1]={"New "..x.name..": "..x.w.." x "..x.h..", "..x.n.." mines",l}end c[#c+1]={"Statistics","stats"}c[#c+1]={"How to play","help"}return c end local function aL()ap()q=false n,s="menu",1 Y()end local ac={left=-1,right=1,up=0,down=0}local az={left=0,right=0,up=-1,down=1}function key(k)if n=="menu"then local G=aq()if k=="up"then s=(s-2)%#G+1 elseif k=="down"then s=s%#G+1 elseif k=="ok"then local P=G[s][2]if P=="resume"then n,u="play",nil;L=os.time()elseif P=="stats"or P=="help"then n=P else ar(P)end end return end if n~="play"then if k=="top"or k=="ok"then n="menu"end return end u=nil if k=="top"then if q or i then aL()else q=true end return end if ac[k]then local B,C=(m-1)%f,(m-1)//f B,C=(B+ac[k])%f,(C+az[k])%j m=C*f+B+1 return end if k~="ok"then return end if i then ar(p);return end ap()local d,R=e[m],false if q then q=false R=_(m)elseif d>=32 then R=_(m)elseif d>=16 then R=aD(m)else R=at(m)end if R then t=t+1 if not i and H==f*j-D then aR()end Y()end end function touch(aI,K,N)if aI~="board"or n~="play"or i or K<1 or K>f or N<1 or N>j then return end m=(N-1)*f+K key("ok")end local function aQ(a)local d=e[a]local y,T=d%16,d//16 if i then if T==1 then if y==9 then return a==Q and aw or af end return ag+y elseif T==2 then return y==9 and ad or aA end return y==9 and af or ae end local c=T==1 and ag+y or(T==2 and ad or ae)if a==m then c=c+ay end return c end local function aB()local au={}for C=0,j-1 do local N={}for B=1,f do N[B]=ai[aQ(C*f+B)]end au[C+1]=table.concat(N)end return table.concat(au,"/")end local function X()if w then return"Time "..V(r)end return t..(t==1 and" move"or" moves")end local function aP()if i=="lost"then return"Boom! That one was a mine."end if i=="won"then return"Cleared in "..(w and V(r)or X())..(M and" - best yet!"or"")end if u then return u end if q then return"Flag: OK marks this cell, BACK options"end if not A then return D.." mines. Open any cell to start."end return(D-o).." mines left    "..X()end local function ak(b)local c={}if b[3]>0 then c[#c+1]="best time "..V(b[3])end if b[4]>0 then c[#c+1]="fewest moves "..b[4]end return#c>0 and table.concat(c,", ")or"no wins yet"end function draw()if n=="menu"then snail.title("Mines")local G=aq()if s>#G then s=#G end for a,aH in ipairs(G)do snail.row(aH[1],a==s)end snail.gap()if F and not i and A then snail.small(O[p].name.." game in progress: "..(D-o).." mines left, "..X()..".")else snail.small("Your first cell is always safe.")end snail.hint("BACK leave OK choose UP/DN move")return end if n=="stats"then snail.title("Statistics")for l=1,3 do local b=I[l]snail.text(O[l].name..": won "..b[2].." of "..b[1])snail.small(ak(b))snail.gap()end if not al()then snail.small("Best times need the clock: connect to Wi-Fi once to set it.")end snail.hint("BACK back OK back")return end if n=="help"then snail.title("How to play")snail.text("Open every cell that has no mine under it.")snail.text("A number says how many mines touch that cell, diagonals too.")snail.text(snail.touchscreen and"Tap a cell to open it. The first cell is always safe."or"Arrows move, OK opens. The first cell is always safe.")snail.text("BACK then OK flags a cell you know is a mine. OK on a flag lifts it.")snail.text("OK on a number opens the cells round it once its mines are flagged.")snail.text("If its covered neighbours can only be mines, OK flags them all.")snail.text("BACK twice gets to the options. Your game is kept.")snail.hint("BACK back OK back")return end snail.title("Mines - "..O[p].name)snail.status(aP())local aj if i=="won"then aj=M and"Cleared! New best!"or"Field cleared!"end snail.board(aB(),nil,aj)if p<3 then local b=I[p]snail.small("Won "..b[2].." of "..b[1]..". "..ak(b):gsub("^%l",string.upper)..".")end if i then snail.hint("BACK options OK new game")elseif q then snail.hint("BACK options OK flag L/R move")else snail.hint("BACK flag OK open L/R move")end end function start()math.randomseed(os.time())snail.ink("fast")Z(1)aJ()if F and not i then n="play"L=os.time()else n="menu"s=F and p+1 or 1 end end
