--!name Sea Battle
--!icon game
--!category Games
local p={5,4,3,3,2}local W={"Carrier","Battleship","Cruiser","Submarine","Destroyer"}local ai={"Easy","Normal","Hard"}local ar=1100 local aN,aM,be="w","x","h"local at,aq,bd="LRTBHV","acdefg","mnpqsu"local aK,ba,a9,bg,bf="W","X","K","M","N"local bc,bh="12134","456"local q={{},{}}local B={{},{}}local y={{},{}}local m,h=2,"none"local G,H,A,P,V=4,4,0,0,0 local o=nil local x,C,I="menu",1,1 local u,O,E,F,aE=1,1,0,0,1 local i={p={0,0,0},w={0,0,0},best={0,0,0},shots=0,hits=0}local function s(d,e)return e*10+d+1 end local aL={{1,0},{0,1},{-1,0},{0,-1}}local function R(f)local n=B[f]for a=1,100 do n[a]=0 end for b,g in ipairs(q[f])do for v=0,g.n-1 do n[s(g.x+v*(1-g.d),g.y+v*g.d)]=b end end end local function aV(f,d,e,j,k)local ao,ap=d+(k-1)*(1-j),e+(k-1)*j if d<0 or e<0 or ao>9 or ap>9 then return false end local n=B[f]for aJ=math.max(0,e-1),math.min(9,ap+1)do for aI=math.max(0,d-1),math.min(9,ao+1)do if n[s(aI,aJ)]~=0 then return false end end end return true end local function aO(f,d,e,j,k)local c=q[f]c[#c+1]={x=d,y=e,d=j,n=k,h=0}R(f)end local function af(f)repeat q[f]={}R(f)local ad=true for b=1,5 do local k,a2=p[b],false for au=1,200 do local j=math.random(0,1)local d,e=math.random(0,9-(k-1)*(1-j)),math.random(0,9-(k-1)*j)if aV(f,d,e,j,k)then aO(f,d,e,j,k);a2=true;break end end if not a2 then ad=false;break end end until ad end local function Q(f,b)local g=q[f][b];return g.h>=g.n end local function X(f)local k=0 for b=1,5 do if not Q(f,b)then k=k+1 end end return k end local function aU(f,a)local b=B[f][a]if b==0 then y[f][a]=1;return"miss"end y[f][a]=2 local g=q[f][b]g.h=g.h+1 return g.h>=g.n and"sunk"or"hit",b end local U,as,T={},{},{}local function br()local n,g=B[1],y[1]for a=1,100 do U[a]=g[a]~=0;as[a]=false end for b=1,5 do if Q(1,b)then local D=q[1][b]local ao,ap=D.x+(D.n-1)*(1-D.d),D.y+(D.n-1)*D.d for aJ=math.max(0,D.y-1),math.min(9,ap+1)do for aI=math.max(0,D.x-1),math.min(9,ao+1)do U[s(aI,aJ)]=true end end end end local t={}for a=1,100 do if g[a]==2 and not Q(1,n[a])then t[#t+1]=a;as[a]=true end end return t end local function ae(aA)if#aA==0 then return nil end return aA[math.random(#aA)]end local function aH(aR)local c={}for a=1,100 do if y[1][a]==0 and(aR==nil or aR(a))then c[#c+1]=a end end return c end local function ab(d,e)return d>=0 and d<=9 and e>=0 and e<=9 and not U[s(d,e)]end local function aP(t)local c={}if#t>=2 then local r,z=t[1]-1,t[#t]-1 local Y,Z,_,aa=r%10,r//10,z%10,z//10 if Z==aa then if ab(Y-1,Z)then c[#c+1]=s(Y-1,Z)end if ab(_+1,aa)then c[#c+1]=s(_+1,aa)end elseif Y==_ then if ab(Y,Z-1)then c[#c+1]=s(Y,Z-1)end if ab(_,aa+1)then c[#c+1]=s(_,aa+1)end end end if#c==0 then for au,a in ipairs(t)do local d,e=(a-1)%10,(a-1)//10 for au,l in ipairs(aL)do if ab(d+l[1],e+l[2])then c[#c+1]=s(d+l[1],e+l[2])end end end end if#c>0 then return ae(c)end local aB=9 for b=1,5 do if not Q(1,b)and p[b]<aB then aB=p[b]end end return ae(aH(function(a)local d,e=(a-1)%10,(a-1)//10 return not U[a]and(d+e)%aB==0 end))or ae(aH(function(a)return not U[a]end))end local function bj(t)local g=y[1]for a=1,100 do T[a]=0 end local bx=#t>0 local aS={}for b=1,5 do local k=p[b]if not Q(1,b)and not aS[k]then local aC=0 for aY=b,5 do if p[aY]==k and not Q(1,aY)then aC=aC+1 end end aS[k]=true for j=0,1 do local an=j==1 and 10 or 1 for e=0,9-(k-1)*j do for d=0,9-(k-1)*(1-j)do local am=e*10+d+1 local ad,ak=true,0 for a=am,am+(k-1)*an,an do if as[a]then ak=ak+1 elseif U[a]then ad=false;break end end if ad and(ak>0 or not bx)then local ah=(1+ak*12)*aC for a=am,am+(k-1)*an,an do if g[a]==0 then T[a]=T[a]+ah end end end end end end end end local aj,c=0,{}for a=1,100 do if T[a]>aj then aj,c=T[a],{a}elseif T[a]==aj and aj>0 then c[#c+1]=a end end return ae(c)or aP(t)end local function bi()local t=br()if m==1 then return ae(aH())end if m==2 then return aP(t)end return bj(t)end local function a7(f)local c={}for b,g in ipairs(q[f])do c[b]=g.x..g.y..g.d end return table.concat(c)end local function a8(f)return table.concat(y[f])end local function S()local aW=""if h~="none"and h~="place"then aW=table.concat({m,h,G,H,A,P,V,a7(1),a7(2),a8(1),a8(2)},";")end snail.save(table.concat({"SB1",table.concat(i.p,","),table.concat(i.w,","),table.concat(i.best,","),i.shots,i.hits,m,aW},"|"))end local function a3(f,g)q[f]={}for b=1,5 do local d,e,j=tonumber(g:sub(3*b-2,3*b-2)),tonumber(g:sub(3*b-1,3*b-1)),tonumber(g:sub(3*b,3*b))if not(d and e and j)or j>1 or d+(p[b]-1)*(1-j)>9 or e+(p[b]-1)*j>9 then return false end q[f][b]={x=d,y=e,d=j,n=p[b],h=0}end R(f)local k=0 for a=1,100 do if B[f][a]>0 then k=k+1 end end return k==17 end local function a4(f,g)for a=1,100 do local l=tonumber(g:sub(a,a))or 0 y[f][a]=l if l==2 and B[f][a]>0 then local D=q[f][B[f][a]]D.h=D.h+1 end end end local function aD(g)local c={}for l in(g or""):gmatch("%d+")do c[#c+1]=tonumber(l)end return c end local function bs()local g=snail.load()if type(g)~="string"or g:sub(1,4)~="SB1|"then return end local K={}for l in(g.."|"):gmatch("([^|]*)|")do K[#K+1]=l end local M,ah,z=aD(K[2]),aD(K[3]),aD(K[4])for a=1,3 do i.p[a],i.w[a],i.best[a]=M[a]or 0,ah[a]or 0,z[a]or 0 end i.shots,i.hits=tonumber(K[5])or 0,tonumber(K[6])or 0 m=math.max(1,math.min(3,tonumber(K[7])or 2))local n={}for l in((K[8]or"")..";"):gmatch("([^;]*);")do n[#n+1]=l end if#n<11 then return end if not(a3(1,n[8])and a3(2,n[9]))then q[1],q[2]={},{}R(1)R(2)return end m,h=tonumber(n[1])or 2,n[2]G,H,A,P,V=tonumber(n[3])or 0,tonumber(n[4])or 0,tonumber(n[5])or 0,tonumber(n[6])or 0,tonumber(n[7])or 0 a4(1,n[10])a4(2,n[11])end local function aQ()for f=1,2 do for a=1,100 do y[f][a]=0 end end end local function a_()snail.tick(0)af(1)af(2)aQ()h,x,I="deploy","game",1 A,P,V,o=0,0,0,nil G,H=4,4 S()end local function bm()af(2)aQ()A,P,V=0,0,0 h,o="you","Fire at the enemy waters (top grid)"S()end local function aT(by)h="over"snail.tick(0)i.p[m]=i.p[m]+1 if by then i.w[m]=i.w[m]+1 if i.best[m]==0 or A<i.best[m]then i.best[m]=A end end end local function bv()local a=s(G,H)if y[2][a]~=0 then o="Already fired there - pick another square";return end local N,b=aU(2,a)A,i.shots=A+1,i.shots+1 if N~="miss"then P,i.hits=P+1,i.hits+1 end if N=="miss"then o="You missed."elseif N=="hit"then o="You hit a ship!"else o="You sank the "..W[b].."!"end if X(2)==0 then aT(true)else h="enemy"snail.tick(ar)end S()end local function bo()local a=bi()if not a then return end local N,b=aU(1,a)V=a if N=="miss"then o="Enemy missed. Your shot."elseif N=="hit"then o="Enemy hit your "..W[b].."! Your shot."else o="Enemy sank your "..W[b].."!"end if X(1)==0 then aT(false)else h="you"snail.tick(0)end S()end function tick()if h=="enemy"and x=="game"then bo()else snail.tick(0)end end local function bw()q[1]={}R(1)h,u,O,E,F,o="place",1,1,0,0,nil end local function ac()local k=p[u]local l=aL[aE]local d,e=E,F if l[1]<0 then d=E-k+1 end if l[2]<0 then e=F-k+1 end local j=l[2]~=0 and 1 or 0 if aV(1,d,e,j,k)then return d,e,j end end local function bu(b)if O==1 then if b=="top"then if u>1 then u=u-1 table.remove(q[1])R(1)else af(1)h="deploy"end elseif b=="ok"then for j=1,4 do aE=j if ac()then O=2;return end end o="No room for the "..W[u].." there"elseif b=="left"then E=(E+9)%10 elseif b=="right"then E=(E+1)%10 elseif b=="up"then F=(F+9)%10 elseif b=="down"then F=(F+1)%10 end return end if b=="top"then O=1 elseif b=="ok"then local d,e,j=ac()if not d then o="It doesn't fit that way";return end aO(1,d,e,j,p[u])u,O=u+1,1 if u>5 then h,I,o="deploy",1,"Fleet placed. Ready?";S()end else aE=({right=1,down=2,left=3,up=4})[b]if not ac()then o="It doesn't fit that way"end end end local function aX()return h~="none"and h~="over"end local function aZ()local c={}if aX()then c[#c+1]={h=="deploy"and"Back to your fleet"or"Back to the battle","resume"}end c[#c+1]={"New battle","new"}c[#c+1]={"Level: "..ai[m],"level"}c[#c+1]={"Statistics","stats"}c[#c+1]={"How to play","help"}return c end local bb={"Start the battle","Shuffle the fleet","Place ships by hand"}function key(b)if x=="menu"then local L=aZ()if C>#L then C=#L end local r=L[C][2]if b=="up"then C=(C-2)%#L+1 elseif b=="down"then C=C%#L+1 elseif r=="level"and(b=="ok"or b=="left"or b=="right")then m=(m-1+(b=="left"and-1 or 1))%3+1 S()elseif b=="ok"then if r=="resume"then x="game"if h=="enemy"then snail.tick(ar)end elseif r=="new"then a_()else x=r end end return end if x~="game"then if b=="top"or b=="ok"then x="menu"end return end if h=="place"then o=nil;bu(b);return end if b=="top"then x,C="menu",1 snail.tick(0)return end if h=="deploy"then o=nil if b=="up"then I=(I+1)%3+1 elseif b=="down"then I=I%3+1 elseif b=="ok"then if I==1 then bm()elseif I==2 then af(1);S()else bw()end end elseif h=="over"then if b=="ok"then a_()end elseif h=="you"then o=nil if b=="left"then G=(G+9)%10 elseif b=="right"then G=(G+1)%10 elseif b=="up"then H=(H+9)%10 elseif b=="down"then H=(H+1)%10 elseif b=="ok"then bv()end end end function touch(bq,r,z)if bq~="board"or x~="game"or type(r)~="number"or type(z)~="number"then return end local d,e=math.floor(r)-1,math.floor(z)-1 if d<0 or d>9 or e<0 or e>9 then return end if h=="you"then G,H=d,e elseif h=="place"then local al,ax=d-E,e-F if O==2 and(al==0 or ax==0)then if al~=0 or ax~=0 then key(al>0 and"right"or al<0 and"left"or ax>0 and"down"or"up")if not ac()then return end end else O,E,F=1,d,e end elseif h~="over"then return end key("ok")end local function aF(ag,g,v)local M if g.d==0 then M=v==0 and 1 or(v==g.n-1 and 2 or 5)else M=v==0 and 3 or(v==g.n-1 and 4 or 6)end return ag:sub(M,M)end local function aG(f,a,ag)local b=B[f][a]local g=q[f][b]local d,e=(a-1)%10,(a-1)//10 return aF(ag,g,(d-g.x)+(e-g.y))end local function bn(a,aw,a5)local l=y[2][a]local b=B[2][a]if l==0 then if aw then return aK end if a5 and b>0 then return aG(2,a,at)end return aN end if l==1 then return aw and ba or aM end if aw then return a9 end if Q(2,b)or a5 then return aG(2,a,aq)end return be end local function a0(a,J)local b=B[1][a]local l=y[1][a]if a==V and h~="deploy"then return l==2 and bf or bg end if J and J[a]then return J[a]end if b>0 then return aG(1,a,l==2 and aq or at)end return l==1 and aM or aN end local function a6(f,N)if N==1 then return f==2 and bc or bh end local b=N-2 if b<1 or b>5 then return""end local ay={d=0,n=p[b]}local ag=Q(f,b)and aq or at local c={}for v=0,p[b]-1 do c[#c+1]=aF(ag,ay,v)end return table.concat(c)end local function bl()local w={}local bt=h=="over"local bk=h=="you"for e=0,9 do local c={}for d=0,9 do c[#c+1]=bn(s(d,e),bk and d==G and e==H,bt)end w[#w+1]=table.concat(c).."."..a6(2,e+1)end w[#w+1]="."for e=0,9 do local c={}for d=0,9 do c[#c+1]=a0(s(d,e))end w[#w+1]=table.concat(c).."."..a6(1,e+1)end return table.concat(w,"/")end local function az()local J=nil if h=="place"then J={}if O==2 then local d,e,j=ac()if d then local ay={d=j,n=p[u]}for v=0,p[u]-1 do J[s(d+v*(1-j),e+v*j)]=aF(bd,ay,v)end end end local r=s(E,F)if not J[r]then J[r]=aK end end local w={}for e=0,9 do local c={}for d=0,9 do c[#c+1]=a0(s(d,e),J)end w[#w+1]=table.concat(c)end for au=1,7 do w[#w+1]="."end return table.concat(w,"/")end local function a1(r,z)return z>0 and(r*100//z)or 0 end function draw()if x=="menu"then snail.title("Sea Battle")local L=aZ()if C>#L then C=#L end for a,bp in ipairs(L)do snail.row(bp[1],a==C)end snail.gap()local M,ah=i.p[m],i.w[m]if M>0 then snail.small(ai[m]..": won "..ah.." of "..M..(i.best[m]>0 and(", best "..i.best[m].." shots.")or"."))else snail.small("Five ships each. Find theirs before they find yours.")end snail.hint("BACK leave OK choose UP/DN move")return end if x=="stats"then snail.title("Statistics")for a=1,3 do local z=i.best[a]>0 and(", best win "..i.best[a].." shots")or""snail.text(ai[a]..": won "..i.w[a].." of "..i.p[a]..z)end snail.gap()snail.small("Shots fired "..i.shots..", hits "..i.hits.." ("..a1(i.hits,i.shots).."% accuracy).")snail.hint("BACK back OK back")return end if x=="help"then snail.title("How to play")snail.text("Each side hides five ships: lengths 5, 4, 3, 3 and 2. Ships never touch.")snail.text(snail.touchscreen and"Top grid: enemy waters. Tap a square to fire at it."or"Top grid: enemy waters. Move the sights with the arrows and fire with OK.")snail.text("A ring is a miss, a black square a hit. Sunk ships show their shape.")snail.text("Bottom grid: your fleet. The framed square is the enemy's last shot.")snail.text("The columns at the right show the ships still afloat on each side.")snail.hint("BACK back OK back")return end if h=="deploy"or h=="place"then snail.title("Sea Battle - your fleet")if h=="deploy"then snail.status(o or"Ready to sail? Ships may not touch.")for a,c in ipairs(bb)do snail.row(c,a==I)end snail.board(az())snail.hint("BACK options OK choose UP/DN move")else snail.status(o or("Place the "..W[u].." ("..p[u].." long)"))if O==1 then snail.text(snail.touchscreen and"Tap the square for the bow."or"Arrows move, OK picks the bow square.")snail.text("Then an arrow points the ship that way.")snail.text(u>1 and"BACK takes back the last ship."or"BACK goes back to the random fleet.")snail.board(az())snail.hint("BACK undo OK pick L/R move UP/DN move")else snail.text(snail.touchscreen and"Tap a square in line with the bow to drop it that way."or"Point the ship with the arrows.")snail.text("OK drops it there.")snail.text("BACK picks another bow square.")snail.board(az())snail.hint("BACK cancel OK place L/R turn UP/DN turn")end end return end snail.title("Sea Battle - "..ai[m].." - "..A..(A==1 and" shot"or" shots"))local av if h=="over"then if X(2)==0 then snail.status("Victory! "..P.." hits, "..a1(P,A).."% accuracy")av="Victory in "..A.." shots!"else snail.status("Defeat. "..X(2).." enemy ships were still afloat.")av="Your fleet is sunk"end else snail.status(o or(h=="you"and"Your shot"or"Enemy is aiming..."))end snail.board(bl(),nil,av)if h=="over"then snail.hint((X(2)==0 and"Well fought, admiral!"or"Better luck next time.").." BACK options OK new battle")elseif h=="you"then snail.hint("BACK options OK fire L/R move UP/DN move")else snail.hint("Enemy is aiming... BACK options")end end function start()math.randomseed(os.time())for f=1,2 do for a=1,100 do y[f][a],B[f][a]=0,0 end end bs()if aX()then x="game"if h=="enemy"then snail.tick(ar)end end end
