Page 1 of 1

RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Fri May 11, 2018 10:47 pm
by Dav
.
NOTE: Updated a better version of game in this thread - You can find it HERE.

Here's another math puzzle game. In this one you have to make the numbers in the rows and columns add up to the numbers on the edges. When they do, they will turn green. Tap on the numbers to turn them on/off. Turn all edge numbers green to complete level. There are 15 levels. Hope someone will enjoy it. IPad only right now.

I set up the code so the grid size can be changed in future updates easy, but I need to figure out an algorithm to come with number data on the fly so I don't have to hard-code the level data for every level.

- Dav

Code: Select all


'RoCoLoco.txt v1.0
'Coded by Dav, May/2018
'Row & Column number adding puzzle game.
'Touch red numbers inside grid to turn them on/off.
'Each row/column of red numbers must total the number
'on the edge of the grid. When they total up correctly
'the edge numbers will turn green. Turn all the edge
'numbers green to complete the level. Don't go loco...


if lowstr$(device_type$())<>"ipad" then
  print "Sorry, game for iPad only right now."
  end
end if

option base 1
set orientation vertical
get screen size sw,sh

level=1  '<<< set starting level here

maxlevel=15

'===
top:
'===

graphics
graphics clear .3,.3,.3

draw color 0,0,0!fill color 1,1,1

grid=7 !size=sw/grid
dim bv(grid*grid),bx(grid*grid),by(grid*grid)
dim fc(grid*grid) 'flag if button on/off

set buttons custom
set buttons font size 74
if grid > 7 then set buttons font size 54
if grid > 9 then set buttons font size 34

draw font size 40
draw color .5,.5,.5
draw text "Level "&level&" of "&str$(maxlevel) at sw/4,sh-62
draw color 0,0,0

'init button values, and generate x/y values...
bc=1
for r= 1 to grid
   for c = 1 to grid
     x=(r*size) ! y=(c*size)
     if bc=1 then goto skip  'dont draw corners...
     if bc=grid then goto skip
     if bc=grid*grid-grid+1 then goto skip
     if bc=grid*grid then goto skip
skip:
     bx(bc)=x-size!by(bc)=y!bv(bc)=bc!fc(bc)=1
     bc=bc+1
   next c
next r


gosub setlevel
gosub drawboard

do
  for t=2 to grid*grid-1
    if button_pressed(str$(t)) then
       'ignore outside rows
       if t < grid then break
       if t > (grid*grid)-grid then break
       for tt=1 to grid*grid step grid
          if t = tt then goto skip2
          if t = tt+(grid-1) then goto skip2
       next tt
       notes stop
       if fc(t)=1 then
          fc(t)=0!notes set "120:sa7"!notes play
       else
          fc(t)=1!notes set "120:sa6"!notes play
       end if
       gosub drawboard
       gosub checkforwin
    end if
    skip2:
  next t
until 0

end


'========
drawboard:
'========

if levelnew=1 then
   s$="" 'generate random notes
   for o = 1 to 32
      s$=s$&chr$(66+rnd(6))&str$(5+rnd(2))
   next o
   notes set "92:t"&s$ '23
   notes play
end if

'draw inside board
for t=2 to grid*grid-1
  'skip corners
  if t=grid then goto skipit
  if t =grid*grid-grid+1 then goto skipit
  'skip outside rows
  if t < grid then goto skipit
  if t > (grid*grid)-grid then goto skipit
  for tt=1 to grid*grid step grid
      if t = tt then goto skipit
      if t = tt+(grid-1) then goto skipit
  next tt
  if fc(t)=1 then 
     fill color 1,.5,.5
     draw color 1,1,1
  else
     fill color .3,.3,.3
     draw color 0,0,0
  end if
  button str$(t) text str$(bv(t)) at bx(t),by(t) size size,size
  if levelnew=1 then pause .025
  skipit:
next t

'compute and draw left/right rows
for t =2 to grid-1
   c=0
   for tt=1 to grid -2
     if fc(t+(grid*tt))= 1 then c=c+bv(t+(grid*tt))
   next tt
   draw color 0,0,0
   if bv(t)=c then
     fill color 0,1,0
     fc(t)=1!fc(t+(grid*(grid-1)))=1
   else
     fill color .5,.5,.5
     fc(t)=0!fc(t+(grid*(grid-1)))=0
   end if
   button str$(t) text str$(bv(t)) at bx(t),by(t) size size,size
   n=t+(grid*(grid-1))
   if levelnew=1 then pause .025
   button str$(n) text str$(bv(n)) at bx(n),by(n) size size,size
next t

