--!name Gomoku
--!icon game
--!category Games
local bd="000000000000000011313351335355701131335133535570113133513353557000000001131335022424461335355702242446133535570224244613353557000000013353557133535571335355713353557133535571335355713353557000011302241335022424461335355702242446133535570224244613353557000133524463557244635572446355724463557244635572446355724463557000133513351335133524461336355713352446133635571335244613363557000355735573557355735573557355735573557355735573557355735573557001021302241335022424461335355702242446133535570224244613353557013243524463557244635572446355724463557244635572446355724463557013243524352435243524462436355724352446243635572435244624363557035465746574657465746574657465746574657465746574657465746574657013131313241335132424461335355713242446133535571324244613353557035353535463657354636573546365735463657354636573546365735463657035353535353535353535463536365735353546353636573535354635363657057575757575757575757575757575757575757575757575757575757575757001021302241335022424461335355702242446133535570224244613353557123243524463557244635572446355724463557244635572446355724463557123243524352435243524462436355724352446243635572435244624363557345465746574657465746574657465746574657465746574657465746574657123232323242335232424462335355723242446233535572324244623353557345454545464657454646574546465745464657454646574546465745464657345454545454545454545464546465745454546454646574545454645464657567676767676767676767676767676767676767676767676767676767676767111121312241335122424461335355712242446133535571224244613353557333343534463557344635573446355734463557344635573446355734463557333343534353435343534463436355734353446343635573435344634363557555566756675667566756675667566756675667566756675667566756675667333333333343335333434463335355733343446333535573334344633353557555555555565667555656675556566755565667555656675556566755565667555555555555555555555565556566755555556555656675555555655565667777777777777777777777777777777777777777777777777777777777777777001021302241335022424461335355702242446133535570224244613353557123243524463557244635572446355724463557244635572446355724463557123243524352435243524462436355724352446243635572435244624363557345465746574657465746574657465746574657465746574657465746574657123232323242335232424462335355723242446233535572324244623353557345454545464657454646574546465745464657454646574546465745464657345454545454545454545464546465745454546454646574545454645464657567676767676767676767676767676767676767676767676767676767676767111121312241335122424461335355712242446133535571224244613353557333343534463557344635573446355734463557344635573446355734463557333343534353435343534463436355734353446343635573435344634363557555566756675667566756675667566756675667566756675667566756675667333333333343335333434463335355733343446333535573334344633353557555555555565667555656675556566755565667555656675556566755565667555555555555555555555565556566755555556555656675555555655565667777777777777777777777777777777777777777777777777777777777777777001021302241335022424461335355702242446133535570224244613353557123243524463557244635572446355724463557244635572446355724463557123243524352435243524462436355724352446243635572435244624363557345465746574657465746574657465746574657465746574657465746574657123232323242335232424462335355723242446233535572324244623353557345454545464657454646574546465745464657454646574546465745464657345454545454545454545464546465745454546454646574545454645464657567676767676767676767676767676767676767676767676767676767676767111121312241335122424461335355712242446133535571224244613353557333343534463557344635573446355734463557344635573446355734463557333343534353435343534463436355734353446343635573435344634363557555566756675667566756675667566756675667566756675667566756675667333333333343335333434463335355733343446333535573334344633353557555555555565667555656675556566755565667555656675556566755565667555555555555555555555565556566755555556555656675555555655565667777777777777777777777777777777777777777777777777777777777777777"local bh=string.byte local x,U=18,1<<24 local j=table.pack(("\9"):rep(343):byte(1,-1))local l=table.pack(("\0"):rep(343):byte(1,-1))local m={}j[0],l[0]=9,0 for n=0,14 do for c=0,14 do local a=(n+2)*x+c+2 j[a]=0 m[#m+1]=a end end local ac={1,x,x+1,x-1}local ae={}for aV=-2,2 do for aU=-2,2 do if aV~=0 or aU~=0 then ae[#ae+1]=aV*x+aU end end end local ax,u={},0 local function ah(a,g,c)local h,k,f=0,1,a-g for S=1,5 do local i=j[f]if i==c then h=h+k elseif i~=0 then break end k=k+k f=f-g end local y,E=0,1 f=a+g for S=1,5 do local i=j[f]if i==c then y=y+E elseif i~=0 then break end E=E+E f=f+g end return bh(bd,(k-1+h)*63+E+y)-48 end local function ak(a)local w=0 for b=1,4 do local g=ac[b]w=w+(ah(a,g,1)<<(b*3-3))+(ah(a,g,2)<<(b*3+9))end l[a]=(l[a]&~(U-1))|w end local function aL(a,bs)for b=1,4 do local g=ac[b]local J=b*3-3 local R=J+12 local bp=~((7<<J)|(7<<R))for aE=-g,g,g+g do local f=a for S=1,5 do f=f+aE local i=j[f]if i==9 then break end if i==0 then local w=l[f]local a_=(w&bp)|(ah(f,g,1)<<J)|(ah(f,g,2)<<R)if a_~=w then if bs then u=u+1 ax[u]=f+(w<<9)end l[f]=a_ end end end end end end local function aJ(a,c)j[a]=c for b=1,24 do local f=a+ae[b]l[f]=l[f]+U end end local function aX(a)j[a]=0 for b=1,24 do local f=a+ae[b]l[f]=l[f]-U end end local function am(a,c)j[a]=c aL(a,true)aJ(a,c)end local function at(a,aa)aX(a)while u>aa do local z=ax[u]l[z&511]=z>>9 u=u-1 end end local function as(A)local E=A&7 for S=1,3 do A=A>>3 if(A&7)>E then E=A&7 end end return E end local bf={[0]=0,4,20,30,200,260,5000,100000}local function aQ(A)if A==0 then return 0 end local s,X,an=0,0,0 for S=1,4 do local e=A&7 A=A>>3 if e==7 then return 100000 end if e==6 then X=X+2 elseif e==5 then X=X+1 elseif e==4 then an=an+1 end s=s+bf[e]end if X>=2 then return 10000 end if X==1 and an>=1 then return 9000 end if an>=2 then return 4000 end return s end local ao,aY,V,ab,bc=0,0,false,0,110 local function Z(I)ao=ao+I if ao>aY then V=true end ab=ab+I if ab>=bc then ab=0;coroutine.yield()end end local function aM(c,e,t,G)local J,R=c*12-12,12-(c*12-12)local T,Q,P,ap,k=-1,nil,-1,nil,0 for b=1,225 do local a=m[b]local w=l[a]if w>=U and j[a]==0 then local h,g=aQ((w>>J)&4095),aQ((w>>R)&4095)k=k+1 if h>T then T,Q=h,a end if g>P then P,ap=g,a end if e>0 then local i if G==1 then i=h*10+math.min(g,260)*5+math.random(0,1500)else i=h*10+g*9+(G==2 and math.random(0,60)or G==3 and math.random(0,3)or 0)end local z=(i<<9)|a local v=#t if v<e or z>t[v]then if v==e then v=v-1 end while v>0 and t[v]<z do t[v+1]=t[v];v=v-1 end t[v+1]=z end end end end Z(2+k//6)return T,Q,P,ap,k end local function aW(a,c)for b=1,4 do local g=ac[b]local bx=(c-1)*12+(b-1)*3 for aE=-g,g,g+g do local f=a for S=1,4 do f=f+aE local i=j[f]if i==9 or(i~=0 and i~=c)then break end if i==0 and((l[f]>>bx)&7)==7 then return f end end end end return nil end local function a9(c,aj)Z(2)if V then return nil end local J,R=c*12-12,12-(c*12-12)local ad,k,a2={},0,false for b=1,225 do local a=m[b]if j[a]==0 and l[a]>=U then local w=l[a]local A=(w>>J)&4095 if A~=0 then local E=as(A)if E==7 then return a end if E>=5 then k=k+1 ad[k]=a end end if as((w>>R)&4095)==7 then a2=true end end end if a2 or k==0 then return nil end for b=1,k do if aQ((l[ad[b]]>>J)&4095)>=10000 then return ad[b]end end if aj<=0 then return nil end for b=1,k do local a=ad[b]local aa=u am(a,c)Z(9)local n,bb=aW(a,c),nil if n then local az=u am(n,3-c)Z(9)bb=a9(c,aj-1)at(n,az)end at(a,aa)if V then return nil end if bb then return a end end return nil end local function aP(c,aj,bg)ao,aY,V=0,bg,false local n=a9(c,aj)if V then n=nil end V=false return n end local av={"Easy","Medium","Hard"}local function bl(c,t,h,Q)local aI=3-c if h>=10000 then return Q end local I=aP(c,7,600)if I then return I end if h>=9000 then return Q end local aT,aB=t[1]&511,nil for e=1,#t do local a,ay=t[e]&511,t[e]>>9 local aa=u am(a,c)Z(9)local i=ay-10000000 if aW(a,c)then i=ay else local aw={}local a0=aM(aI,3,aw,0)if a0<4000 and(a0<260 or not aP(aI,5,100))then local au for v=1,#aw do local n=aw[v]&511 local az=u am(n,aI)Z(9)local T,S,bq=aM(c,0)local z=T*2-bq if T>=9000 or(T>=260 and aP(c,5,60))then z=1000000 end at(n,az)if not au or z<au then au=z end end i=ay+(au or 0)end end at(a,aa)if not aB or i>aB then aT,aB=a,i end end return aT end local function bA(G,c)if j[m[113]]==0 and l[m[113]]<U then local aS=false for b=1,225 do if j[m[b]]~=0 then aS=true;break end end if not aS then return m[113]end end ab,u,ax=0,0,{}local t={}local h,Q,g,ap,k=aM(c,G==3 and 10 or 1,t,G)if k==0 then for b=1,225 do if j[m[b]]==0 then return m[b]end end return nil end if h>=100000 and(G>1 or math.random(10)<=9)then return Q end if g>=100000 and(G>1 or math.random(4)<=3)then return ap end if G==3 then return bl(c,t,h,Q)end return t[1]&511 end local p,af={},{}local K,o,O,C=1,nil,"",0 local q,r,D,Y,W=1,1,2,false,false local _={0,0,0,0,0,0,0,0,0}local B=0 local function aC()for b=0,342 do l[b]=0 end for b=1,225 do j[m[b]]=0 end p,af={},{}K,C,O,o,u,B=1,0,"",nil,0,0 end local function ba(c)if q==1 then return c==r and"You"or"Computer"end return c==1 and"Black"or"White"end local function ai(c)return c==1 and"Black"or"White"end local function bk(a)local c=j[a]for b=1,4 do local g=ac[b]local h,y=a,a while j[h-g]==c do h=h-g end while j[y+g]==c do y=y+g end if(y-h)//g>=4 then for f=h,y,g do af[f]=true end return true end end return false end local be={[4]="an open three",[5]="a four",[6]="an open four",[7]="five in a row"}local function aK(a)local c=K local J,R=c*12-12,12-(c*12-12)local a3,a1=as((l[a]>>J)&4095),as((l[a]>>R)&4095)aJ(a,c)if B==0 then aL(a,false)end p[#p+1]=a C=a local d={}local bm=q==1 and(c==r and"the computer's"or"your")or(c==1 and"White's"or"Black's")if a1>=6 then d[1]="blocks "..bm..(a1==7 and" four"or" three")end if a3>=4 then d[#d+1]=(q==1 and c==r and"make "or"makes ")..be[a3]end if#d==0 then O=ba(c)..(q==1 and c==r and" played."or" played the marked stone.")else if d[1]:sub(1,1)=="b"and q==1 and c==r then d[1]="block"..d[1]:sub(7)end O=ba(c).." "..table.concat(d," and ").."."end K=3-c end local function a4(e)if q~=1 or W then return end W=true local b=(D-1)*3+e _[b]=_[b]+1 end local function aN()o=nil if C>0 and bk(C)then local I=j[C]if q==1 then o=I==r and"You win!"or"Computer wins"a4(I==r and 1 or 2)else o=ai(I).." wins"end elseif#p>=225 then o="Draw - the board is full"a4(3)end end local function bw()local d={"G1",q,r==1 and"b"or"w",D,W and 1 or 0,table.concat(_,","),":"}for b=1,#p do local a=p[b]d[#d+1]=string.char(95+a%x,95+a//x)end d[#d+1]="|"..O return table.concat(d)end local function a5()snail.save(bw())end local function bu()local s=snail.load()if type(s)~="string"then return false end local P,aF,G,bi,by,aG,a7=s:match("^G1(%d)([bw])(%d)(%d)([%d,]+):([a-q]*)|?(.*)$")if not P then return false end local b=0 for i in by:gmatch("%d+")do b=b+1;_[b]=tonumber(i)end q,r,D=tonumber(P),aF=="b"and 1 or 2,tonumber(G)if D<1 or D>3 then D=2 end aC()B=1 for e=1,#aG-1,2 do local a=(aG:byte(e+1)-95)*x+aG:byte(e)-95 if a>342 or j[a]~=0 or o then break end aK(a)W=true aN()end W=bi=="1"if a7~=""then O=a7:sub(1,95)end Y=true return true end local N,ar,aD=nil,false,nil local H,F,L="menu",1,m[113]local aA local function bv()for b=0,342 do l[b]=0 end for b=1,225 do j[m[b]]=0 end for b=1,#p do aJ(p[b],2-b%2)end for b=1,225 do if j[m[b]]==0 then ak(m[b])end end u=0 end local function aO()if N and coroutine.status(N)~="dead"then bv()end N,ar,aD=nil,false,nil snail.tick(0)end local aq local function a6()ar=true aD=aA()local c=K N=coroutine.create(function()return bA(D,c)end)snail.after(aq)snail.tick(450)end local function al()return not o and not ar and B==0 and(q==2 or K==r)end local function ag()a5()if C>0 then L=C end if not o and q==1 and K~=r then a6()end end aq=function()if not N then return end local br,a=coroutine.resume(N)if coroutine.status(N)~="dead"then return end aO()u=0 if not br or not a or j[a]~=0 then for b=1,225 do if j[m[b]]==0 then ak(m[b])end end a=nil for b=1,225 do local f=m[b]if j[f]==0 and(not a or l[f]>l[a])then a=f end end end if not a then return end aK(a)aN()ag()end local function bt()local z=math.min(225,B+79)for b=B,z do local a=m[b]if j[a]==0 then ak(a)end end B=z+1 if B>225 then B=0 snail.tick(0)if H=="play"and not o and q==1 and K~=r then a6()end end end local function aH(P,aF)aO()q,r,Y,W=P,aF,true,false aC()L=m[113]H="play"ag()end local function a8()local k=#p if k==0 or B>0 then return 0 end if q==2 then return 1 end local e=1 if j[p[k]]~=r then e=2 end if e>k then return 0 end return e end local function bC()local e=a8()if e==0 then return end aO()for S=1,e do local a=table.remove(p)aX(a)ak(a)aL(a,false)end af,o={},nil K=#p%2==0 and 1 or 2 C=p[#p]or 0 O="Took back "..(e==1 and"a move."or"two moves.")H="play"ag()end local function aZ()local d={}if Y then d[#d+1]={"Back to the game","resume"}end d[#d+1]={"New game as Black (first)","b"}d[#d+1]={"New game as White","w"}d[#d+1]={"New game, two players","2"}d[#d+1]={"Computer level: "..av[D],"lvl"}if Y and a8()>0 then d[#d+1]={"Take back a move","undo"}end return d end local aR={up=-x,down=x,left=-1,right=1}function key(e)if H=="menu"then local M=aZ()if F>#M then F=#M end local h=M[F][2]if e=="up"then F=(F-2)%#M+1 elseif e=="down"then F=F%#M+1 elseif e=="ok"or((e=="left"or e=="right")and h=="lvl")then if h=="resume"then H="play"elseif h=="b"then aH(1,1)elseif h=="w"then aH(1,2)elseif h=="2"then aH(2,1)elseif h=="lvl"then D=(D+(e=="left"and 1 or 0))%3+1 if Y then a5()end elseif h=="undo"then bC()end end return end if e=="top"then H="menu";F=1;return end if o then if e=="ok"then H="menu";F=2 end return end if not al()then return end if e=="ok"then if j[L]==0 then aK(L)aN()ag()end elseif aR[e]then local f=L+aR[e]if j[f]~=9 then L=f end end end function touch(bo,h,y)if bo~="board"or H~="play"or not al()then return end if h<1 or h>15 or y<1 or y>15 then return end local a=(y+1)*x+h+1 if j[a]==0 then L=a;key("ok")end end function tick()if B>0 then bt();return end aq()if N then snail.after(aq)end end local function bj(a)local n,c=a//x-2,a%x-2 if(n==3 or n==7 or n==11)and(c==3 or c==7 or c==11)and(n==7)==(c==7)then return"J"end return string.char(65+(n==0 and 0 or(n==14 and 6 or 3))+(c==0 and 0 or(c==14 and 2 or 1)))end local function bB(a)local i=j[a]if a==L and H=="play"and al()then if i==0 then return K==1 and"Q"or"R"end if a==C then return i==1 and"U"or"V"end return i==1 and"S"or"T"end if i==0 then return bj(a)end if af[a]then return i==1 and"O"or"P"end if a==C then return i==1 and"M"or"N"end return i==1 and"K"or"L"end aA=function()local d={}for b=1,225 do d[#d+1]=bB(m[b])if b%15==0 and b<225 then d[#d+1]="/"end end return table.concat(d)end local function bz()local d="Move "..(#p+(o and 0 or 1)).." - "if B>0 then return"Setting out the stones..."end if o then return d.."game over"end if ar then return d.."computer thinking..."end if q==1 then return d.."your turn ("..ai(r)..")"end return d..ai(K).." to play"end function draw()if H=="menu"then snail.title("Gomoku")local M=aZ()if F>#M then F=#M end for b,bn in ipairs(M)do snail.row(bn[1],b==F)end snail.gap()if Y then local I=q==2 and"Two players"or("You play "..ai(r)..", "..av[D])snail.small(I..(o and(". "..o..".")or(", "..#p..(#p==1 and" stone"or" stones").." down.")))else snail.small("Take turns placing stones. Five or more in a row - across, down or slanting - wins.")end local s=_ snail.small("Won-lost-drawn against the computer:")snail.small("Easy "..s[1].."-"..s[2].."-"..s[3].."   Medium "..s[4].."-"..s[5].."-"..s[6].."   Hard "..s[7].."-"..s[8].."-"..s[9])snail.hint("BACK leave OK choose UP/DN move")return end snail.title(q==1 and("Gomoku - "..av[D])or"Gomoku - two players")snail.status(bz())snail.text(O~=""and O or(snail.touchscreen and"Tap a point to place a stone."or"Move the cursor with the arrows, OK places a stone."))snail.board(aD or aA(),nil,o)if o then snail.hint("BACK options OK new game")elseif not al()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())aC()if bu()then if C>0 then L=C end if not o then H="play"end if B>0 then snail.tick(450)end end end
