--!name Hop Across
--!icon game
--!category Games
local p=snail.touchscreen==true local i,M=14,14 local V={2,5,8,11}local am,A=7,13 local L={[2]={-1,1,"lll...llll...."},[3]={1,2,"..lll.....lll."},[4]={-1,2,"llll....lll..."},[5]={1,1,".ll...ll...ll."},[6]={-1,2,"...llll...llll"},[8]={-1,2,"ab....ab......"},[9]={1,1,"c....c.....c.."},[10]={-1,2,"..d...d...d..."},[11]={-1,1,"....ab.....ab."},[12]={1,2,"c...c....c...."},}local al={l="W",c="C",d="D",a="T"}local ae=4 local ad={}local an={"..^!..","..++..","<++++>","=++++?","..++..","..~|.."}local h,K="menu",1 local H,u={},{}local d,f,Y=6,A,A local g,I,D,U=0,3,1,0 local j,ai,y=0,"x",0 local x,t,Q,C,aa=false,1,nil,0,false local n={0,0}local T=1 local function az(av)T=(T*1664525+1013904223)&0xFFFFFFFF return(T>>16)%av end local function _()if t==1 then return math.max(500,1000-(D-1)*100)end return math.max(450,750-(D-1)*75)end local function aA()snail.save(n[1]..","..n[2])end local function ab(a)return a>=2 and a<=6 end local function ac()d,f,Y=6,A,A end local function aw()if os and os.time then T=os.time()%100000+11 end for a,w in pairs(L)do local ao=az(i)H[a]={}for b=1,i do local c=(b+ao-1)%i+1 H[a][b]=w[3]:sub(c,c)end end for c=1,#V do u[c]=false end ac()g,I,D,U=0,3,1,0 j,y=0,1 x,Q,C=false,nil,0 h="play"snail.tick(_())end local function ax()h="over"snail.tick(0)aa=g>n[t]if aa then n[t]=g aA()end end local function X(v)I=I-1 j,ai=3,v end local function ah()if not L[f]or j>0 then return end local o=H[f][d+1]if ab(f)then if o~="l"then X("s")end elseif o~="."then X("x")end end local function aB(a,as)local P=H[a]if as>0 then table.insert(P,1,table.remove(P))else table.insert(P,table.remove(P,1))end end function tick()if h~="play"or x then snail.tick(0)return end if y>0 then y=y-1 return end if C>0 then C=C-1 if C==0 then Q=nil end end if j>0 then j=j-1 if j==0 then ac()if I==0 then ax()end end return end U=U+1 for a,w in pairs(L)do if U%w[2]==0 then aB(a,w[1])if a==f and ab(a)then d=d+w[1]if d<0 or d>=i then d=math.max(0,math.min(i-1,d))X("s")end end end end ah()end local function at(b)for c,Z in ipairs(V)do if Z==b then return c end end end local function ay(c)u[c]=true g=math.min(9999,g+10)ac()for W,Z in ipairs(u)do if not Z then return end end for au=1,#u do u[au]=false end D=D+1 g=math.min(9999,g+20)Q,C="ROUND "..D.."  +20",2 snail.tick(_())end local function B(E,F)if h~="play"or x or y>0 or j>0 then return end local b,a=d+E,f+F if b<0 or b>=i or a<1 or a>A then return end if a==1 then local c=at(b)if c and not u[c]then ay(c)end return end d,f=b,a ah()if a<Y and j==0 then Y=a g=math.min(9999,g+1)end end function key(e)if h=="menu"then if e=="up"or e=="down"then K=3-K elseif e=="ok"then t=K aw()end return end if h=="over"then if e=="ok"then h="menu"end return end if e=="ok"then x=not x snail.tick(x and 0 or _())return end if e=="up"then B(0,-1)elseif e=="down"then B(0,1)elseif e=="left"then B(-1,0)elseif e=="right"then B(1,0)end end local function aq(m,r)local a,b=r-M-1,m-1 if a<0 then return nil end local O,aj=2*(b-ae)-5,2*a-5 if O>=-7 and O<=7 then local af,ag=math.abs(O),math.abs(aj)if af>ag then return O<0 and"left"or"right"end if ag>af then return aj<0 and"up"or"down"end return nil end for W,s in ipairs(ad)do if b>=s[1]and b<=s[1]+1 then return s[3]end end end function touch(v,m,r)if v~="board"then return end if p then local e=aq(m,r)if e then return key(e)end else m=m-1 end if r>M then return end local E,F=m-1-d,r-1-f if F~=0 and math.abs(F)>=math.abs(E)then B(0,F>0 and 1 or-1)elseif E~=0 then B(E>0 and 1 or-1,0)end end local function ar()local k={}for a=1,6 do k[a]={}for b=1,i do k[a][b]="."end for c=1,6 do k[a][ae+c]=an[a]:sub(c,c)end end for W,s in ipairs(ad)do local R,a,b=s[4]and"()[]"or"$&;:",s[2]+1,s[1]+1 k[a][b],k[a][b+1]=R:sub(1,1),R:sub(2,2)k[a+1][b],k[a+1][b+1]=R:sub(3,3),R:sub(4,4)end for a=1,6 do k[a]=table.concat(k[a])end return k end local function aC(G)local J={}for a=0,M-1 do local z=table.concat(G[a])J[#J+1]=p and z or"."..z.."."end if p then for W,S in ipairs(ar())do J[#J+1]=S end end return table.concat(J,"/")end local function ap()local q=y>0 local G={}for a=0,M-1 do local S={}for b=1,i do S[b]="."end G[a]=S end local function l(b,a,o)G[a][b+1]=o end local z=q and"8888"or string.format("%4d",g):gsub(" ","_")for c=1,4 do l(c-1,0,z:sub(c,c))end for c=1,3 do l(10+c,0,(q or I>=c)and"h"or"H")end for b=0,i-1 do l(b,1,"w")l(b,am,q and"h"or"H")l(b,A,q and"h"or"H")end for c,b in ipairs(V)do l(b,1,(q or u[c])and"n"or"N")end for a,w in pairs(L)do local v=w[3]:match("%a")for b=0,i-1 do local o=H[a][b+1]if q then o=v=="a"and(b%2==0 and"a"or"b")or v end l(b,a,o=="."and al[v]or o)end end if j>0 then l(d,f,ai)elseif not q then l(d,f,ab(f)and"g"or"h")end return aC(G)end function draw()local ak=string.format("GAME %s  BEST %d",t==1 and"A"or"B",n[t])snail.title((p and h~="menu")and"Hop Across  "..ak or"Hop Across")if h=="menu"then snail.row("Game A",K==1)snail.row("Game B (faster)",K==2)snail.gap()snail.text(string.format("Best: A %d   B %d",n[1],n[2]))snail.gap()snail.small("Hop over the road, ride the logs across the river")snail.small("and fill the four lily-pad homes.")if p then snail.small("Hop with the d-pad under the screen, or tap the")snail.small("screen on the side you want to hop to.")else snail.small("UP and DOWN hop forward and back, LEFT and RIGHT")snail.small("hop sideways.")end snail.small("Three hoppers a game; each round runs faster.")snail.hint("OK start BACK leave")return end if not p then snail.small(ak)end local N=Q if y>0 then N="GAME "..(t==1 and"A"or"B")end if x then N="PAUSED"end if h=="over"then N=string.format("GAME OVER  %d%s",g,aa and"  BEST!"or"")end snail.board(ap(),nil,N)if h=="over"then snail.hint("OK menu BACK leave")elseif p then snail.hint("OK pause BACK leave")else snail.hint("L left R right OK pause BACK leave")end end function start()snail.ink("fast")local z=snail.load()if z then local m,r=z:match("(%d+),(%d+)")n={tonumber(m)or 0,tonumber(r)or 0}end end