'compute and draw top/bottom
for t=grid+1 to (grid*(grid-2)+1) step grid
   c=0
   for tt=1 to (grid-2)
      if fc(t+tt)=1 then c=c+bv(t+tt)
   next tt
   if bv(t)=c then
     fill color 0,1,0
     fc(t)=1!fc(t+(grid-1))=1
   else
     fill color .5,.5,.5
     fc(t)=0!fc(t+(grid-1))=0
   end if
   if levelnew then pause .025
   button str$(t) text str$(bv(t)) at bx(t),by(t) size size,size
n=t+(grid-1)
button str$(n) text str$(bv(n)) at bx(n),by(n) size size,size
next t
notes stop
levelnew=0
return


'==========
checkforwin:
'==========

'check down
for t=2 to grid-1
  if fc(t)=0 then goto nope
next t
'check across
for t=grid+1 to (grid*5+1) step grid
  if fc(t)=0 then goto nope
next t

'made it here, must be completed
level=level+1
notes stop
notes set "7:s(c3eg)(e4cg3)(g4ec)(c5g4e)"
notes play 

pause 1

fill color 0,1,0
for i=2 to grid*grid-1
  if i=grid then goto skippp
  if i=grid*grid-grid+1 then goto skippp
  button str$(i) text chr$(9989) at bx(i),by(i) size size,size
  pause .015
  skippp:
next i

pause .5

for i=2 to grid*grid-1
   button str$(i) delete
   pause .015
next i

pause .5

levelnew=1
if level>maxlevel then end   'all levels done? end
goto top

nope:
return


'=======
setlevel:
'=======

if level = 1 then
'make first level data
bv(8)=15!bv(15)=7!bv(22)=17!bv(29)=12!bv(36)=9
bv(2)=3!bv(9)=3!bv(16)=1!bv(23)=8!bv(30)=5!bv(37)=5!bv(44)=3
bv(3)=25!bv(10)=2!bv(17)=3!bv(24)=8!bv(31)=9!bv(38)=6!bv(45)=25
bv(4)=7!bv(11)=4!bv(18)=9!bv(25)=2!bv(32)=1!bv(39)=3!bv(46)=7
bv(5)=9!bv(12)=2!bv(19)=4!bv(26)=2!bv(33)=7!bv(40)=3!bv(47)=9
bv(6)=16!bv(13)=6!bv(20)=3!bv(27)=5!bv(34)=2!bv(41)=5!bv(48)=16
bv(14)=15!bv(21)=7!bv(28)=17!bv(35)=12!bv(42)=9
end if

if level = 2 then
'make first level data
bv(8)=10!bv(15)=13!bv(22)=30!bv(29)=6!bv(36)=17
bv(2)=17!bv(9)=6!bv(16)=6!bv(23)=7!bv(30)=2!bv(37)=9!bv(44)=17
bv(3)=21!bv(10)=1!bv(17)=7!bv(24)=9!bv(31)=6!bv(38)=5!bv(45)=21
bv(4)=16!bv(11)=4!bv(18)=2!bv(25)=7!bv(32)=4!bv(39)=3!bv(46)=16
bv(5)=10!bv(12)=3!bv(19)=1!bv(26)=7!bv(33)=7!bv(40)=6!bv(47)=10
bv(6)=12!bv(13)=1!bv(20)=4!bv(27)=7!bv(34)=7!bv(41)=8!bv(48)=12
bv(14)=10!bv(21)=13!bv(28)=30!bv(35)=6!bv(42)=17
end if

if level = 3 then
'make first level data
bv(8)=19!bv(15)=20!bv(22)=15!bv(29)=18!bv(36)=20
bv(2)=26!bv(9)=5!bv(16)=8!bv(23)=1!bv(30)=9!bv(37)=9!bv(44)=26
bv(3)=13!bv(10)=4!bv(17)=2!bv(24)=7!bv(31)=4!bv(38)=7!bv(45)=13
bv(4)=25!bv(11)=7!bv(18)=7!bv(25)=8!bv(32)=7!bv(39)=4!bv(46)=25
bv(5)=19!bv(12)=8!bv(19)=3!bv(26)=6!bv(33)=2!bv(40)=3!bv(47)=19
bv(6)=9!bv(13)=4!bv(20)=3!bv(27)=9!bv(34)=6!bv(41)=1!bv(48)=9
bv(14)=19!bv(21)=20!bv(28)=15!bv(35)=18!bv(42)=20
end if

