--!name Reversi
--!icon game
--!category Games
local a0="ABCDEFGHIJ"local a_={}for b=1,#a0 do a_[b-1]=a0:sub(b,b)end local bk,an=9,100 local h,l,B,L,aF,S={},{},{},{},{},{}for b=0,an do h[b]=bk;l[b]=0;B[b]=0;L[b]=0;aF[b]=0 end do local bl={100,-20,10,5,5,10,-20,100,-20,-50,-2,-2,-2,-2,-50,-20,10,-2,1,1,1,1,-2,10,5,-2,1,0,0,1,-2,5,5,-2,1,0,0,1,-2,5,10,-2,1,1,1,1,-2,10,-20,-50,-2,-2,-2,-2,-50,-20,100,-20,10,5,5,10,-20,100}for ae=0,7 do for as=0,7 do local a=11+10*ae+as S[#S+1]=a l[a]=bl[ae*8+as+1]end end end local aJ={}for b=1,64 do aJ[b]=S[b]end table.sort(aJ,function(i,w)if l[i]~=l[w]then return l[i]>l[w]end return i<w end)local aZ={-11,-10,-9,-1,1,9,10,11}local aj={11,22,12,21,18,27,17,28,81,72,82,71,88,77,87,78}local g,H,O,D,y=1,0,0,60,0 local aG,a1,al={},{},{}local function aM(a,e)local Y=-e for b=1,8 do local r=aZ[b]local c=a+r if h[c]==Y then repeat c=c+r until h[c]~=Y if h[c]==e then return true end end end return false end local function aw(a,G)local e,Y=g,-g local ag,m=y,H for b=1,8 do local r=aZ[b]local c=a+r if h[c]==Y then repeat c=c+r until h[c]~=Y if h[c]==e then c=c-r repeat h[c]=e ag=ag+1 aG[ag]=c m=m+2*e*l[c]c=c-r until c==a end end end local d=ag-y if d==0 then return 0 end a1[G]=y y=ag h[a]=e H=m+e*l[a]O=O+e*(d+d+1)local i,w=L[a],B[a]B[i]=w L[w]=i D=D-1 g=Y return d end local function aY(a,G)local e=-g g=e local aQ=a1[G]local m=H-e*l[a]for b=y,aQ+1,-1 do local c=aG[b]h[c]=-e m=m-2*e*l[c]end local d=y-aQ y=aQ H=m h[a]=0 O=O-e*(d+d+1)B[L[a]]=a L[B[a]]=a D=D+1 end local function aN(M)local d,e=0,g local ab=B[0]while ab~=an do if aM(ab,e)then d=d+1;al[M+d]=ab end ab=B[ab]end return d end local aH,ao=1000000,100000 local bj=8 local ay,a8,T,ai,bh,ac=0,0,false,0,1700,0 local function bB(J)ay=ay+J if ay>a8 then T=true end ai=ai+J if ai>=bh then ai=0;coroutine.yield()end end local function bp(Q,aT)local f=H for b=1,16,4 do if h[aj[b]]~=0 then local R,a3,a4=aj[b+1],aj[b+2],aj[b+3]f=f-l[R]*h[R]-l[a3]*h[a3]-l[a4]*h[a4]end end f=f*g+bj*(Q-aT)if D<16 then f=f+O*g*(16-D)//2 end return f end local function bq()local r=O*g if r>0 then return ao+r elseif r<0 then return-ao+r end return 0 end local function aW(W,v,aK,G,aT,bv)bB(D+4)if T then return 0 end local M=ac local d=aN(M)if d==0 then if bv then return bq()end g=-g local f=-aW(W,-aK,-v,G+1,0,true)g=-g return f end if W<=0 then return bp(d,aT)end ac=M+d local j=-aH for b=1,d do local a=al[M+b]aw(a,G)local f=-aW(W-1,-aK,-v,G+1,d,false)aY(a,G)if T then ac=M;return 0 end if f>j then j=f if f>v then v=f if f>=aK then break end end end end ac=M return j end local bi={{1,0,0},{3,3000,12},{12,52000,0}}local aI={"Easy","Medium","Hard"}local o={}local function bC(av)local ah=bi[av]local I={}for b=1,#o do I[b]=o[b]end local d=#I if d==0 then return nil end if d==1 then return I[1]end if av==1 then local j,aL for b=1,d do local a=I[b]local f=aw(a,0)+math.random(0,4)aY(a,0)if l[a]==100 and math.random(2)==1 then f=f+6 end if not aL or f>aL then j,aL=a,f end end return j end ay,a8,T,ai,ac=0,ah[2],false,0,0 local j=I[1]for W=1,ah[1]do local v,au,a2=-aH,nil,1 for b=1,d do local a=I[b]aw(a,0)local f=-aW(W-1,-aH,ah[3]-v,1,d,false)aY(a,0)if T then break end if ah[3]>0 then f=f+math.random(0,ah[3])end if f>v then v,au,a2=f,a,b end end if au then j=au table.remove(I,a2)table.insert(I,1,au)end if T or v>ao//2 or v<-ao//2 or W>=D then break end end return j end local t,am,K,ak={},{},{},{}local _,n,X,aP=1,nil,"",0 local C,E=2,2 local q,u,s,Z,V=1,1,2,false,false local aa={0,0,0,0,0,0,0,0,0}local function aV()for b=1,64 do h[S[b]]=0 end h[44],h[55],h[45],h[54]=-1,-1,1,1 local ad=0 for b=1,64 do local a=aJ[b]if h[a]==0 then B[ad]=a;L[a]=ad;ad=a end end B[ad]=an L[an]=ad g,y,D,H,O=1,0,60,0,0 for b=1,64 do local a=S[b]H=H+l[a]*h[a]end t,am,ak={},{},{}aP,X,n=0,"",nil end local function by(a)return string.char(97+(a-11)%10)..((a-11)//10+1)end local function bg(e)if q==1 then return e==u and"You"or"Computer"end return e==1 and"Black"or"White"end local function aS(a)local e=g local d=aw(a,0)ak={}for b=1,y do ak[aG[b]]=true end y=0 t[#t+1]=a am[#am+1]=e aP=a X=bg(e)..": "..by(a)..", "..d..(d==1 and" disc"or" discs").." turned"end local function bc(k)if q~=1 or V then return end V=true local b=(s-1)*3+k aa[b]=aa[b]+1 end local function af()local d=aN(0)if d==0 then g=-g d=aN(0)if d==0 then g=-g elseif#t>0 then local m=bg(-g)X=X..". "..m..(m=="You"and" have"or" has").." no move"end end o,K={},{}for b=1,d do o[b]=al[b];K[al[b]]=true end _=g C,E=0,0 for b=1,64 do local a=S[b]local f=h[a]aF[a]=f if f==1 then C=C+1 elseif f==-1 then E=E+1 end end n=nil if d==0 then local az=C.."-"..E if E>C then az=E.."-"..C end if C==E then n="Draw "..az bc(3)else local J=C>E and 1 or-1 if q==1 then n=(J==u and"You win "or"Computer wins ")..az bc(J==u and 1 or 2)else n=(J==1 and"Black"or"White").." wins "..az end end end end local function bx()local c={"R1",q,u==1 and"b"or"w",s,V and 1 or 0,table.concat(aa,","),":"}for b=1,#t do local a=t[b]c[#c+1]=string.char(48+(a-11)//10*8+(a-11)%10)end return table.concat(c)end local function bd()snail.save(bx())end local function aU(P,d)aV()for b=1,d do local a=P[b]if not aM(a,g)then g=-g end if h[a]~=0 or not aM(a,g)then break end aS(a)end af()end local function bw()local e=snail.load()if type(e)~="string"then return false end local ax,aO,av,bo,bz,ba=e:match("^R1(%d)([bw])(%d)(%d)([%d,]+):(.*)$")if not ax then return false end local b=0 for f in bz:gmatch("%d+")do b=b+1;aa[b]=tonumber(f)end q,u,s=tonumber(ax),aO=="b"and 1 or-1,tonumber(av)if s<1 or s>3 then s=2 end local P={}for k=1,#ba do local F=ba:byte(k)-48 P[k]=11+(F//8)*10+F%8 end V=true aU(P,#P)V=bo=="1"Z=true return true end local U,aB=nil,false local A,p,x="menu",1,34 local function aX()U,aB=nil,false snail.tick(0)end local aA local function be()aB=true U=coroutine.create(function()return bC(s)end)snail.after(aA)snail.tick(450)end local function at()return not n and not aB and(q==2 or _==u)end local function aC(a)return(a-11)%10,(a-11)//10 end local function a7()if K[x]or#o==0 then return end local aq,ar=aC(x)local j,N for b=1,#o do local R,aD=aC(o[b])local F=(R-aq)^2+(aD-ar)^2 if not N or F<N then j,N=o[b],F end end x=j end local function ap()bd()if not n and q==1 and _~=u then be()else a7()end end aA=function()if not U then return end local bu,Q=coroutine.resume(U)if coroutine.status(U)~="dead"then return end aX()if not bu or not Q or not K[Q]then aU(t,#t)Q=o[math.random(#o)]end if not Q then return end aS(Q)af()ap()end local function aR(ax,aO)aX()q,u,Z,V=ax,aO,true,false aV()af()x=34 A="play"ap()end local function bf()local d=#t-1 if q==1 then while d>=0 and am[d+1]~=u do d=d-1 end end if d<0 then return nil end return d end local function bD()local d=bf()if not d then return end aX()local P=t aU(P,d)A="play"ap()end local function a9()local c={}if Z then c[#c+1]={"Back to the game","resume"}end c[#c+1]={"New game as Black (first)","b"}c[#c+1]={"New game as White","w"}c[#c+1]={"New game, two players","2"}c[#c+1]={"Computer level: "..aI[s],"lvl"}if Z and bf()then c[#c+1]={"Take back a move","undo"}end return c end local function bs(a5,a6)local aq,ar=aC(x)local j,N for bb=1,2 do for b=1,#o do local R,aD=aC(o[b])local m=(R-aq)*a5+(aD-ar)*a6 local e=math.abs((R-aq)*a6)+math.abs((aD-ar)*a5)if m>0 and(bb==2 or e<=m)then local F=bb==1 and m*2+e or m+e*3 if not N or F<N then j,N=o[b],F end end end if j then break end end if j then x=j end end local aE={up={0,-1},down={0,1},left={-1,0},right={1,0}}function key(k)if A=="menu"then local z=a9()if p>#z then p=#z end if k=="up"then p=(p-2)%#z+1 elseif k=="down"then p=p%#z+1 elseif k=="ok"or((k=="left"or k=="right")and z[p][2]=="lvl")then local i=z[p][2]if i=="resume"then A="play"elseif i=="b"then aR(1,1)elseif i=="w"then aR(1,-1)elseif i=="2"then aR(2,1)elseif i=="lvl"then s=(s+(k=="left"and 1 or 0))%3+1 if Z then bd()end elseif i=="undo"then bD()end end return end if k=="top"then A="menu";p=1;return end if n then if k=="ok"then A="menu";p=2 end return end if not at()then return end if k=="ok"then if K[x]then aS(x)af()ap()end elseif aE[k]then bs(aE[k][1],aE[k][2])end end function touch(bt,i,w)if bt~="board"or A~="play"or not at()then return end if i<2 or i>9 or w<2 or w>9 then return end local a=11+10*(w-2)+i-2 if K[a]then x=a;key("ok")end end function tick()aA()if U then snail.after(aA)end end local function bn(a)local f=aF[a]if f==0 then if K[a]and at()and A=="play"then if a==x then return _==1 and 4 or 5 end return 3 end return 0 end if a==aP then return f==1 and 6 or 7 end if ak[a]then return f==1 and 8 or 9 end return f==1 and 1 or 2 end local function bm()local c={" abcdefgh"}for ae=0,7 do c[#c+1]="/"..(ae+1)for as=0,7 do c[#c+1]=a_[bn(11+10*ae+as)]end end return table.concat(c)end local function bA()local c="Black "..C.."  White "..E.." - "if n then return c.."game over"end if aB then return c.."thinking..."end if q==1 then return c.."your move"end return c..(_==1 and"Black"or"White").." to move"end function draw()if A=="menu"then snail.title("Reversi")local z=a9()if p>#z then p=#z end for b,br in ipairs(z)do snail.row(br[1],b==p)end snail.gap()if Z then local J=q==2 and"Two players"or("You play "..(u==1 and"Black"or"White")..", "..aI[s])snail.small(J..(n and(". "..n..".")or(", move "..(#t+1)..".")))else snail.small("Black moves first. Turn over discs by trapping them between two of yours.")end local e=aa snail.small("Won-lost-drawn against the computer:")snail.small("Easy "..e[1].."-"..e[2].."-"..e[3].."   Medium "..e[4].."-"..e[5].."-"..e[6].."   Hard "..e[7].."-"..e[8].."-"..e[9])snail.hint("BACK leave OK choose UP/DN move")return end snail.title(q==1 and("Reversi - "..aI[s])or"Reversi - two players")snail.status(bA())snail.text(X~=""and X or(snail.touchscreen and"Tap a dot to place a disc."or"Pick a dot with the arrows, OK places a disc."))snail.board(bm(),nil,n)if n then snail.hint("BACK options OK new game")elseif not at()then snail.hint("BACK options")else snail.hint("BACK options OK place L/R move UP/DN move")end end function start()math.randomseed(os.time())aV()af()if bw()then if not n then A="play"if q==1 and _~=u then be()else a7()end end end end
