Made a change to save the board layout. The reason was as I got into the higher levels ( currently at 11), it would take longer than I wanted to work on it and when I closed Codea, I would lose what I had done so far. Now, I can come back at a later time and pickup where I left off.
viewer.mode=FULLSCREEN
function setup()
--saveProjectData("cubes",2) -- set start size to 2
--saveProjectData("stab",nil) -- clear board save table
cubes=readProjectData("cubes",2)
rectMode(CENTER)
size=70
setup2()
end
function setup2()
dx=(WIDTH-(size*cubes))//2-size//2
dy=(HEIGHT-(size*cubes))//2-size//2
xSize,ySize=cubes,cubes
s1,s2=vec2(0,0),vec2(0,0)
tab={}
for x=1,xSize do
tab[x]={}
for y=1,ySize do
tab[x][y]=sqr(x*size,y*size)
end
end
ss=readProjectData("stab")
if ss~=nil then
fillBoard()
else
randomBoard()
end
end
function fillBoard()
sTab=json.decode(ss)
pos=0
for x=1,xSize do
for y=1,ySize do
tab[x][y].left=sTab[pos+1]
tab[x][y].right=sTab[pos+2]
tab[x][y].top=sTab[pos+3]
tab[x][y].bottom=sTab[pos+4]
pos=pos+4
end
end
end
function randomBoard()
for x=1,xSize do
for y=1,ySize do
if x==1 then
tab[x][y].left=math.random(10,99)
tab[x][y].right=math.random(10,99)
else
tab[x][y].left=tab[x-1][y].right
tab[x][y].right=math.random(10,99)
end
if y==1 then
tab[x][y].bottom=math.random(10,99)
tab[x][y].top=math.random(10,99)
else
tab[x][y].bottom=tab[x][y-1].top
tab[x][y].top=math.random(10,99)
end
end
end
for z=1,100 do
x1=math.random(xSize)
y1=math.random(ySize)
x2=math.random(xSize)
y2=math.random(ySize)
swap(x1,y1,x2,y2)
end
saveTab()
end
function swap(x1,y1,x2,y2)
left=tab[x2][y2].left
right=tab[x2][y2].right
top=tab[x2][y2].top
bottom=tab[x2][y2].bottom
tab[x2][y2].left=tab[x1][y1].left
tab[x2][y2].right=tab[x1][y1].right
tab[x2][y2].top=tab[x1][y1].top
tab[x2][y2].bottom=tab[x1][y1].bottom
tab[x1][y1].left=left
tab[x1][y1].right=right
tab[x1][y1].top=top
tab[x1][y1].bottom=bottom
end
function draw()
background(172, 216, 223, 255)
fontSize(18)
for x=1,xSize do
for y=1,ySize do
tab[x][y]:draw()
end
end
if corr then
fontSize(30)
fill(255, 0, 0)
text("Double tap for next level",WIDTH/2,HEIGHT-100)
end
end
function saveTab()
sTab={}
for x=1,xSize do
for y=1,ySize do
l=tab[x][y].left
r=tab[x][y].right
t=tab[x][y].top
b=tab[x][y].bottom
table.insert(sTab,l)
table.insert(sTab,r)
table.insert(sTab,t)
table.insert(sTab,b)
end
end
sss=json.encode(sTab)
saveProjectData("stab",sss)
end
function touched(t)
if t.state==BEGAN then
if t.tapCount==2 and corr then
cubes=cubes+1
saveProjectData("cubes",cubes)
corr=false
saveProjectData("stab",nil)
setup2()
end
if t.x<WIDTH/2 and t.y>HEIGHT-100 and corr then
xSize=xSize+1
ySize=ySize+1
corr=false
setup2()
return
end
for x=1,xSize do
for y=1,ySize do
tab[x][y]:touched(t)
end
end
end
end
function checkCorrect()
corr=true
for x=2,xSize do
for y=2,ySize do
if tab[x][y].left~=tab[x-1][y].right then
corr=false
end
if tab[x][y].bottom~=tab[x][y-1].top then
corr=false
end
end
end
end
sqr = class()
function sqr:init(x,y,c)
self.x=x
self.y=y
self.left=0
self.right=0
self.top=0
self.bottom=0
end
function sqr:draw()
fill(255)
if corr then
fill(255, 255, 0, 255)
end
if s1.x==self.x and s1.y==self.y then
fill(0, 255, 13, 255)
end
rect(self.x+dx,self.y+dy,size-2,size-2)
fill(255,0,0)
text(self.left,self.x-size/3.5+dx,self.y+dy)
text(self.right,self.x+size/3.5+dx,self.y+dy)
text(self.top,self.x+dx,self.y+size/3.5+dy)
text(self.bottom,self.x+dx,self.y-size/3.5+dy)
end
function sqr:touched(t)
if t.x>self.x-size/2+dx and t.x<self.x+size/2+dx and
t.y>self.y-size/2+dy and t.y<self.y+size/2+dy then
if s1.x==0 then
s1=vec2(self.x,self.y)
else
s2=vec2(self.x,self.y)
swap(s1.x/size,s1.y/size,s2.x/size,s2.y/size)
s1,s2=vec2(0,0),vec2(0,0)
checkCorrect()
saveTab()
end
end
end