if level = 4 then
'make first level data
bv(8)=17!bv(15)=10!bv(22)=15!bv(29)=14!bv(36)=18
bv(2)=6!bv(9)=7!bv(16)=5!bv(23)=1!bv(30)=3!bv(37)=7!bv(44)=6
bv(3)=16!bv(10)=4!bv(17)=2!bv(24)=6!bv(31)=8!bv(38)=5!bv(45)=16
bv(4)=21!bv(11)=8!bv(18)=1!bv(25)=8!bv(32)=4!bv(39)=4!bv(46)=21
bv(5)=12!bv(12)=5!bv(19)=2!bv(26)=9!bv(33)=9!bv(40)=5!bv(47)=12
bv(6)=19!bv(13)=4!bv(20)=7!bv(27)=9!bv(34)=6!bv(41)=9!bv(48)=19
bv(14)=17!bv(21)=10!bv(28)=15!bv(35)=14!bv(42)=18
end if

if level = 5 then
'make first level data
bv(8)=12!bv(15)=7!bv(22)=13!bv(29)=17!bv(36)=18
bv(2)=14!bv(9)=7!bv(16)=1!bv(23)=7!bv(30)=5!bv(37)=1!bv(44)=14
bv(3)=19!bv(10)=8!bv(17)=2!bv(24)=7!bv(31)=3!bv(38)=9!bv(45)=19
bv(4)=10!bv(11)=2!bv(18)=6!bv(25)=4!bv(32)=2!bv(39)=6!bv(46)=10
bv(5)=8!bv(12)=9!bv(19)=3!bv(26)=8!bv(33)=7!bv(40)=1!bv(47)=8
bv(6)=16!bv(13)=3!bv(20)=8!bv(27)=6!bv(34)=9!bv(41)=7!bv(48)=16
bv(14)=12!bv(21)=7!bv(28)=13!bv(35)=17!bv(42)=18
end if

if level = 6 then
'make first level data
bv(8)=6!bv(15)=5!bv(22)=12!bv(29)=9!bv(36)=14
bv(2)=9!bv(9)=2!bv(16)=3!bv(23)=3!bv(30)=3!bv(37)=3!bv(44)=9
bv(3)=6!bv(10)=3!bv(17)=3!bv(24)=3!bv(31)=4!bv(38)=3!bv(45)=6
bv(4)=13!bv(11)=3!bv(18)=4!bv(25)=4!bv(32)=2!bv(39)=4!bv(46)=13
bv(5)=6!bv(12)=3!bv(19)=4!bv(26)=3!bv(33)=4!bv(40)=2!bv(47)=6
bv(6)=12!bv(13)=2!bv(20)=2!bv(27)=2!bv(34)=4!bv(41)=4!bv(48)=12
bv(14)=6!bv(21)=5!bv(28)=12!bv(35)=9!bv(42)=14
end if

if level = 7 then
'make first level data
bv(8)=11!bv(15)=8!bv(22)=12!bv(29)=7!bv(36)=14
bv(2)=9!bv(9)=3!bv(16)=4!bv(23)=4!bv(30)=4!bv(37)=2!bv(44)=9
bv(3)=8!bv(10)=4!bv(17)=4!bv(24)=3!bv(31)=4!bv(38)=4!bv(45)=8
bv(4)=15!bv(11)=4!bv(18)=3!bv(25)=4!bv(32)=4!bv(39)=4!bv(46)=15
bv(5)=10!bv(12)=2!bv(19)=2!bv(26)=4!bv(33)=2!bv(40)=4!bv(47)=10
bv(6)=10!bv(13)=4!bv(20)=3!bv(27)=3!bv(34)=3!bv(41)=2!bv(48)=10
bv(14)=11!bv(21)=8!bv(28)=12!bv(35)=7!bv(42)=14
end if

if level = 8 then
'make first level data
bv(8)=13!bv(15)=15!bv(22)=18!bv(29)=10!bv(36)=30
bv(2)=19!bv(9)=4!bv(16)=8!bv(23)=7!bv(30)=6!bv(37)=8!bv(44)=19
bv(3)=14!bv(10)=9!bv(17)=8!bv(24)=4!bv(31)=1!bv(38)=9!bv(45)=14
bv(4)=21!bv(11)=7!bv(18)=6!bv(25)=3!bv(32)=8!bv(39)=7!bv(46)=21
bv(5)=13!bv(12)=7!bv(19)=4!bv(26)=6!bv(33)=4!bv(40)=9!bv(47)=13
bv(6)=19!bv(13)=6!bv(20)=5!bv(27)=7!bv(34)=1!bv(41)=6!bv(48)=19
bv(14)=13!bv(21)=15!bv(28)=18!bv(35)=10!bv(42)=30
end if

