--!name Chess
--!icon game
--!category Games
local bg="!$%&()+,-09:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`ijklmnpqrstuvwxyz"local bo={}for a=1,#bg do bo[a-1]=bg:sub(a,a)end local bn="KKKKKKKKUUUUUUUUMMOQQOMMLLMPPMLLKKKOOKKKLJIKKIJLLMMGGMMLKKKKKKKKACEEEECACGKKKKGCEKMNNMKEELNOONLEEKNOONKEELMNNMLECGKLLKGCACEEEECAGIIIIIIGIKKKKKKIIKLMMLKIILLMMLLIIKMMMMKIIMMMMMMIILKKKKLIGIIIIIIGKKKKKKKKLMMMMMMLJKKKKKKJJKKKKKKJJKKKKKKJJKKKKKKJJKKKKKKJKKKLLKKKGIIJJIIGIKKKKKKIIKLLLLKIJKLLLLKJKKLLLLKJILLLLLKIIKLKKKKIGIIJJIIGECCAACCEECCAACCEECCAACCEECCAACCEGEECCEEGIGGGGGGIOOKKKKOOOQMKKMQOACEGGECAEGIKKIGEEIOQQOIEEIQSSQIEEIQSSQIEEIOQQOIEEEKKKKEEAEEEEEEA"local bt=string.byte local aX={0,64,128,192,256,320}local aW={100,320,330,500,900,0}local am={100,320,330,500,900,2000}local bm={"Pawn","Knight","Bishop","Rook","Queen","King"}local ak=99 local g,bi,O,al,bl={},{},{},{},{}for a=0,119 do g[a]=ak end for p=0,7 do for o=0,7 do local i=21+o+10*p O[#O+1]=i al[i]=p*8+o+1 bl[i]=(7-p)*8+o+1 end end local bj={-21,-19,-12,-8,8,12,19,21}local av={-11,-10,-9,-1,1,9,10,11}local b_={true,false,true,false,false,true,false,true}local b0={[3]={-11,-9,9,11},[4]={-10,-1,1,10},[5]=av}local bh={[95]=12,[98]=14,[91]=13,[25]=3,[28]=11,[21]=7}local t,U,aa,V,ah=1,15,0,0,0 local W={}local bT,bW,bU,bV,bX={},{},{},{},{}local F=524288 local aw={}for a=0,40 do aw[a]={}end local function ac(e,i)if e>0 then return aW[e]+(bt(bn,aX[e]+al[i])-75)*5 end return-aW[-e]-(bt(bn,aX[-e]+bl[i])-75)*5 end local function J(i,ae)if ae==1 then if g[i+9]==1 or g[i+11]==1 then return true end elseif g[i-9]==-1 or g[i-11]==-1 then return true end local c,m=2*ae,6*ae for a=1,8 do local l=g[i+bj[a]]if l==c then return true end if g[i+av[a]]==m then return true end end local ao,p,r=3*ae,4*ae,5*ae for a=1,8 do local _=av[a]local d=i+_ local l=g[d]while l==0 do d=d+_;l=g[d]end if l==r then return true end if b_[a]then if l==ao then return true end elseif l==p then return true end end return false end local function aZ(k,ax)local c,h=0,t for a=1,64 do local b=O[a]local e=g[b]*h if e>0 and e<ak then if e==1 then local by=-10*h local j=b+by local bI=(h==1 and j<30)or(h==-1 and j>90)for _=-1,1,2 do local d=j+_ local l=g[d]if l~=ak and l*h<0 then if l*h==-6 then return-1 end local q=am[-l*h]*10-1 if bI then for r=5,2,-1 do c=c+1;k[c]=b+d*128+r*16384+(q+am[r])*F end else c=c+1;k[c]=b+d*128+(q)*F end elseif d==aa then c=c+1;k[c]=b+d*128+131072+(999)*F end end if g[j]==0 then if bI then for r=5,2,-1 do c=c+1;k[c]=b+j*128+r*16384+(am[r])*F end elseif not ax then c=c+1;k[c]=b+j*128 if(h==1 and b>80)or(h==-1 and b<39)then local bS=j+by if g[bS]==0 then c=c+1;k[c]=b+bS*128+393216 end end end end elseif e==2 or e==6 then local au=e==2 and bj or av for B=1,8 do local d=b+au[B]local l=g[d]if l==0 then if not ax then c=c+1;k[c]=b+d*128 end elseif l~=ak and l*h<0 then if l*h==-6 then return-1 end c=c+1;k[c]=b+d*128+(am[-l*h]*10-e)*F end end if e==6 and not ax and b==(h==1 and 95 or 25)then local bA=h==1 and 1 or 4 if U&bA~=0 and g[b+1]==0 and g[b+2]==0 and g[b+3]==4*h and not J(b,-h)and not J(b+1,-h)and not J(b+2,-h)then c=c+1;k[c]=b+(b+2)*128+262144 end if U&(bA*2)~=0 and g[b-1]==0 and g[b-2]==0 and g[b-3]==0 and g[b-4]==4*h and not J(b,-h)and not J(b-1,-h)and not J(b-2,-h)then c=c+1;k[c]=b+(b-2)*128+262144 end end else local au=b0[e]for B=1,#au do local _=au[B]local d=b+_ while true do local l=g[d]if l~=0 then if l~=ak and l*h<0 then if l*h==-6 then return-1 end c=c+1;k[c]=b+d*128+(am[-l*h]*10-e)*F end break end if not ax then c=c+1;k[c]=b+d*128 end d=d+_ end end end end end return c end local function ap(f,n)local b,j=f%128,(f//128)%128 local r,A=(f//16384)%8,f//131072 local h=t local e,l=g[b],g[j]bT[n],bW[n],bU[n],bV[n],bX[n]=l,U,aa,V,ah local E=ah-ac(e,b)if l~=0 then E=E-ac(l,j)end local cf=e==h if r>0 then e=r*h end g[b]=0 g[j]=e E=E+ac(e,j)if A==1 then local ad=j+10*h E=E-ac(-h,ad)g[ad]=0 elseif A==2 then local aL,be=b+3,b+1 if j<b then aL,be=b-4,b-1 end local p=g[aL]E=E-ac(p,aL)+ac(p,be)g[aL]=0 g[be]=p end if e==6*h then W[h]=j end if l~=0 or cf then V=0 else V=V+1 end aa=A==3 and(b+j)//2 or 0 U=U&(bh[b]or 15)&(bh[j]or 15)ah=E t=-h end local function aQ(f,n)local h=-t t=h local b,j=f%128,(f//128)%128 local r,A=(f//16384)%8,f//131072 local e=g[j]if r>0 then e=h end g[b]=e g[j]=bT[n]if A==1 then g[j+10*h]=-h elseif A==2 then if j>b then g[b+3]=g[b+1];g[b+1]=0 else g[b-4]=g[b-1];g[b-1]=0 end end if e==6*h then W[h]=b end U,aa,V,ah=bW[n],bU[n],bV[n],bX[n]end local function bC(ba)local k=aw[39]local c=aZ(k,false)local m=0 for a=1,c do local f=k[a]%F ap(f,40)if not J(W[-t],t)then m=m+1;ba[m]=f end aQ(f,40)end for a=#ba,m+1,-1 do ba[a]=nil end return m end local aU,bk,at=1000000,100000,500000 local aH,bD,T,S,bZ=0,0,false,0,520 local a1={}local function bL()local a9,E=0,0 for a=1,64 do local e=g[O[a]]if e~=0 and e~=-1 and e~=1 then a9=a9+aW[e<0 and-e or e]end end aX[6]=a9<=2600 and 384 or 320 for a=1,64 do local i=O[a]if g[i]~=0 then E=E+ac(g[i],i)end end ah=E end local function bF(k,c,n)local m=0 for a=1,c do if k[a]>=F then m=m+1 k[a],k[m]=k[m],k[a]end end for a=2,m do local q,B=k[a],a-1 while B>0 and k[B]<q do k[B+1]=k[B];B=B-1 end k[B+1]=q end local bB=a1[n]if bB then for a=m+1,c do if k[a]==bB then k[a],k[m+1]=k[m+1],k[a];break end end end end local function bR(ck)aH=aH+1 if aH>bD then T=true end S=S+ck if S>=bZ then S=0;coroutine.yield()end end local function bK(u,Z,n)bR(2)if T then return 0 end if J(W[-t],t)then return at end local ar=ah*t if ar>=Z or n>30 then return ar end local k=aw[n]local c=aZ(k,true)S=S+5+c if ar>u then u=ar end bF(k,c,0)for a=1,c do if ar+k[a]//F//10+200>u then local f=k[a]%F ap(f,n)local q=-bK(-Z,-u,n+1)aQ(f,n)if T then return 0 end if q~=-at then if q>=Z then return q end if q>u then u=q end end end end return u end local function bN(aA,u,Z,n)if aA<=0 then return bK(u,Z,n)end bR(8)if T then return 0 end local k=aw[n]local c=aZ(k,false)if c<0 then return at end bF(k,c,n)local a4,z=0,-aU for a=1,c do local f=k[a]%F S=S+1 ap(f,n)local q=-bN(aA-1,-Z,-u,n+1)aQ(f,n)if T then return 0 end if q~=-at then a4=a4+1 if q>z then z=q end if q>u then u=q if q>=Z then if k[a]<F then a1[n]=f end return q end end end end if a4==0 then if J(W[t],-t)then return-bk+n end return 0 end return z end local b1={{1,200,90},{3,1500,20},{6,4000,0}}local aV={"Easy","Medium","Hard"}local function ci(a5)local aM=b1[a5]local Q={}local c=bC(Q)if c==0 then return nil end for a=c,2,-1 do local B=math.random(a)Q[a],Q[B]=Q[B],Q[a]end bL()aH,bD,T,S=0,aM[2],false,0 a1={}local z=Q[1]for aA=1,aM[1]do local u,aD,br=-aU,nil,1 for a=1,c do local f=Q[a]ap(f,0)local q=-bN(aA-1,-aU,-u,1)aQ(f,0)if T then break end if aM[3]>0 then q=q+math.random(0,aM[3])end if q>u then u,aD,br=q,f,a end end if aD then z=aD table.remove(Q,br)table.insert(Q,1,aD)end if T or u>bk-100 then break end end return z end local G,M,N={},{},{}local aj,v,a0,aE=1,nil,false,""local a2,a3=0,0 local x,C,H,ai=1,1,2,false local function bH()local a_=t*7+U*131+aa for a=1,64 do a_=a_*31+g[O[a]]+7 end return a_ end local function aK()local bq={4,2,3,5,6,3,2,4}for o=0,7 do g[21+o]=-bq[o+1]g[31+o]=-1 for p=2,5 do g[21+o+10*p]=0 end g[81+o]=1 g[91+o]=bq[o+1]end W[1],W[-1]=95,25 t,U,aa,V=1,15,0,0 G,M={},{}a2,a3,aE=0,0,""bL()M[1]=bH()end local function bO(i)local o,p=(i-21)%10,(i-21)//10 return string.char(97+o)..(8-p)end local function b5(f)local b,j=f%128,(f//128)%128 local r,A=(f//16384)%8,f//131072 local as=t==1 and"White"or"Black"if x==1 then as=t==C and"You"or"Computer"end if A==2 then return as..": castles "..(j>b and"kingside"or"queenside")end local e=g[b]local d=as..": "..bm[e<0 and-e or e].." "..bO(b)..((g[j]~=0 or A==1)and"x"or"-")..bO(j)if r>0 then d=d.." = "..bm[r]end return d end local function aJ(f,b9)aE=b5(f)a2,a3=f%128,(f//128)%128 ap(f,40)G[#G+1]=f if V==0 then M={}end if b9 then M[#M+1]=bH()end end local function b6()local a6=0 for a=1,64 do local e=g[O[a]]if e<0 then e=-e end if e==1 or e==4 or e==5 then return false end if e==2 or e==3 then a6=a6+1 end end return a6<=1 end local function aq()local c=bC(N)aj=t a0=J(W[t],-t)v=nil if c==0 then v=a0 and("Checkmate - "..(t==1 and"Black"or"White").." wins")or"Stalemate - draw"elseif V>=100 then v="Draw - fifty moves"elseif b6()then v="Draw - too few pieces"else local bc,b4=0,M[#M]for a=1,#M do if M[a]==b4 then bc=bc+1 end end if bc>=3 then v="Draw - repetition"end end for a=1,64 do bi[a]=g[O[a]]end end local function cc(b,j,r)local e=g[b]local A=0 if(e==1 or e==-1)then if j==aa and(j-b)%10~=0 then A=1 elseif j-b==20 or b-j==20 then A=3 end elseif(e==6 or e==-6)and(j-b==2 or b-j==2)then A=2 end return b+j*128+r*16384+A*131072 end local function cg()local d={"C1",x,C==1 and"w"or"b",H,":"}for a=1,#G do local f=G[a]d[#d+1]=string.char(47+al[f%128],47+al[(f//128)%128])local r=(f//16384)%8 if r>0 then d[#d+1]=string.char(110+r)end end return table.concat(d)end local function bM()local h=cg()if#h<=1020 then snail.save(h)end end local function bP(l)return O[l-47]end local function bb(P,c)aK()for a=1,c do aJ(P[a],a>c-110)end aq()end local function cb()local h=snail.load()if type(h)~="string"then return false end local aF,aB,a5,aG=h:match("^C1(%d)([wb])(%d):(.*)$")if not aF then return false end x,C,H=tonumber(aF),aB=="w"and 1 or-1,tonumber(a5)if H<1 or H>3 then H=2 end aK()local a,c=1,0 local P={}while a<#aG do local b,j=bP(aG:byte(a)),bP(aG:byte(a+1))a=a+2 local r=0 local l=aG:byte(a)if l and l>111 then r=l-110;a=a+1 end if not b or not j or g[b]==0 then break end local f=cc(b,j,r)c=c+1 P[c]=f aJ(f,false)end bb(P,c)ai=true return true end local af,aP=nil,false local y,D,s,w="menu",1,nil,85 local L={}local ab,bJ=1,nil local function bf()af,aP=nil,false snail.tick(0)end local aN,an local function bQ()aP=true af=coroutine.create(function()return ci(H)end)snail.after(aN)snail.tick(450)end local function aO()local d,R={},{}for a=1,#N do local f=N[a]local b,j=f%128,(f//128)%128 local i if s then if b==s then i=j end else i=b end if i and not R[i]then R[i]=true;d[#d+1]=i end end if s then d[#d+1]=s end return d,R end local ag=false local function aR(i)local o,p=(i-21)%10,(i-21)//10 if ag then return 7-o,7-p end return o,p end local function bx()local d,R=aO()if R[w]or w==s or#d==0 then return end local ay,az=aR(w)local z,Y for a=1,#d do local ad,aS=aR(d[a])local l=(ad-ay)^2+(aS-az)^2 if not Y or l<Y then z,Y=d[a],l end end w=z end local function aC()return not v and not aP and(x==2 or aj==C)end an=function()s=nil ag=x==1 and C==-1 bM()if not v and x==1 and aj~=C then bQ()else bx()end end aN=function()if not af then return end local ce,f=coroutine.resume(af)if coroutine.status(af)~="dead"then return end bf()if not ce or not f then local P=G bb(P,#P)f=N[math.random(#N)]end if not f then return end aJ(f,true)aq()an()end local function a8(aF,aB)bf()x,C,ai=aF,aB,true aK()aq()w=aB==1 and 85 or 35 y="play"an()end local function cj()bf()local c=#G-1 if x==1 then while c>0 and((c%2==0)and 1 or-1)~=C do c=c-1 end end if c<0 then c=0 end local P=G bb(P,c)y="play"an()end local function bE()local d={}if ai then d[#d+1]={"Back to the game","resume"}end d[#d+1]={"New game as White","w"}d[#d+1]={"New game as Black","b"}d[#d+1]={"New game, two players","2"}d[#d+1]={"Computer level: "..aV[H],"lvl"}if ai and#G>0 and not(x==1 and#G==1 and C==-1)then d[#d+1]={"Take back a move","undo"}end return d end local function b8(bv,bw)local d=aO()local ay,az=aR(w)local z,Y for bG=1,2 do for a=1,#d do local ad,aS=aR(d[a])local e=(ad-ay)*bv+(aS-az)*bw local h=math.abs((ad-ay)*bw)+math.abs((aS-az)*bv)if e>0 and(bG==2 or h<=e)then local l=bG==1 and e*2+h or e+h*3 if not Y or l<Y then z,Y=d[a],l end end end if z then break end end if z then w=z end end local function b3(i)s=i L={}for a=1,#N do local f=N[a]if f%128==i then local j=(f//128)%128 L[j]=L[j]or{}table.insert(L[j],f)end end end local function bz(f)s=nil y="play"aJ(f,true)aq()w=(f//128)%128 an()end local aT={up={0,-1},down={0,1},left={-1,0},right={1,0}}function key(m)if y=="menu"then local K=bE()if D>#K then D=#K end if m=="up"then D=(D-2)%#K+1 elseif m=="down"then D=D%#K+1 elseif m=="ok"or((m=="left"or m=="right")and K[D][2]=="lvl")then local I=K[D][2]if I=="resume"then y="play"elseif I=="w"then a8(1,1)elseif I=="b"then a8(1,-1)elseif I=="2"then a8(2,1)elseif I=="lvl"then H=(H+(m=="left"and 1 or 0))%3+1 if ai then bM()end elseif I=="undo"then cj()end end return end if y=="promo"then if m=="top"then y="play"elseif m=="up"then ab=(ab-2)%4+1 elseif m=="down"then ab=ab%4+1 elseif m=="ok"then for bp,f in ipairs(bJ)do if(f//16384)%8==6-ab then bz(f)end end end return end if m=="top"then if s then s=nil else y="menu";D=1 end return end if v then if m=="ok"then y="menu";D=1 end return end if not aC()then return end if m=="ok"then if s then if w==s then s=nil elseif L[w]then local a7=L[w]if#a7>1 then y,ab,bJ="promo",1,a7 else bz(a7[1])end end else local bp,R=aO()if R[w]then b3(w)end end elseif aT[m]then b8(aT[m][1],aT[m][2])end end function touch(ca,I,ao)if ca~="board"or y~="play"or not aC()then return end if I<1 or I>8 or ao<1 or ao>8 then return end local o,p=I-1,ao-1 if ag then o,p=7-o,7-p end local i=21+o+10*p if s and i~=s and not L[i]then for a=1,#N do if N[a]%128==i then key("top");break end end end local bp,R=aO()if(s and(i==s or L[i]))or(not s and R[i])then w=i;key("ok")end end function tick()aN()if af then snail.after(aN)end end local function b2(i,o,p,aI)local e=bi[al[i]]local X=e>0 and e or(e<0 and 6-e or 0)if i==w and aC()and y=="play"then return 26+X end if i==s then return 38+X end if not aI then if s then if L[i]then return X==0 and 51 or 51+X end elseif i==a3 and X>0 then return 51+X elseif i==a2 and X==0 then return 64 end end return((p+o)%2==1 and 13 or 0)+X end local function bs(aI)local d,bY,aY={},{},0 for bd=0,7 do local p=ag and 7-bd or bd for bu=0,7 do local o=ag and 7-bu or bu local a=b2(21+o+10*p,o,p,aI)if not bY[a]then bY[a]=true;aY=aY+1 end d[#d+1]=bo[a]end if bd<7 then d[#d+1]="/"end end if aY>32 and not aI then return bs(true)end return table.concat(d)end local function ch()if v then if x==1 and v:find("Checkmate")then return aj==C and"Checkmate - you lost"or"Checkmate - you won!"end return v end local d if aP then d="Computer is thinking..."elseif x==1 then d="Your move"else d=(aj==1 and"White"or"Black").." to move"end if a0 then d=d.." - check!"end return d end function draw()if y=="menu"then snail.title("Chess")local K=bE()if D>#K then D=#K end for a,b7 in ipairs(K)do snail.row(b7[1],a==D)end snail.gap()if ai then local as=x==2 and"Two players"or("You play "..(C==1 and"White"or"Black")..", "..aV[H])snail.small(as..(v and(". "..v..".")or(", move "..(#G//2+1)..".")))else snail.small("Pick a side to start. The computer level can be changed at any time.")end snail.hint("BACK leave OK choose UP/DN move")return end if y=="promo"then snail.title("Promote pawn")for a,cd in ipairs({"Queen","Rook","Bishop","Knight"})do snail.row(cd,a==ab)end snail.hint("BACK cancel OK choose UP/DN move")return end snail.title(x==1 and("Chess - "..aV[H])or"Chess - two players")snail.status(ch())snail.text(aE~=""and aE or(snail.touchscreen and"Tap a piece, then its square."or"Move with the arrows, OK picks a piece."))snail.board(bs(false),nil,v)if v then snail.hint("BACK options OK new game")elseif not aC()then snail.hint("BACK options")elseif s then snail.hint("BACK cancel OK move 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())aK()aq()if cb()then ag=x==1 and C==-1 if not v then y="play"if x==1 and aj~=C then bQ()else bx()end end end end
