--!name Checkers
--!icon game
--!category Games
local bq="ABCDEFGHIJKLMNOPQRSTUVW"local bp={}for a=1,#bq do bp[a-1]=bq:sub(a,a)end local am=9 local k,a0,G,a1,aE,aD={},{},{},{},{},{}for a=0,99 do k[a]=am;a0[a]=0;aE[a]=0;aD[a]=0 end for v=0,7 do for p=0,7 do if(v+p)%2==1 then local c=11+10*v+p G[#G+1]=c a1[c]=#G-1 k[c]=0 local d=(7-v)*3 if v==7 then d=8 end if p>=2 and p<=5 and v>=3 and v<=4 then d=d+4 end if p==0 or p==7 then d=d-3 end aE[c]=d aD[c]=8-(math.abs(2*v-7)+math.abs(2*p-7))//2 end end end local T={[0]=-11,-9,9,11}local aZ={[-11]=0,[-9]=1,[9]=2,[11]=3}local y,W,J,K=1,0,12,12 local X,aj=0,0 local br,bs,aF,bt,R,bo={},{},{},{},{},{}local function aS(h,c)if h==1 then return 100+aE[c]elseif h==-1 then return-100-aE[99-c]elseif h==2 then return 150+aD[c]end return-150-aD[c]end local function ch()W,J,K=0,0,0 for a=1,32 do local c=G[a]local h=k[c]if h~=0 then W=W+aS(h,c)if h>0 then J=J+1 else K=K+1 end end end end local function bI(al,g,m,n,h,i,b6,e)local ap,aq=0,3 if h==1 then if i==1 then aq=1 else ap=2 end end for as=ap,aq do local w=T[as]local F=n+w local j=k[F]if j~=0 and j~=am and j*i<0 and k[F+w]==0 then local a8=F+w local bP=b6|(as<<(11+2*e))k[F]=am local cd=g if e<9 and not(h==1 and(a8<19 or a8>80))then g=bI(al,g,m,a8,h,i,bP,e+1)end if g==cd then g=g+1;al[g]=m|((e+1)<<7)|bP end k[F]=j end end return g end local function bG(al,r)local i,g,au=y,r,0 for a=1,32 do local c=G[a]local d=k[c]local h=d*i if h>0 then local ap,aq=0,3 if h==1 then if i==1 then aq=1 else ap=2 end end local bH=false for as=ap,aq do local w=T[as]local j=k[c+w]if j==0 then if g==r then au=au+1;bo[au]=c|(as<<11)end elseif not bH and j~=am and j*i<0 and k[c+w+w]==0 then bH=true k[c]=0 g=bI(al,g,c,c,h,i,0,0)k[c]=d end end end end if g>r then return g-r,true end for a=1,au do al[r+a]=bo[a]end return au,false end local function bb(f,B)local m=f&127 local e=(f>>7)&15 local h=k[m]local bj=W-aS(h,m)bt[B]=W k[m]=0 local n=m if e==0 then n=m+T[(f>>11)&3]else for o=0,e-1 do local w=T[(f>>(11+2*o))&3]local F=n+w local j=k[F]X=X+1 br[X]=j bj=bj-aS(j,F)k[F]=0 n=F+w end if y==1 then K=K-e else J=J-e end end bs[B]=h aF[B]=n if h==1 and n<19 then h=2 elseif h==-1 and n>80 then h=-2 end k[n]=h W=bj+aS(h,n)y=-y end local function b0(f,B)y=-y local m=f&127 local e=(f>>7)&15 k[aF[B]]=0 k[m]=bs[B]W=bt[B]if e>0 then local n=m local r=X-e for o=0,e-1 do local w=T[(f>>(11+2*o))&3]local F=n+w k[F]=br[r+o+1]n=F+w end X=r if y==1 then K=K+e else J=J+e end end end local a2,a4=1000000,100000 local aR,bK,ae,aB,b1=0,0,false,0,140 local a6={}local bm,bn={},{}local function ck()aR=aR+1 if aR>bK then ae=true end aB=aB+1 if aB>=b1 then aB=0;coroutine.yield()end end local function b9()local d=W if J~=K then d=d+(J-K)*(24-J-K)*3 if J+K<=8 then local aU=J>K and 1 or-1 local z,V=0,0 for a=1,32 do local c=G[a]local h=k[c]*aU if h==2 then z=z+1;bm[z]=c elseif h<0 then V=V+1;bn[V]=c end end local bl=0 for a=1,z do local ab=bm[a]for o=1,V do local ac=bn[o]local bB=math.abs(ab//10-ac//10)local bA=math.abs(ab%10-ac%10)bl=bl+(bB>bA and bB or bA)end end d=d-aU*bl*2 end end return d*y end local function bV(ar,S,bu,B)ck()if ae then return 0 end local r=aj local g,ao=bG(R,r)if g==0 then return-a4+B end if(ar<=0 and not ao)or B>40 then return b9()end aj=r+g if not ao then local a7=a6[B]if a7 then for a=r+2,r+g do if R[a]==a7 then R[a],R[r+1]=R[r+1],a7;break end end end end local ce=g==1 and ar or ar-1 local C=-a2 for a=r+1,r+g do local f=R[a]bb(f,B)local d=-bV(ce,-bu,-S,B+1)b0(f,B)if ae then aj=r;return 0 end if d>C then C=d if d>S then S=d if d>=bu then if not ao then a6[B]=f end break end end end end aj=r return C end local b2={{2,300,60},{5,2000,10},{20,5000,0}}local a3={"Easy","Medium","Hard"}local H={}local function cl(ba)local ax=b2[ba]local N={}for a=1,#H do N[a]=H[a]end local g=#N if g==0 then return nil end if g==1 then return N[1]end for a=g,2,-1 do local o=math.random(a)N[a],N[o]=N[o],N[a]end aR,bK,ae,aB,aj,X=0,ax[2],false,0,0,0 a6={}local C=N[1]for ar=1,ax[1]do local S,aL,bv=-a2,nil,1 for a=1,g do local f=N[a]bb(f,0)local d=-bV(ar-1,-a2,ax[3]-S,1)b0(f,0)if ae then break end if ax[3]>0 then d=d+math.random(0,ax[3])end if d>S then S,aL,bv=d,f,a end end if aL then C=aL table.remove(N,bv)table.insert(N,1,aL)end if ae or S>a4//2 or S<-a4//2 then break end end return C end local O,U,ad,aC={},{},{},{}local az,x,aM,a9,aN=1,nil,"",0,0 local aa,aP,aQ,bc=0,12,12,false local A,D,I,ak,ag=1,1,2,false,false local an={0,0,0,0,0,0,0,0,0}local Y=""local function bS()local ai=y for a=1,32 do ai=ai*5+k[G[a]]+2 end return ai end local function bh()local _=Y for a=1,32 do local c=G[a]local d=0 if#_>0 then local j=_:sub(a,a)d=j=="b"and 1 or j=="B"and 2 or j=="w"and-1 or j=="W"and-2 or 0 elseif c<41 then d=-1 elseif c>60 then d=1 end k[c]=d end y,aa=1,0 if#_>0 then y=_:sub(33,33)=="w"and-1 or 1 aa=tonumber(_:sub(34))or 0 end X,aj=0,0 ch()O,U,aC={},{bS()},{}a9,aN,aM,x=0,0,"",nil end local function cg()local b={}for a=1,32 do local d=k[G[a]]b[a]=d==1 and"b"or d==2 and"B"or d==-1 and"w"or d==-2 and"W"or"."end return table.concat(b)..(y==1 and"b"or"w")..aa end local function cn(i)if A==1 then return i==D and"You"or"Computer"end return i==1 and"Black"or"White"end local function bR(f)local m=f&127 local e=(f>>7)&15 local l={m}if e==0 then l[2]=m+T[(f>>11)&3]else local n=m for o=0,e-1 do n=n+2*T[(f>>(11+2*o))&3]l[o+2]=n end end return l end local function bN(l)local m=l[1]if#l==2 and aZ[l[2]-m]then return m|(aZ[l[2]-m]<<11)end local f=m|((#l-1)<<7)for o=2,#l do local bz=aZ[(l[o]-l[o-1])//2]if not bz then return nil end f=f|(bz<<(11+2*(o-2)))end return f end local function bf(f)local i=y local m=f&127 local e=(f>>7)&15 local bL=k[m]==i aC={}if e>0 then local n=m for o=0,e-1 do local w=T[(f>>(11+2*o))&3]aC[n+w]=true n=n+2*w end end bb(f,0)X=0 a9,aN=m,aF[0]local b7=bL and k[aN]==2*i aM=cn(i)..": "..(bL and"man"or"king")..(e==0 and" moved"or(" took "..e))..(b7 and", crowned!"or"")O[#O+1]=f if e>0 then aa=0;U={}else aa=aa+1 end U[#U+1]=bS()end local function bT(e)if A~=1 or ag then return end ag=true local a=(I-1)*3+e an[a]=an[a]+1 end local function aw()local g,ao=bG(R,0)H,ad={},{}for a=1,g do H[a]=R[a];ad[a]=bR(R[a])end az,bc=y,ao aP,aQ=0,0 for a=1,32 do local c=G[a]local d=k[c]a0[c]=d if d>0 then aP=aP+1 elseif d<0 then aQ=aQ+1 end end x=nil if g==0 then local aA=-y if A==1 then x=aA==D and"You win!"or"Computer wins"bT(aA==D and 1 or 2)else x=(aA==1 and"Black"or"White").." wins"end else local bg,n=0,U[#U]for a=1,#U do if U[a]==n then bg=bg+1 end end if bg>=3 then x="Draw - same position 3 times"elseif aa>=80 then x="Draw - 40 moves, no jump"end if x then bT(3)end end end local function bW()local b={"K1",A,D==1 and"b"or"w",I,ag and 1 or 0,table.concat(an,","),":",Y,":"}for a=1,#O do local l=bR(O[a])b[#b+1]=string.char(65+a1[l[1]])for o=2,#l do b[#b+1]=string.char(33+a1[l[o]])end end return table.concat(b)end local function av(t,g)bh()for a=1,g do local f=t[a]local h=k[f&127]if h==0 or h==am or h*y<0 then break end bf(f)end aw()end local function bU()local i=bW()if#i>1000 then local t=O local ai=#t//2 av(t,ai)Y=cg()local aW={}for a=ai+1,#t do aW[#aW+1]=t[a]end av(aW,#aW)i=bW()end snail.save(i)end local function ci()local i=snail.load()if type(i)~="string"then return false end local aO,aK,ba,b8,aU,_,bO=i:match("^K1(%d)([bw])(%d)(%d)([%d,]+):([^:]*):(.*)$")if not aO then return false end local a=0 for d in aU:gmatch("%d+")do a=a+1;an[a]=tonumber(d)end A,D,I=tonumber(aO),aK=="b"and 1 or-1,tonumber(ba)if I<1 or I>3 then I=2 end Y=_ local t,l={},nil for e=1,#bO do local j=bO:byte(e)if j>=65 then if l then t[#t+1]=bN(l)end l={G[j-64]}elseif l then l[#l+1]=G[j-32]end end if l then t[#t+1]=bN(l)end ag=true av(t,#t)ag=b8=="1"ak=true return true end local af,aX=nil,false local Q,E,u="menu",1,76 local q,s=nil,{}local ah=false local function bk()af,aX=nil,false snail.tick(0)end local aV local function bX()aX=true af=coroutine.create(function()return cl(I)end)snail.after(aV)snail.tick(450)end local function at()return not x and not aX and(A==2 or az==D)end local function bw()local be={}for a=1,#H do local l=ad[a]if l[1]==q then local bF=true for o=1,#s do if l[o+1]~=s[o]then bF=false;break end end if bF then be[#be+1]=a end end end return be end local function ay()local b,L={},{}if not q then for a=1,#H do local p=ad[a][1]if not L[p]then L[p]=true;b[#b+1]=p end end else local j=bw()for a=1,#j do local aT=ad[j[a]][#s+2]if aT and not L[aT]then L[aT]=true;b[#b+1]=aT end end if#s==0 then b[#b+1]=q end end return b,L end local function aY(c)local p,v=(c-11)%10,(c-11)//10 if ah then return 7-p,7-v end return p,v end local function aJ()local b,L=ay()if L[u]or#b==0 then return end local aH,aI=aY(u)local C,Z for a=1,#b do local ab,ac=aY(b[a])local j=(ab-aH)^2+(ac-aI)^2 if b[a]~=q and(not Z or j<Z)then C,Z=b[a],j end end u=C or u end local function aG()q,s=nil,{}ah=A==1 and D==-1 bU()if not x and A==1 and az~=D then bX()else aJ()end end aV=function()if not af then return end local cf,f=coroutine.resume(af)if coroutine.status(af)~="dead"then return end bk()local bJ=false for a=1,#H do if H[a]==f then bJ=true end end if not cf or not bJ then local t=O av(t,#t)f=H[math.random(#H)]end if not f then return end bf(f)aw()aG()end local function bd(aO,aK)bk()A,D,ak,ag,Y=aO,aK,true,false,""bh()aw()u=aK==1 and 76 or 23 Q="play"aG()end local function b_()local g=#O-1 local bE=#Y>0 and(Y:sub(33,33)=="w"and-1 or 1)or 1 if A==1 then while g>=0 and((g%2==0)and bE or-bE)~=D do g=g-1 end end if g<0 then return nil end return g end local function cm()local g=b_()if not g then return end bk()local t=O av(t,g)Q="play"aG()end local function bM()local b={}if ak then b[#b+1]={"Back to the game","resume"}end b[#b+1]={"New game as Black (first)","b"}b[#b+1]={"New game as White","w"}b[#b+1]={"New game, two players","2"}b[#b+1]={"Computer level: "..a3[I],"lvl"}if ak and b_()then b[#b+1]={"Take back a move","undo"}end return b end local function cb(bC,bD)local b=ay()local aH,aI=aY(u)local C,Z for bQ=1,2 do for a=1,#b do local ab,ac=aY(b[a])local h=(ab-aH)*bC+(ac-aI)*bD local i=math.abs((ab-aH)*bD)+math.abs((ac-aI)*bC)if h>0 and(bQ==2 or i<=h)then local j=bQ==1 and h*2+i or h+i*3 if not Z or j<Z then C,Z=b[a],j end end end if C then break end end if C then u=C end end local a_={up={0,-1},down={0,1},left={-1,0},right={1,0}}function key(e)if Q=="menu"then local P=bM()if E>#P then E=#P end if e=="up"then E=(E-2)%#P+1 elseif e=="down"then E=E%#P+1 elseif e=="ok"or((e=="left"or e=="right")and P[E][2]=="lvl")then local z=P[E][2]if z=="resume"then Q="play"elseif z=="b"then bd(1,1)elseif z=="w"then bd(1,-1)elseif z=="2"then bd(2,1)elseif z=="lvl"then I=(I+(e=="left"and 1 or 0))%3+1 if ak then bU()end elseif z=="undo"then cm()end end return end if e=="top"then if#s>0 then u=s[#s]s[#s]=nil elseif q then u=q q=nil else Q="menu"E=1 end return end if x then if e=="ok"then Q="menu";E=2 end return end if not at()then return end if e=="ok"then local b4,L=ay()if not q then if L[u]then q=u;aJ()end elseif u==q and#s==0 then q=nil elseif L[u]then s[#s+1]=u local j=bw()if#j==1 then local f=H[j[1]]bf(f)aw()u=aF[0]aG()else aJ()end end elseif a_[e]then cb(a_[e][1],a_[e][2])end end function touch(cc,z,V)if cc~="board"or Q~="play"or not at()then return end if z<1 or z>8 or V<1 or V>8 then return end local p,v=z-1,V-1 if ah then p,v=7-p,7-v end local c=11+10*v+p if q and c~=q and#s==0 then for a=1,#H do if ad[a][1]==c then key("top");break end end end local b4,L=ay()if L[c]or(c==q and#s==0)then u=c;key("ok")end end function tick()aV()if af then snail.after(aV)end end local b3={[1]=1,[2]=2,[-1]=3,[-2]=4}local function b5()local a5=at()and Q=="play"local bY,bx={},{}if a5 and q then local b=ay()for a=1,#b do bY[b[a]]=true end for a=1,#s do bx[s[a]]=true end end local b={}for bi=0,7 do local v=ah and 7-bi or bi for by=0,7 do local p=ah and 7-by or by local c=11+10*v+p local a=0 if(v+p)%2==1 then local M=b3[a0[c]]or 0 if a5 and c==u then a=M==0 and 6 or 6+M elseif a5 and q then if c==q then a=10+M elseif bx[c]then a=22 elseif bY[c]then a=15 else a=M==0 and 1 or 1+M end elseif c==aN and M>0 then a=16+M elseif M==0 and c==a9 then a=16 elseif M==0 and aC[c]then a=21 else a=M==0 and 1 or 1+M end end b[#b+1]=bp[a]end if bi<7 then b[#b+1]="/"end end return table.concat(b)end local function cj()local b="Black "..aP.."  White "..aQ.." - "if x then return b.."game over"end if aX then return b.."thinking..."end if A==1 then return b..(bc and"you must jump"or"your move")end return b..(az==1 and"Black"or"White")..(bc and" must jump"or" to move")end function draw()if Q=="menu"then snail.title("Checkers")local P=bM()if E>#P then E=#P end for a,ca in ipairs(P)do snail.row(ca[1],a==E)end snail.gap()if ak then local aA=A==2 and"Two players"or("You play "..(D==1 and"Black"or"White")..", "..a3[I])snail.small(aA..(x and(". "..x..".")or(", move "..(#O//2+1)..".")))else snail.small("Black moves first. Jumps are compulsory and a jump goes on while it can.")end local i=an snail.small("Won-lost-drawn against the computer:")snail.small("Easy "..i[1].."-"..i[2].."-"..i[3].."   Medium "..i[4].."-"..i[5].."-"..i[6].."   Hard "..i[7].."-"..i[8].."-"..i[9])snail.hint("BACK leave OK choose UP/DN move")return end snail.title(A==1 and("Checkers - "..a3[I])or"Checkers - two players")snail.status(cj())local bZ=aM~=""and aM or(snail.touchscreen and"Tap a piece, then where it goes."or"Move with the arrows, OK picks a piece.")if q and at()then bZ=#s>0 and"Keep jumping: pick the next square."or"Pick a square for it, or OK on it to cancel."end snail.text(bZ)snail.board(b5(),nil,x)if x then snail.hint("BACK options OK new game")elseif not at()then snail.hint("BACK options")elseif q then snail.hint("BACK undo step OK here L/R move UP/DN move")else snail.hint("BACK options OK pick L/R move UP/DN move")end end function start()math.randomseed(os.time())bh()aw()if ci()then ah=A==1 and D==-1 if not x then Q="play"if A==1 and az~=D then bX()else aJ()end end end end