if level = 9 then
'make first level data
bv(8)=9!bv(15)=6!bv(22)=14!bv(29)=9!bv(36)=6
bv(2)=13!bv(9)=4!bv(16)=3!bv(23)=4!bv(30)=2!bv(37)=3!bv(44)=13
bv(3)=5!bv(10)=4!bv(17)=2!bv(24)=3!bv(31)=2!bv(38)=4!bv(45)=5
bv(4)=4!bv(11)=2!bv(18)=2!bv(25)=3!bv(32)=2!bv(39)=2!bv(46)=4
bv(5)=11!bv(12)=3!bv(19)=2!bv(26)=3!bv(33)=3!bv(40)=3!bv(47)=11
bv(6)=11!bv(13)=4!bv(20)=4!bv(27)=4!bv(34)=3!bv(41)=3!bv(48)=11
bv(14)=9!bv(21)=6!bv(28)=14!bv(35)=9!bv(42)=6
end if

if level = 10 then
'make first level data
bv(8)=7!bv(15)=11!bv(22)=13!bv(29)=8!bv(36)=4
bv(2)=5!bv(9)=3!bv(16)=2!bv(23)=2!bv(30)=4!bv(37)=4!bv(44)=5
bv(3)=10!bv(10)=2!bv(17)=2!bv(24)=4!bv(31)=4!bv(38)=4!bv(45)=10
bv(4)=6!bv(11)=4!bv(18)=4!bv(25)=2!bv(32)=3!bv(39)=3!bv(46)=6
bv(5)=11!bv(12)=4!bv(19)=4!bv(26)=3!bv(33)=2!bv(40)=2!bv(47)=11
bv(6)=11!bv(13)=2!bv(20)=3!bv(27)=4!bv(34)=2!bv(41)=2!bv(48)=11
bv(14)=7!bv(21)=11!bv(28)=13!bv(35)=8!bv(42)=4
end if

if level = 11 then
'make first level data
bv(8)=4!bv(15)=24!bv(22)=41!bv(29)=19!bv(36)=29
bv(2)=14!bv(9)=1!bv(16)=6!bv(23)=11!bv(30)=7!bv(37)=9!bv(44)=14
bv(3)=25!bv(10)=14!bv(17)=4!bv(24)=12!bv(31)=16!bv(38)=13!bv(45)=25
bv(4)=31!bv(11)=3!bv(18)=13!bv(25)=15!bv(32)=7!bv(39)=17!bv(46)=31
bv(5)=25!bv(12)=12!bv(19)=18!bv(26)=12!bv(33)=3!bv(40)=10!bv(47)=25
bv(6)=22!bv(13)=7!bv(20)=5!bv(27)=2!bv(34)=9!bv(41)=6!bv(48)=22
bv(14)=4!bv(21)=24!bv(28)=41!bv(35)=19!bv(42)=29
end if

if level = 12 then
'make first level data
bv(8)=8!bv(15)=8!bv(22)=15!bv(29)=8!bv(36)=6
bv(2)=10!bv(9)=3!bv(16)=2!bv(23)=4!bv(30)=2!bv(37)=2!bv(44)=10
bv(3)=0!bv(10)=3!bv(17)=2!bv(24)=2!bv(31)=3!bv(38)=2!bv(45)=0
bv(4)=10!bv(11)=4!bv(18)=3!bv(25)=4!bv(32)=4!bv(39)=2!bv(46)=10
bv(5)=16!bv(12)=4!bv(19)=4!bv(26)=4!bv(33)=4!bv(40)=3!bv(47)=16
bv(6)=9!bv(13)=2!bv(20)=2!bv(27)=3!bv(34)=2!bv(41)=2!bv(48)=9
bv(14)=8!bv(21)=8!bv(28)=15!bv(35)=8!bv(42)=6
end if

if level = 13 then
'make first level data
bv(8)=27!bv(15)=26!bv(22)=25!bv(29)=31!bv(36)=15
bv(2)=19!bv(9)=2!bv(16)=7!bv(23)=11!bv(30)=10!bv(37)=12!bv(44)=19
bv(3)=20!bv(10)=6!bv(17)=16!bv(24)=13!bv(31)=7!bv(38)=19!bv(45)=20
bv(4)=26!bv(11)=6!bv(18)=5!bv(25)=12!bv(32)=3!bv(39)=8!bv(46)=26
bv(5)=20!bv(12)=5!bv(19)=4!bv(26)=6!bv(33)=11!bv(40)=4!bv(47)=20
bv(6)=39!bv(13)=14!bv(20)=10!bv(27)=2!bv(34)=12!bv(41)=15!bv(48)=39
bv(14)=27!bv(21)=26!bv(28)=25!bv(35)=31!bv(42)=15
end if

