--!name Four in a Row
--!icon game
--!category Games
local aQ="ABCDEFGHIJ"local ax={}for b=1,#aQ do ax[b-1]=aQ:sub(b,b)end local I,as,k,J={},{},{},{}for b=0,50 do I[b]=9;as[b]=0 end for a=0,6 do k[a]=0 J[a]=0 for n=0,5 do I[a*7+n+1]=0 end end local N,aa,ag,af,Q={},{},{},{},{}do local ar={}for b=1,50 do ar[b]={}end local bo={{1,0},{0,1},{1,1},{1,-1}}for a=0,6 do for n=0,5 do for aS,E in ipairs(bo)do local be,aV=a+3*E[1],n+3*E[2]if be<=6 and aV>=0 and aV<=5 then local d=#N+1 N[d]=a*7+n+1 aa[d]=E[1]*7+E[2]for f=0,3 do local e=ar[N[d]+f*aa[d]]e[#e+1]=d end end end end end for b=1,50 do ag[b]=#Q+1 af[b]=#ar[b]for aS,d in ipairs(ar[b])do Q[#Q+1]=d end end end local aL,aM,aN,aO={},{},{},{}local Y,G={[1]=aL,[-1]=aM},{[1]=aN,[-1]=aO}local ay={[0]=0,1,5,25,0}local z,P,L=1,0,0 local aR={}local function a6(c,m)local n=(m-1)%7 if(n+(c==1 and 0 or 1))%2==0 then return 14 end return 4 end local function am(a,v)local c=z local y=k[a]local g=a*7+y+1 I[g]=c k[a]=y+1 aR[v]=P local M,av,aP,_=Y[c],Y[-c],G[c],G[-c]local l,a8=0,false local K=ag[g]for f=K,K+af[g]-1 do local d=Q[f]local i,A=M[d],av[d]if A==0 then l=l+ay[i+1]-ay[i]if i==2 then local m,E=N[d],aa[d]while I[m]~=0 do m=m+E end aP[m]=aP[m]+1 l=l+a6(c,m)elseif i==3 then a8=true end elseif i==0 then l=l+ay[A]if A==3 then _[g]=_[g]-1 l=l+a6(-c,g)end end M[d]=i+1 end P=P+c*l z=-c L=L+1 return a8 end local function aK(a,v)local c=-z z=c local y=k[a]-1 k[a]=y local g=a*7+y+1 local M,av=Y[c],Y[-c]local K=ag[g]for f=K,K+af[g]-1 do local d=Q[f]local i=M[d]-1 M[d]=i local A=av[d]if A==0 then if i==2 then local m,E=N[d],aa[d]while I[m]~=0 do m=m+E end G[c][m]=G[c][m]-1 end elseif i==0 and A==3 then G[-c][g]=G[-c][g]+1 end end I[g]=0 P=aR[v]L=L-1 end local at,ab=1000000,100000 local aw={3,2,4,1,5,0,6}local ao,a_,R,ae,a9=0,0,false,0,280 local aD={}local function bn()ao=ao+1 if ao>a_ then R=true end ae=ae+1 if ae>=a9 then ae=0;coroutine.yield()end end local function a4(U,H,aT,v)bn()if R then return 0 end local c=z local bb,_=G[c],G[-c]local O,aW=0,0 for a=0,6 do local y=k[a]if y<6 then local g=a*7+y+1 if bb[g]>0 then return ab-v end if _[g]>0 then O=O+1;aW=a end end end if L>=42 then return 0 end if O>1 then return-ab+v+1 end if U<=0 and O==0 then return P*c end local bi=O==1 and U or U-1 local aZ=aD[v]or-1 local r=-at for b=0,7 do local a=-1 if O==1 then if b>0 then break end a=aW elseif b==0 then a=aZ elseif aw[b]~=aZ then a=aw[b]end if a>=0 and k[a]<6 then local h if O==0 and _[a*7+k[a]+2]>0 then h=-ab+v+2 else am(a,v)h=-a4(bi,-aT,-H,v+1)aK(a,v)if R then return 0 end end if h>r then r=h if h>H then H=h if h>=aT then aD[v]=a break end end end end end return r end local ba={{1,100,60},{6,1800,8},{42,11000,0}}local au={"Easy","Medium","Hard"}local F={}local function bp(al)local V=ba[al]local w={}for b=1,7 do local a=aw[b]if k[a]<6 then w[#w+1]=a end end local j=#w if j==0 then return nil end for b=1,j do if G[z][w[b]*7+k[w[b]]+1]>0 then return w[b]end end if j==1 then return w[1]end ao,a_,R,ae=0,V[2],false,0 aD={}if al==1 and math.random(10)<=3 then local r,aB for b=1,j do local a=w[b]am(a,0)local h=-P*z+math.random(0,V[3])aK(a,0)if not aB or h>aB then r,aB=a,h end end return r end local r=w[1]for U=1,V[1]do local H,aj,aU=-at,nil,1 for b=1,j do local a=w[b]am(a,0)local h=-a4(U-1,-at,V[3]-H,1)aK(a,0)if R then break end if V[3]>0 then h=h+math.random(0,V[3])end if h>H then H,aj,aU=h,a,b end end if aj then r=aj table.remove(w,aU)table.insert(w,1,aj)end if R or H>ab//2 or H<-ab//2 or U>42-L then break end end return r end local x,az={},{}local X,o,ak,aE=1,nil,"",0 local q,t,u,W,T=1,1,2,false,false local Z={0,0,0,0,0,0,0,0,0}local ac=false local function aI()for a=0,6 do k[a]=0 for n=0,5 do I[a*7+n+1]=0 end end for b=1,50 do aN[b]=0;aO[b]=0 end for d=1,#N do aL[d]=0;aM[d]=0 end z,P,L,ac=1,0,0,false x,az={},{}aE,ak,o=0,"",nil end local function br(c)if q==1 then return c==t and"You"or"Computer"end return c==1 and"Black"or"White"end local function aG(a)local c=z local g=a*7+k[a]+1 ac=am(a,0)aE=g x[#x+1]=a ak=br(c)..": column "..(a+1)if ac then local M=Y[c]local K=ag[g]for f=K,K+af[g]-1 do local d=Q[f]if M[d]==4 then for bg=0,3 do az[N[d]+bg*aa[d]]=true end end end end end local function a2(f)if q~=1 or T then return end T=true local b=(u-1)*3+f Z[b]=Z[b]+1 end local function ad()X=z F={}for a=0,6 do J[a]=k[a]if k[a]<6 then F[#F+1]=a end for n=0,5 do as[a*7+n+1]=I[a*7+n+1]end end o=nil if ac then local d=-z if q==1 then o=d==t and"You win!"or"Computer wins"a2(d==t and 1 or 2)else o=(d==1 and"Black"or"White").." wins"end elseif L>=42 then o="Draw - the board is full"a2(3)end end local function bl()local e={"F1",q,t==1 and"b"or"w",u,T and 1 or 0,table.concat(Z,","),":"}for b=1,#x do e[#e+1]=x[b]end return table.concat(e)end local function a3()snail.save(bl())end local function aH(C,j)aI()for b=1,j do local a=C[b]if ac or not a or a<0 or a>6 or k[a]>=6 then break end aG(a)end ad()end local function bk()local c=snail.load()if type(c)~="string"then return false end local an,aC,al,bd,E,a1=c:match("^F1(%d)([bw])(%d)(%d)([%d,]+):(%d*)$")if not an then return false end local b=0 for h in E:gmatch("%d+")do b=b+1;Z[b]=tonumber(h)end q,t,u=tonumber(an),aC=="b"and 1 or-1,tonumber(al)if u<1 or u>3 then u=2 end local C={}for f=1,#a1 do C[f]=a1:byte(f)-48 end T=true aH(C,#C)T=bd=="1"W=true return true end local S,aq=nil,false local D,p,s="menu",1,3 local function aJ()S,aq=nil,false snail.tick(0)end local ap local function a5()aq=true S=coroutine.create(function()return bp(u)end)snail.after(ap)snail.tick(450)end local function ai()return not o and not aq and(q==2 or X==t)end local function aX()if J[s]<6 or#F==0 then return end local r,aA for b=1,#F do local l=math.abs(F[b]-s)if not aA or l<aA then r,aA=F[b],l end end s=r end local function ah()a3()if not o and q==1 and X~=t then a5()else aX()end end ap=function()if not S then return end local bj,a=coroutine.resume(S)if coroutine.status(S)~="dead"then return end aJ()if not bj or not a or a<0 or a>6 or J[a]>=6 then local C=x aH(C,#C)a=F[math.random(#F)]end if not a then return end aG(a)ad()ah()end local function aF(an,aC)aJ()q,t,W,T=an,aC,true,false aI()ad()s=3 D="play"ah()end local function a7()local j=#x-1 if q==1 then while j>=0 and((j%2==0)and 1 or-1)~=t do j=j-1 end end if j<0 then return nil end return j end local function bq()local j=a7()if not j then return end aJ()local C=x aH(C,j)D="play"ah()end local function a0()local e={}if W then e[#e+1]={"Back to the game","resume"}end e[#e+1]={"New game, you go first","b"}e[#e+1]={"New game, computer first","w"}e[#e+1]={"New game, two players","2"}e[#e+1]={"Computer level: "..au[u],"lvl"}if W and a7()then e[#e+1]={"Take back a move","undo"}end return e end function key(f)if D=="menu"then local B=a0()if p>#B then p=#B end if f=="up"then p=(p-2)%#B+1 elseif f=="down"then p=p%#B+1 elseif f=="ok"or((f=="left"or f=="right")and B[p][2]=="lvl")then local i=B[p][2]if i=="resume"then D="play"elseif i=="b"then aF(1,1)elseif i=="w"then aF(1,-1)elseif i=="2"then aF(2,1)elseif i=="lvl"then u=(u+(f=="left"and 1 or 0))%3+1 if W then a3()end elseif i=="undo"then bq()end end return end if f=="top"then D="menu";p=1;return end if o then if f=="ok"then D="menu";p=2 end return end if not ai()then return end if f=="ok"then if J[s]<6 then aG(s)ad()ah()end elseif f=="left"or f=="right"then local l=f=="left"and-1 or 1 for aS=1,7 do s=(s+l)%7 if J[s]<6 then break end end end end function touch(bh,i,A)if bh~="board"or D~="play"or not ai()or type(i)~="number"or type(A)~="number"then return end local a=math.floor(i)-1 local y=J[a]if not y or y>=6 or A<1 or A>7 then return end s=a key("ok")end function tick()ap()if S then snail.after(ap)end end local function bc()local aY=ai()and D=="play"local e={}for a=0,6 do e[#e+1]=(aY and a==s)and ax[X==1 and 8 or 9]or" "end for n=5,0,-1 do e[#e+1]="/"for a=0,6 do local g=a*7+n+1 local h=as[g]local b=0 if h==0 then if aY and a==s and n==J[a]then b=7 end elseif az[g]then b=h==1 and 5 or 6 elseif g==aE then b=h==1 and 3 or 4 else b=h==1 and 1 or 2 end e[#e+1]=ax[b]end end e[#e+1]="/1234567"return table.concat(e)end local function bm()if o then return"Game over after "..#x.." discs"end if aq then return"Computer is thinking..."end if q==1 then return"Your move - you have the "..(t==1 and"solid"or"ringed").." discs"end return(X==1 and"Black (solid)"or"White (ringed)").." to move"end function draw()if D=="menu"then snail.title("Four in a Row")local B=a0()if p>#B then p=#B end for b,bf in ipairs(B)do snail.row(bf[1],b==p)end snail.gap()if W then local d=q==2 and"Two players"or("You go "..(t==1 and"first"or"second")..", "..au[u])snail.small(d..(o and(". "..o..".")or(", "..#x..(#x==1 and" disc"or" discs").." down.")))else snail.small("Drop discs in turn. Four in a line - across, up or slanted - wins.")end local c=Z snail.small("Won-lost-drawn against the computer:")snail.small("Easy "..c[1].."-"..c[2].."-"..c[3].."   Medium "..c[4].."-"..c[5].."-"..c[6].."   Hard "..c[7].."-"..c[8].."-"..c[9])snail.hint("BACK leave OK choose UP/DN move")return end snail.title(q==1 and("Four in a Row - "..au[u])or"Four in a Row - two players")snail.status(bm())snail.text(ak~=""and ak or(snail.touchscreen and"Tap a column to drop a disc."or"Left and right pick a column, OK drops."))snail.board(bc(),nil,o)if o then snail.hint("BACK options OK new game")elseif not ai()then snail.hint("BACK options")else snail.hint("BACK options OK drop L/R column")end end function start()math.randomseed(os.time())aI()ad()if bk()then if not o then D="play"if q==1 and X~=t then a5()else aX()end end end end
