--!name Score Keeper
--!icon notes
--!category Games
local T,ao,aJ,I=8,10,8,8 local d={"Player 1","Player 2"}local o={{}}local J={}local v=0 local M,w,u,F=true,0,false,false local y={}local l,g,ac,p="setup",1,nil,nil local C=1 local _,ab local function L(Z,aX,aQ)return math.max(aX,math.min(aQ,Z))end local function aB(j)return(j>=0 and"+"or"")..j end local function V()return o[#o]end local function K()return#o+v end local function S(a)local b=J[a]or 0 for U,m in ipairs(o)do b=b+(m[a]or 0)end return b end local function ak()local z={table.concat({"S1",M and 1 or 0,w,u and 1 or 0,F and 1 or 0,v},"|")}for U,j in ipairs(d)do z[#z+1]="p"..j end local function aA(e,m)local b={}for a=1,#d do b[a]=m[a]or""end z[#z+1]=e..table.concat(b,",")end aA("b",J)for U,m in ipairs(o)do aA("r",m)end return table.concat(z,"\n")end local function A()local h=ak()while#h>1023 and#o>1 do local m=table.remove(o,1)for a=1,#d do J[a]=(J[a]or 0)+(m[a]or 0)end v=v+1 h=ak()end snail.save(h)end local function at(h)if type(h)~="string"or h:sub(1,3)~="S1|"then return end local j,m,s,i={},{},{},{}for W in h:gmatch("[^\n]+")do local e,Z=W:sub(1,1),W:sub(2)if e=="S"then for q in(Z.."|"):gmatch("([^|]*)|")do i[#i+1]=q end elseif e=="p"and#j<T then j[#j+1]=Z:sub(1,ao)elseif e=="b"or e=="r"then local b,a={},0 for q in(Z..","):gmatch("([^,]*),")do a=a+1 if a<=T then b[a]=tonumber(q)and L(math.floor(tonumber(q)),-9999999,9999999)end end if e=="b"then s=b else m[#m+1]=b end end end d,J,o=j,s,#m>0 and m or{{}}M,w,u=i[2]~="0",L(math.floor(tonumber(i[3])or 0),0,99999),i[4]=="1"F,v=i[5]=="1"and#j>0,math.max(0,math.floor(tonumber(i[6])or 0))return true end local function R(ba,Y)y[#y+1]={s=ak(),what=ba,tag=Y,d=0}if#y>aJ then table.remove(y,1)end end local function aO()local D=table.remove(y)if not D then p="Nothing to undo";return end at(D.s)A()p="Undone: "..D.what end local function ar(a,j,Y)local D=y[#y]if not(Y and D and D.tag==Y)then R("",Y)D=y[#y]end D.d=D.d+j D.what=d[a].." "..aB(D.d)V()[a]=L((V()[a]or 0)+j,-99999,99999)A()end local function av(i)i(J)for U,m in ipairs(o)do i(m)end end local function a7(x,s)d[x],d[s]=d[s],d[x]av(function(b)b[x],b[s]=b[s],b[x]end)end local function a0(a)for G=a,T do d[G]=d[G+1]end av(function(b)for G=a,T do b[G]=b[G+1]end end)end local function aw()local O,c=nil,{}for a=1,#d do local b=S(a)if not O or(M and b>O)or(not M and b<O)then O,c=b,{d[a]}elseif b==O then c[#c+1]=d[a]end end return c,O end local function aD(c)return#c==1 and c[1]or#c==2 and c[1].." and "..c[2]or#c.." players"end local function a6()local c,s=aw()if#c==0 then return""end if#c>1 and#c==#d then return"All tied on "..s end return aD(c)..(#c>1 and" tie on "or u and" wins with "or" leads with ")..s end local function ax()o,J,v,u={{}},{},0,false end local function aY()local ai=false for a=1,#d do ai=ai or V()[a]~=nil end if not ai then p="No points in round "..K().." yet";return end R("round "..K())if w>0 then for a=1,#d do u=u or S(a)>=w end end o[#o+1]={}g=1 p=not u and"Round "..K()or nil A()end local aG="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function a9(a)return aG:sub(a+1,a+1)end local aI={"1234567890<","QWERTYUIOP'","ASDFGHJKL-+","^ZXCVBNM,./","()[_____]?>"}local aK={"1234567890<","-+        >"}local aL="'-+,./()?"local k local n,t,H=1,2,1 local function ag()return k.num and aK or aI end local function E(q,N)return ag()[N]:sub(q,q)end local function ae(i,a)_=a local h=i=="n"and d[a]or""k={f=i,s=h,at=#h,num=i~="n",max=i=="n"and ao or 6}H=(h==""and i=="n")and 1 or 0 if k.num then n,t=1,1 elseif t==1 or t>5 then n,t=1,2 end l="kb"end local function as()local i,a=k.f,_ local h=(k.s:gsub("^%s+",""):gsub("%s+$",""))l,k=i=="p"and"table"or"setup",nil if i=="n"then if h~=""then d[a]=h else if not ab then if F then R("remove "..d[a])end p=d[a].." is out"end a0(a)g=L(g,1,#d+1)end ab=nil elseif h~=""then local a4,j=h:match("^([+-]?)(%d+)$")if not j then p="That isn't a number: "..h elseif i=="t"then w=L(tonumber(j),0,99999)else ar(a,a4=="-"and-tonumber(j)or tonumber(j))g,p=a+1,d[a].." now has "..S(a)end end A()end local function az()local e=E(n,t)local h,r=k.s,k.at if e=="<"then if r>0 then k.s,k.at=h:sub(1,r-1)..h:sub(r+1),r-1 end elseif e=="^"then H=(H+1)%3 elseif e=="["then k.at=math.max(0,r-1)elseif e=="]"then k.at=math.min(#h,r+1)elseif e==">"then as()elseif#h>=k.max then p="That's as long as it can be"else if e=="_"then e=" "elseif H==0 then e=e:lower()end k.s,k.at=h:sub(1,r)..e..h:sub(r+1),r+1 if H==1 and e:match("%a")then H=0 end end end local function aT(f)if f=="top"then as()elseif f=="ok"then az()elseif f=="left"or f=="right"then local aj,e=f=="left"and-1 or 1,E(n,t)local j repeat n=(n-1+aj)%11+1 j=E(n,t)until j~=" "and(e~="_"or j~="_")elseif f=="up"or f=="down"then t=(t-1+(f=="up"and-1 or 1))%#ag()+1 if E(n,t)==" "then for Q=1,10 do if n-Q>=1 and E(n-Q,t)~=" "then n=n-Q;break end if n+Q<=11 and E(n+Q,t)~=" "then n=n+Q;break end end end end end function touch(aV,x,s)if aV~="board"or l~="kb"or x<1 or x>11 or s<1 or s>#ag()or E(x,s)==" "then return end p=nil n,t=x,s az()end local function aU()local an=ag()local X,aZ={},E(n,t)=="_"for N=1,#an do local b={}for q=1,11 do local e=an[N]:sub(q,q)local af=N==t and(q==n or(aZ and e=="_"))local P,ay,a=af and 1 or 0,aL:find(e,1,true)if e==" "then a=nil;e="."elseif e:match("%u")then a=af and e:byte()-65 elseif e:match("%d")then a=af and 26+e:byte()-48 elseif ay then a=(af and 35+ay)or(e=="/"and 45)elseif e=="<"then a=46+P elseif e=="^"then a=48+H*2+P elseif e=="["then a=54+P elseif e=="]"then a=56+P elseif e=="_"then a=58+P*3+(E(q-1,N)~="_"and 0 or E(q+1,N)~="_"and 2 or 1)else a=64+P end b[q]=a and a9(a)or e=="."and"."or("{"..e.."}")end X[N]=table.concat(b)end return table.concat(X,"/")end local function aa()local b={u and"playon"or"next"}if not u then b[2]="end"end for U,c in ipairs({"undo","hist","again","setup","fresh"})do b[#b+1]=c end return b end local function ap(c)for G,q in ipairs(aa())do if q==c then return#d+G end end end local function ah()local b={}for a=1,#d do b[a]=a end if#d<T then b[#b+1]="add"end b[#b+1]="win"b[#b+1]="target"b[#b+1]="start"return b end local function a1()if l=="table"then return#d+#aa()end if l=="setup"then return#ah()end return 1 end local function a8(f,x)local aq=aa()local j=#d+#aq local B=(f=="left"and-1)or(f=="right"and 1)if f=="up"then g=(g-2)%j+1 elseif f=="down"then g=g%j+1 elseif d[g]then if f=="ok"then ae("p",g)elseif B then ar(g,B,g.."."..K())end elseif f=="ok"then local c=aq[g-#d]if c=="next"then aY()elseif c=="end"then R("end the game");u=true;A()elseif c=="playon"then R("play on");u=false;p="Playing on";A()elseif c=="undo"then aO()elseif c=="hist"then l,C="hist",math.max(1,#o-I+1)elseif c=="setup"then l,g="setup",1 elseif x~=c then ac=c elseif c=="again"then R("new game")ax()g,p=1,"New game: round 1"A()else ax()F,y,l,g=false,{},"setup",1 A()end end end local function a2(f)local b=ah()local c=b[g]local B=(f=="left"and-1)or(f=="right"and 1)if f=="top"then l,g="table",ap("setup")elseif f=="up"then g=(g-2)%#b+1 elseif f=="down"then g=g%#b+1 elseif type(c)=="number"then if f=="ok"then ae("n",c)elseif B and d[c+B]then a7(c,c+B);g=c+B;A()end elseif c=="add"then if f=="ok"then d[#d+1]=""ab=true ae("n",#d)end elseif c=="win"then if f=="ok"or B then M=not M;A()end elseif c=="target"then if f=="ok"then ae("t")elseif B then local r=w+(B<0 and-1 or 0)local aj=r<50 and 5 or r<200 and 10 or 50 w=L(w+B*aj,0,99999)A()end elseif f=="ok"then if#d==0 then p="Add a player first"else F,l,g=true,"table",1 A()end end end local function aR(f)local al=math.max(1,#o-I+1)if f=="top"then l,g="table",ap("hist")elseif f=="up"or f=="left"then C=math.max(1,C-I)elseif f=="down"or f=="right"then C=math.min(al,C+I)end end function key(f)if l=="kb"then p=nil;aT(f);return end if f=="top"and(l=="table"or(l=="setup"and not F))then return end p=nil local x=ac ac=nil if l=="table"then a8(f,x)elseif l=="setup"then a2(f)else aR(f)end if l~="kb"then g=L(g,1,a1())end end local aF=("...................."):rep(3,"/")local function X(b,r,am,a5)local aC=math.max(1,math.min(r-am//2,#b-am+1))for a=aC,math.min(#b,aC+am-1)do snail.row(b[a]:sub(1,44),a==r)if a==a5 and a<#b then snail.rule()end end end local aE={next="Next round",["end"]="End the game now",playon="Play on",hist="Round history",again="New game, same players",setup="Players and rules",fresh="Set up a new game",}local function aM(c)if ac==c then return c=="again"and"Press OK again: scores back to 0"or"Press OK again: start over"end if c=="undo"then return#y>0 and"Undo: "..y[#y].what or"Undo (nothing yet)"end return aE[c]end local function a_(a)local au=V()[a]return d[a].."  "..S(a)..(au and"  ("..aB(au)..")"or"")end local function a3(c)if type(c)=="number"then return c..". "..(d[c]~=""and d[c]or"(new)")end if c=="add"then return"+ Add a player"end if c=="win"then return"Winner: "..(M and"highest"or"lowest").." score"end if c=="target"then return"Play to: "..(w>0 and w or"no target")end return F and"Back to the scores"or"Start the game"end local function ad(h)return"{"..tostring(h):gsub("[{}/]",""):sub(1,5).."}"end local function aS()local z,b={},{"{#}"}for a=1,#d do b[#b+1]=ad(d[a]:sub(1,4))end z[1]=table.concat(b)local function W(aW,m)b={ad(aW)}for a=1,#d do b[#b+1]=m[a]and ad(m[a])or"."end z[#z+1]=table.concat(b)end if v>0 and C==1 then W(v>1 and"1-"..v or 1,J)end for m=C,math.min(#o,C+I-1)do W(m+v,o[m])end b={"{=}"}for a=1,#d do b[#b+1]=ad(S(a))end z[#z+1]=table.concat(b)return table.concat(z,"/")end local aH={n="Up to 10 letters. Clear the name to take the player out.",t="The game ends after the round where someone reaches it. 0: no target.",}local function aP()local i=k.f snail.title(i=="p"and d[_].." - round "..K()or i=="t"and"Play to"or ab and"New player"or"Name")snail.status(p or(k.num and""or H==2 and"CAPS"or H==1 and"Shift"or""))snail.text(k.s:sub(1,k.at).."|"..k.s:sub(k.at+1))snail.small(i=="p"and d[_].." has "..S(_)..". Type 12 to add, -5 to take away."or aH[i])snail.board(aU())snail.hint("arrows move  BACK done  OK press")end function draw()if l=="kb"then return aP()end if l=="setup"then snail.title(F and"Players and rules"or"New game")snail.status(p or(#d..(#d==1 and" player"or" players")..(w>0 and", to "..w or"")))local b={}for G,c in ipairs(ah())do b[G]=a3(c)end X(b,g,14)local c=ah()[g]if type(c)=="number"then snail.small("OK renames (clear the name to take them out). LEFT/RIGHT changes the seating order.")elseif c=="target"then snail.small("The game ends after the round where someone reaches this score.")elseif c=="win"then snail.small("Lowest wins suits golf and hearts-style games.")end local aN=F and"BACK scores"or"BACK leave"snail.hint(aN..(type(c)=="number"and"  OK rename  L/R seat"or c=="target"and"  OK type  L/R change"or c=="win"and"  OK change"or"  OK choose").."  UP/DN move")return end if l=="hist"then local al=math.min(#o,C+I-1)snail.title("Round history")snail.status("Rounds "..(C+v).."-"..(al+v).." of "..K()..(v>0 and C==1 and", 1-"..v.." merged"or""))snail.board(aS())if#o>I then snail.small("UP/DOWN: earlier and later rounds.")end snail.hint("BACK scores"..(#o>I and"  UP/DN rounds"or""))return end snail.title(u and"Game over"or"Round "..K())snail.status((p or a6()..(w>0 and not u and", to "..w or"")):sub(1,47))if u then local c=aw()snail.board(aF,nil,(#c==1 and c[1].." wins!"or aD(c).." tie"):upper():sub(1,32))end local b={}for a=1,#d do b[a]=a_(a)end for U,c in ipairs(aa())do b[#b+1]=aM(c)end X(b,g,14,#d)if K()==1 and not next(V())then snail.small("OK types a player's points, LEFT/RIGHT adds or takes 1. Then Next round.")end snail.hint(d[g]and"BACK leave  OK points  L-1  R+1  UP/DN move"or"BACK leave  OK choose  UP/DN move")end function start()if at(snail.load())then l=F and"table"or"setup"end g=1 end