if level = 14 then
'make first level data
bv(8)=39!bv(15)=5!bv(22)=47!bv(29)=36!bv(36)=51
bv(2)=53!bv(9)=11!bv(16)=4!bv(23)=16!bv(30)=7!bv(37)=19!bv(44)=53
bv(3)=49!bv(10)=11!bv(17)=6!bv(24)=19!bv(31)=19!bv(38)=16!bv(45)=49
bv(4)=20!bv(11)=13!bv(18)=5!bv(25)=8!bv(32)=7!bv(39)=8!bv(46)=20
bv(5)=38!bv(12)=17!bv(19)=1!bv(26)=4!bv(33)=3!bv(40)=17!bv(47)=38
bv(6)=18!bv(13)=14!bv(20)=2!bv(27)=15!bv(34)=3!bv(41)=15!bv(48)=18
bv(14)=39!bv(21)=5!bv(28)=47!bv(35)=36!bv(42)=51
end if

if level = 15 then
'make first level data
bv(8)=47!bv(15)=46!bv(22)=50!bv(29)=14!bv(36)=31
bv(2)=24!bv(9)=13!bv(16)=8!bv(23)=12!bv(30)=2!bv(37)=9!bv(44)=29
bv(3)=37!bv(10)=15!bv(17)=1!bv(24)=19!bv(31)=15!bv(38)=18!bv(45)=37
bv(4)=34!bv(11)=11!bv(18)=18!bv(25)=16!bv(32)=5!bv(39)=6!bv(46)=34
bv(5)=42!bv(12)=15!bv(19)=12!bv(26)=13!bv(33)=12!bv(40)=3!bv(47)=42
bv(6)=51!bv(13)=19!bv(20)=16!bv(27)=15!bv(34)=15!bv(41)=1!bv(48)=51
bv(14)=47!bv(21)=46!bv(28)=50!bv(35)=14!bv(42)=31
end if

levelnew=1

return


Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Sat May 12, 2018 9:12 am
by Henko
Hi Dav,
Nice and interesting game, reached level 3 already :mrgreen:
But far more interesting is the algorithm you're after. I'm going to look into that anyhow, along the following lines:
In case of a n by m grid, there are n.m unknowns (the numbers in the grid) and n+m lineair equations (the totals for each of the rows and columns. In the current case this means 10 equations with 25 unknowns.
Easily spoken, this means that 15 unknowns can be chosen freely, after which the remaining 10 numbers may be obtained by solving a set of 10 lineair equations with 10 variables.
However, there are restrictions: the solution must deliver only positive integers, and the selection of the 15 'free' numbers must be such that the set of equations is solvable (in math terms : does not result in a singular coefficient matrix).
A very interesting case to look into. Maybe a workable algorithm pops up, may be not.
If such algoritm exists for a 5 by 5 grid, then generalizing it to a n by m grid should not be too difficult. I'll keep you informed.

Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Sat May 12, 2018 11:08 am
by Dav
Hi Henko! Glad you like the game. As the levels get higher, they kind of get easier I think.

Thanks for the tip on figuring out an algorithm. My math skill is so weak, I would not know where to begin.

- Dav

Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Sun May 13, 2018 6:44 pm
by rbytes
I just finished level 8. This is an absorbing game! Thanks, Dav. 8-)

Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Tue May 15, 2018 11:40 am
by Henko
Hi Dav,

The math solution i had in mind, did not work, but i ended up with a rather simple function to generate feasible puzzles.
It's done by filling an n x m empty table (named "solve") with a couple of random numbers, leaving the rest of the table elements zero. The size of those numbers lie between "num_min" and "num_max". Then the row and column totals are calculated and stored in the arrays "rtot" and "ctot". That's the backbone of one puzzle. To hide the solution, the zero's in the table are replaced by dummy numbers that have nothing to do with the solution. The size of those numbers can be restricted between "dum_min" and "dum_max". The resulting table to be used for the presentation in the game is stored in the array "table".
Generally spoken, each of the generated puzzles may have more solutions than the one used to genrate the puzzle. It doesn't matter, if the check for correctness is done by just summing up the selected numbers and check them against the arrays "rtot" and "ctot"
There is some hocus pocus in the code to ensure enough randomness in the placement of the solution numbers in the table.

I expect that difficulty levels can be realized by the format/size of the table and possibly by playing with the "num_min" and "num_max" parameters.

Code: Select all

