--!name Mancala
--!icon game
--!category Games
local aZ,aW,aY="ABCDEFGHIJK","LMNOPQRSTUV","abcdefghijk"local f,l={},{}for a=1,14*24 do f[a]=0 end for a=0,13 do l[a]=0 end local aE,a_=0,0 local function aA(j,d,a)local h=j+14 for b=0,13 do f[h+b]=f[j+b]end local e=f[h+a]f[h+a]=0 local aS,ao=13,6 if d==1 then aS,ao=6,13 end local n=a while e>0 do n=n+1 if n==14 then n=0 end if n~=aS then f[h+n]=f[h+n]+1 e=e-1 end end local q,I=1-d,0 if n==ao then q=d elseif f[h+n]==1 and(d==0 and n<6 or d==1 and n>6)then local aN=h+12-n local m=f[aN]if m>0 then I=m+1 f[h+ao]=f[h+ao]+I f[h+n]=0 f[aN]=0 end end local g,p=0,0 for b=h,h+5 do g=g+f[b]end for b=h+7,h+12 do p=p+f[b]end if g==0 or p==0 then aE,a_=g,p f[h+6]=f[h+6]+g f[h+13]=f[h+13]+p for b=0,5 do f[h+b]=0;f[h+7+b]=0 end return-1,I,n end return q,I,n end local Y,_=1000000,100000 local ai,aK,M,W,aV=0,0,false,0,330 local function bc()ai=ai+1 if ai>aK then M=true end W=W+1 if W>=aV then W=0;coroutine.yield()end end local function a2(j,d)local g,p=0,0 for b=j,j+5 do g=g+f[b]end for b=j+7,j+12 do p=p+f[b]end local i=(f[j+6]-f[j+13])*4+g-p if d==1 then i=-i end return i end local function aH(j,d)local x=f[j+6]-f[j+13]if d==1 then x=-x end if x>0 then return _+x elseif x<0 then return-_+x end return 0 end local function am(j,d,P,w,as)bc()if M then return 0 end if P<=0 then return a2(j,d)end local s,U,h=-Y,d*7,j+14 for a=U+5,U,-1 do if f[j+a]>0 then local q=aA(j,d,a)local i if q<0 then i=aH(h,d)elseif q==d then i=am(h,d,P-1,w,as)else i=-am(h,q,P-1,-as,-w)end if M then return 0 end if i>s then s=i if i>w then w=i if i>=as then break end end end end end return s end local aX={{1,100,10},{4,2500,3},{18,5000,0}}local ar={"Easy","Medium","Hard"}local o={}local function bd(ag,d)local aB=aX[ag]local F={}for a=1,#o do F[a]=o[a]end local e=#F if e==0 then return nil end if e==1 then return F[1]end if ag==1 and math.random(10)<=3 then return F[math.random(e)]end for b=0,13 do f[1+b]=l[b]end ai,aK,M,W=0,aB[2],false,0 local s,aj=F[1],aB[3]for P=1,aB[1]do local w,ae,aF=-Y,nil,1 for a=1,e do local E=F[a]local q=aA(1,d,E)local i if q<0 then i=aH(15,d)elseif q==d then i=am(15,d,P-1,w-aj,Y)else i=-am(15,q,P-1,-Y,aj-w)end if M then break end if aj>0 then i=i+math.random(0,aj)end if i>w then w,ae,aF=i,E,a end end if ae then s=ae table.remove(F,aF)table.insert(F,1,ae)end if M or w>_//2 or w<-_//2 then break end end return s end local v,L,Z,G={},{},{},{}local A,r,Q,aD=0,nil,"",-1 local k,J,t,C,ac,S,O=1,0,2,4,4,false,false local T={0,0,0,0,0,0,0,0,0}local ak,aJ=false,-1 local function aq(d)if k==1 then return d==0 and"You"or"Computer"end return d==0 and"Player 1"or"Player 2"end local function ax()for a=0,13 do l[a]=(a==6 or a==13)and 0 or ac end A=k==1 and J or 0 v,L,Z,G={},{},{},{}Q,r,aD,ak,aJ="",nil,-1,false,-1 end local function a6(a)return a<6 and a+1 or a-6 end local function av(a)local d=A for b=0,13 do f[1+b]=l[b]end local e=l[a]local q,I,n=aA(1,d,a)for b=0,13 do l[b]=f[15+b]end v[#v+1]=a L[#L+1]=d aJ=n if aD~=d then G,Z,aD={},{},d end Z[a]=true local c="pit "..a6(a)if q==d then c=c.." - extra turn"elseif I>0 then c=c.." - captured "..I end G[#G+1]=c local V=aq(d).." sowed "..table.concat(G,", then ")local b=1 while#V>95 and b<#G do b=b+1 V=aq(d).." sowed ... "..table.concat(G,", then ",b)end Q=V if q<0 then ak=true local aG=aE>0 and 1 or 0 local R=k==2 and aq(aG).."'s"or(aG==0 and"Your"or"Computer's")Q=Q:sub(1,70)..". "..R.." row is empty"else A=q end return e end local function aQ(b)if k~=1 or O then return end O=true local a=(t-1)*3+b T[a]=T[a]+1 end local function K()o={}r=nil if ak then local g,p=l[6],l[13]local ay=math.max(g,p).."-"..math.min(g,p)if g==p then r="Draw "..ay aQ(3)elseif k==1 then r=(g>p and"You win "or"Computer wins ")..ay aQ(g>p and 1 or 2)else r=(g>p and"Player 1"or"Player 2").." wins "..ay end return end local U=A*7 for a=U,U+5 do if l[a]>0 then o[#o+1]=a end end end local function a9()local c={"K1",k,J,t,ac,O and 1 or 0,table.concat(T,","),":"}for a=1,#v do c[#c+1]=v[a]%7 end return table.concat(c)end local function aR()snail.save(a9())end local function aw(D,e)ax()K()for a=1,e do local al=D[a]if ak or not al or al<0 or al>5 then break end local aO=A*7+al if l[aO]==0 then break end av(aO)K()end K()end local function a7()local d=snail.load()if type(d)~="string"then return false end local ah,at,ag,a8,a1,ba,aM=d:match("^K1(%d)(%d)(%d)(%d)(%d)([%d,]+):(%d*)$")if not ah then return false end local a=0 for i in ba:gmatch("%d+")do a=a+1;T[a]=tonumber(i)end k,J,t,C=tonumber(ah),tonumber(at),tonumber(ag),tonumber(a8)if k<1 or k>2 then k=1 end if J>1 then J=0 end if t<1 or t>3 then t=2 end if C<3 or C>6 then C=4 end ac=C local D={}for b=1,#aM do D[b]=aM:byte(b)-48 end O=true aw(D,#D)O=a1=="1"S=true return true end local N,ap=nil,false local z,u,y="menu",1,2 local function aC()N,ap=nil,false snail.tick(0)end local an local function aT()ap=true local d=A N=coroutine.create(function()return bd(t,d)end)snail.after(an)snail.tick(450)end local function ad()return not r and not ap and(k==2 or A==0)end local function ab(a)return a<6 and a+1 or 13-a end local function af(a)for b=1,#o do if o[b]==a then return true end end return false end local function aI()if af(y)or#o==0 then return end local m,s,H=ab(y),nil,nil for b=1,#o do local x=math.abs(ab(o[b])-m)if not H or x<H then s,H=o[b],x end end y=s end local function aa()aR()if not r and k==1 and A==1 then aT()else aI()end end an=function()if not N then return end local a5,E=coroutine.resume(N)if coroutine.status(N)~="dead"then return end aC()if not a5 or not E or not af(E)then aw(v,#v)E=o[math.random(#o)]end if not E then return end av(E)K()aa()end local function au(ah,at)aC()k,J,ac,S,O=ah,at,C,true,false ax()K()y=2 z="play"aa()end local function aU()local e=#v if k==1 then while e>0 and L[e]~=0 do e=e-1 end end if e==0 then return nil end local d=L[e]while e>1 and L[e-1]==d do e=e-1 end return e-1 end local function be()local e=aU()if not e then return end aC()local D={}for a=1,e do D[a]=v[a]%7 end aw(D,e)z="play"aa()end local function aL()local c={}if S then c[#c+1]={"Back to the game","resume"}end c[#c+1]={"New game, you go first","f"}c[#c+1]={"New game, computer first","c"}c[#c+1]={"New game, two players","2"}c[#c+1]={"Computer level: "..ar[t],"lvl"}c[#c+1]={"Seeds per pit: "..C,"seeds"}if S and aU()then c[#c+1]={"Take back a move","undo"}end return c end function key(b)if z=="menu"then local B=aL()if u>#B then u=#B end local g=B[u][2]if b=="up"then u=(u-2)%#B+1 elseif b=="down"then u=u%#B+1 elseif b=="ok"or((b=="left"or b=="right")and(g=="lvl"or g=="seeds"))then if g=="resume"then z="play"elseif g=="f"then au(1,0)elseif g=="c"then au(1,1)elseif g=="2"then au(2,0)elseif g=="lvl"then t=(t+(b=="left"and 1 or 0))%3+1 if S then aR()end elseif g=="seeds"then C=(C-3+(b=="left"and 3 or 1))%4+3 elseif g=="undo"then be()end end return end if b=="top"then z="menu";u=1;return end if r then if b=="ok"then z="menu";u=2 end return end if not ad()then return end if b=="ok"then if af(y)then av(y)K()aa()end elseif b=="left"or b=="right"then local x=b=="left"and-1 or 1 local m=ab(y)local s,H for a=1,#o do local X=(ab(o[a])-m)*x if X<=0 then X=X+6 end if not H or X<H then s,H=o[a],X end end if s then y=s end end end function touch(a4,g,j)if a4~="board"or z~="play"or not ad()or type(g)~="number"or type(j)~="number"then return end local m,R=math.floor(g),math.floor(j)if m<2 or m>7 then return end local a=(R==1 or R==2)and 14-m or(R==4 or R==5)and m-2 if not af(a)then return end y=a key("ok")end function tick()an()if N then snail.after(an)end end local function aP(a)local e=l[a]if e>10 then e=10 end local az=aZ if a==y and ad()and z=="play"then az=aW elseif Z[a]then az=aY end return az:sub(e+1,e+1)end local function a0()local c={" "}for m=1,6 do c[#c+1]="{"..l[13-m].."}"end c[#c+1]=" /W"for m=1,6 do c[#c+1]=aP(13-m)end c[#c+1]="W/{"..l[13].."}YYYYYY{"..l[6].."}/X"for m=1,6 do c[#c+1]=aP(m-1)end c[#c+1]="X/ "for m=1,6 do c[#c+1]="{"..l[m-1].."}"end c[#c+1]=" "return table.concat(c)end local function bb()local c if k==1 then c="You "..l[6].."  Computer "..l[13].." - "else c="P1 "..l[6].."  P2 "..l[13].." - "end if r then return c.."game over"end if ap then return c.."thinking..."end if k==1 then return c.."your move"end return c..aq(A).." to move"end function draw()if z=="menu"then snail.title("Mancala")local B=aL()if u>#B then u=#B end for a,a3 in ipairs(B)do snail.row(a3[1],a==u)end snail.gap()if S then local V=k==2 and"Two players"or("You go "..(J==0 and"first"or"second")..", "..ar[t])snail.small(V..(r and(". "..r..".")or(", "..#v..(#v==1 and" move"or" moves").." made.")))else snail.small("Sow seeds anticlockwise. Last seed in your store: go again; in an empty pit of yours: capture.")end local d=T snail.small("Won-lost-drawn against the computer:")snail.small("Easy "..d[1].."-"..d[2].."-"..d[3].."   Medium "..d[4].."-"..d[5].."-"..d[6].."   Hard "..d[7].."-"..d[8].."-"..d[9])snail.hint("BACK leave OK choose UP/DN move")return end snail.title(k==1 and("Mancala - "..ar[t])or"Mancala - two players")snail.status(bb())snail.text(Q~=""and Q or(snail.touchscreen and"Tap a pit to sow it."or"Left and right pick a pit, OK sows it."))snail.board(a0(),nil,r)if k==1 then snail.small("Top row and left store: Computer. Bottom row and right store: you.")else snail.small("Player 1: bottom row, right store. Player 2: top row, left store.")end if r then snail.hint("BACK options OK new game")elseif not ad()then snail.hint("BACK options")else snail.hint("BACK options OK sow L/R pit")end end function start()math.randomseed(os.time())ax()K()if a7()then if not r then z="play"if k==1 and A==1 then aT()else aI()end end end end