option base 1 ! randomize
nrow=5             ' number of rows of the table
ncol=5             ' number of columns of the table
num_min=1          ' minimum size of numbers used in the solution
num_max=4          ' maximum size of numbers used in the solution
dum_min=4          ' minimum size of dummy numbers in the table
dum_max=9          ' maximum size of the dummy numbers
dim rtot(nrow)     ' row totals 
dim ctot(ncol)     ' column totals
dim table(nrow,ncol) ' table filled with solution and dummy numbers
dim solve(nrow,ncol) ' table with solution numbers, others zero's

puzzle_gen(nrow,ncol,num_min,num_max,dum_min,dum_max,table,solve,rtot,ctot)

' display table() in game and play game

' check row and column totals of selected numbers 
      against rtot() and ctot()
end


def puzzle_gen(nr,nc,num_min,num_max,dum_min,dum_max,table(,),solve(,),rtot(),ctot())
dim jj(nc),ind(nc)
for i=1 to nr ! for j=1 to nc ! solve(i,j)=0 ! next j ! next i
for i=1 to nc ! jj(i)=i ! next i
for i=1 to nc ! r=1+rnd(nc)
  aux=jj(i) ! jj(i)=jj(r) ! jj(r)=aux
  next i
for i=1 to nr 
  for k=1 to nc ! ind(k)=jj(k) ! next k
  np=nc-floor(nc/2)
  for j=1 to np
    do ! m=1+rnd(nc) ! until ind(m)>0
    solve(i,ind(m))=num_min+rnd(num_max-num_min)
    ind(m)=0
    next j
  next i
for i=1 to nr
  rtot(i)=0 ! for j=1 to nc ! rtot(i)+=solve(i,j) ! next j
  next i
for j=1 to nc
  ctot(j)=0 ! for i=1 to nr ! ctot(j)+=solve(i,j) ! next i
  next j
for i=1 to nr ! for j=1 to nc
  if solve(i,j)>0 then
    table(i,j)=solve(i,j)
    else
    table(i,j)=dum_min+rnd(dum_max-dum_min)
    end if
  next j ! next i
end def

Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Tue May 15, 2018 1:49 pm
by Dav
Great work, Henko! Much appreciated. I'll implement that in a new version of the game and give option for larger grids.

I was trying to come up with an algorithm too. I only came up with a headache. :D

- Dav

Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Wed May 16, 2018 3:38 am
by Dav
Here's an updated version of the game, now using Henko's puzzle generating function. I left in the old versions method of computing totals, but I can eliminate that with the rtot() and ctot() variable set up with henko's function. I just didn't get around to it yet.

What's new: choose from 3 grid sizes, 5x5,6x6,7x7. Choose range of numbers to use 1 to 9, 2 to 4, and 1 to 19. You can change these anytime, just select the button. I removed level play - game is endless play now - there's enough math now to drive one loco.

MANY THANKS TO HENKO. You are a math guru!

- Dav

Code: Select all


'RoCoLoco.txt v1.1
'Row & Column number adding puzzle game
'Game mostly Coded by Dav, May/2018
'
'CREDITS to Henko. I'm using his puzzle_gen function
'to generate puzzle data on the fly. (thanks Henko)
'
'Touch red numbers inside grid to turn them on/off.
'Each row/column of red numbers must total the number
'on the edge of the grid. When they total up correctly
'the edge numbers will turn green. Turn all the edge
'numbers green to complete the level.
'
'You can change the grid size and numbers range.
'Default setting is 5x5 grid using number 1 to 9.


if lowstr$(device_type$())<>"ipad" then
  print "Sorry, game for iPad only right now."
  end
end if

randomize
option base 1
set orientation vertical
get screen size sw,sh

'defaults...

butcho=1 'default is 5x5 grid
numcho=1 'numbers 1-9

nrow=5             ' number of rows of the table
ncol=5             ' number of columns of the table
dum_max=10          ' use numbers 1-9
num_max=10          ' maximum size of numbers used in the 
num_min=1          ' minimum size of numbers used in the 
dum_min=1          ' minimum size of dummy numbers in the table

'===
top:
'===

dim rtot(nrow)     ' row totals 
dim ctot(ncol)     ' column totals
dim table(nrow,ncol) ' table filled with solution and dummy numbers
dim solve(nrow,ncol) ' table with s


graphics
graphics clear .3,.3,.3

draw color 0,0,0!fill color 1,1,1

grid=nrow+2 !size=sw/grid
dim bv(grid*grid),bx(grid*grid),by(grid*grid)
dim fc(grid*grid) 'flag if button on/off

set buttons custom
set buttons font size 54

'draw grid and number buttons
x=sw/5
draw color 0,0,0
if butcho=1 then fill color 1,1,1 else fill color .5,.5,.5
button "5x5" text "5x5" at x,1
if butcho=2 then fill color 1,1,1 else fill color .5,.5,.5
button "6x6" text "6x6" at x*2,1
if butcho=3 then fill color 1,1,1 else fill color .5,.5,.5
button "7x7" text "7x7" at x*3,1
if numcho=1 then fill color 1,1,1 else fill color .5,.5,.5
button "1-9" text "1-9" at x,sh-80
if numcho=2 then fill color 1,1,1 else fill color .5,.5,.5
button "2-4" text "2-4" at x*2,sh-80
if numcho=3 then fill color 1,1,1 else fill color .5,.5,.5
button "1-19" text "1-19" at x*3,sh-80

set buttons font size 74
if grid > 7 then set buttons font size 54
if grid > 9 then set buttons font size 34

'init button values, and generate x/y values...
bc=1
for r= 1 to grid
   for c = 1 to grid
     x=(r*size) ! y=(c*size)
     if bc=1 then goto skip  'dont draw corners...
     if bc=grid then goto skip
     if bc=grid*grid-grid+1 then goto skip
     if bc=grid*grid then goto skip
skip:
     bx(bc)=x-size!by(bc)=y!bv(bc)=bc!fc(bc)=1
     bc=bc+1
   next c
next r

gosub setlevel
gosub drawboard

do
  for t=2 to grid*grid-1
    if button_pressed(str$(t)) then
       'ignore outside rows
       if t < grid then break
       if t > (grid*grid)-grid then break
       for tt=1 to grid*grid step grid
          if t = tt then goto skip2
          if t = tt+(grid-1) then goto skip2
       next tt
       notes stop
       if fc(t)=1 then
          fc(t)=0!notes set "120:sa7"!notes play
       else
          fc(t)=1!notes set "120:sa6"!notes play
       end if
       gosub drawboard
       gosub checkforwin
    end if
    skip2:
  next t

  if button_pressed("5x5") then
     for d=1 to grid*grid
       button str$(d) delete
     next d
     butcho=1!nrow=5!ncol=5
     goto top
  end if

  if button_pressed("6x6") then
     for d=1 to grid*grid
       button str$(d) delete
     next d
     butcho=2!nrow=6!ncol=6
     goto top
  end if

  if button_pressed("7x7") then
     for d=1 to grid*grid
       button str$(d) delete
     next d
     butcho=3!nrow=7!ncol=7
     goto top
  end if

  if button_pressed("1-9") then
     numcho=1
     dum_max=10!num_max=10 
     num_min=1!dum_min=1  
     goto top
  end if

  if button_pressed("2-4") then
     numcho=2
     dum_max=5!num_max=5
     num_min=2!dum_min=2  
     goto top
  end if

  if button_pressed("1-19") then
     numcho=3
     dum_max=20!num_max=20
     num_min=1!dum_min=1
     goto top
  end if

until 0

end


'========
drawboard:
'========

if levelnew=1 then
   s$="" 'generate random notes
   for o = 1 to 32
      s$=s$&chr$(66+rnd(6))&str$(5+rnd(2))
   next o
   notes set "92:t"&s$ '23
   notes play
end if

'draw inside board
for t=2 to grid*grid-1
  'skip corners
  if t=grid then goto skipit
  if t =grid*grid-grid+1 then goto skipit
  'skip outside rows
  if t < grid then goto skipit
  if t > (grid*grid)-grid then goto skipit
  for tt=1 to grid*grid step grid
      if t = tt then goto skipit
      if t = tt+(grid-1) then goto skipit
  next tt
  if fc(t)=1 then 
     fill color 1,.5,.5
     draw color 1,1,1
  else
     fill color .3,.3,.3
     draw color 0,0,0
  end if
  button str$(t) text str$(bv(t)) at bx(t),by(t) size size,size
  if levelnew=1 then pause .015
  skipit:
next t

'compute and draw left/right rows
for t =2 to grid-1
   c=0
   for tt=1 to grid -2
     if fc(t+(grid*tt))= 1 then c=c+bv(t+(grid*tt))
   next tt
   draw color 0,0,0
   if bv(t)=c then
     fill color 0,1,0
     fc(t)=1!fc(t+(grid*(grid-1)))=1
   else
     fill color .5,.5,.5
     fc(t)=0!fc(t+(grid*(grid-1)))=0
   end if
   button str$(t) text str$(bv(t)) at bx(t),by(t) size size,size
   n=t+(grid*(grid-1))
   if levelnew=1 then pause .015
   button str$(n) text str$(bv(n)) at bx(n),by(n) size size,size
next t

'compute and draw top/bottom
for t=grid+1 to (grid*(grid-2)+1) step grid
   c=0
   for tt=1 to (grid-2)
      if fc(t+tt)=1 then c=c+bv(t+tt)
   next tt
   if bv(t)=c then
     fill color 0,1,0
     fc(t)=1!fc(t+(grid-1))=1
   else
     fill color .5,.5,.5
     fc(t)=0!fc(t+(grid-1))=0
   end if
   if levelnew then pause .015
   button str$(t) text str$(bv(t)) at bx(t),by(t) size size,size
n=t+(grid-1)
button str$(n) text str$(bv(n)) at bx(n),by(n) size size,size
next t
notes stop
levelnew=0
return


'==========
checkforwin:
'==========

'check down
for t=2 to grid-1
  if fc(t)=0 then goto nope
next t
'check across
for t=grid+1 to (grid*(grid-2)+1) step grid
  if fc(t)=0 then goto nope
next t

'made it here, must be completed
notes stop
notes set "7:s(c3eg)(e4cg3)(g4ec)(c5g4e)"
notes play 

pause 1

fill color 0,1,0
for i=2 to grid*grid-1
  if i=grid then goto skippp
  if i=grid*grid-grid+1 then goto skippp
  button str$(i) text chr$(9989) at bx(i),by(i) size size,size
  pause .015
  skippp:
next i

pause .5

for i=2 to grid*grid-1
   button str$(i) delete
   pause .015
next i

pause .5

goto top

nope:
return


'=======
setlevel:
'=======


puzzle_gen(nrow,ncol,num_min,num_max,dum_min,dum_max,table,solve,rtot,ctot)

'set inside grid
p=grid+2!pc=1
for r = 1 to grid-2
   for c = 1 to grid-2
    bv(p)=table(r,c)
    pc=pc+1!p=p+1
    if pc >ncol then
       p=p+2! pc=1
    end if
   next c
next r
      
'set top/bottom rows
r=1
for t=grid+1 to (grid*(grid-2)+1) step grid
   bv(t)=rtot(r)
   n=t+(grid-1)!bv(n)=rtot(r)
   r=r+1
next t

'set left/right columns
c=1
for t =2 to grid-1
   bv(t)=ctot(c)
   n=t+(grid*(grid-1))!bv(n)=ctot(c)
   c=c+1
next t

levelnew=1

return


def puzzle_gen(nr,nc,num_min,num_max,dum_min,dum_max,table(,),solve(,),rtot(),ctot())
dim jj(nc),ind(nc)
for i=1 to nr ! for j=1 to nc ! solve(i,j)=0 ! next j ! next i
for i=1 to nc ! jj(i)=i ! next i
for i=1 to nc ! r=1+rnd(nc)
  aux=jj(i) ! jj(i)=jj(r) ! jj(r)=aux
  next i
for i=1 to nr 
  for k=1 to nc ! ind(k)=jj(k) ! next k
  np=nc-floor(nc/2)
  for j=1 to np
    do ! m=1+rnd(nc) ! until ind(m)>0
    solve(i,ind(m))=num_min+rnd(num_max-num_min)
    ind(m)=0
    next j
  next i
for i=1 to nr
  rtot(i)=0 ! for j=1 to nc ! rtot(i)+=solve(i,j) ! next j
  next i
for j=1 to nc
  ctot(j)=0 ! for i=1 to nr ! ctot(j)+=solve(i,j) ! next i
  next j
for i=1 to nr ! for j=1 to nc
  if solve(i,j)>0 then
    table(i,j)=solve(i,j)
    else
    table(i,j)=dum_min+rnd(dum_max-dum_min)
    end if
  next j ! next i
end def


Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Tue May 22, 2018 10:09 pm
by MarkP
Dav,

Because of your well-designed code, your RoCoLoco program can easily be updated to run on an iPhone with only 3 changes:

Change the clause that tests for iPad to:

Code: Select all

if lowstr$(device_type$())<>"ipad" then iPhone=1
After the "font size 54" line, add:

Code: Select all

if iPhone then set buttons font size 18
After the "font size 34" line, add:

Code: Select all

if iPhone then set buttons font size 24
Done! It now runs great on an iPhone.

Re: RoCoLoco v1.0 - Number puzzle game (ipad)

Posted: Fri May 25, 2018 5:02 pm
by Dav
Thanks, MarkP. THat's a simple way to make it for all devices. :D

- Dav