Page 1 of 1

Birth of a game

Posted: Sat Sep 15, 2018 10:22 am
by Henko
Found on internet. Interesting game.
This is just a working setup of the game, kind of template.
In a couple of days i hope to have coded the remaining functions.

Code: Select all

' digipuzzle   (from internet)
' rules of the game:   
' - fill each empty cell with a 0 or 1
' - at most two equal numbers next to each other (hor. or vert.)
' - each row and column must have an equal number of 0's and 1's
' - rows and columns must be unique
' touching a cell changes its content cylclic into blank, 0, and 1
'
option base 1
input "size of puzzle: ":N    ' = number of rows and columns
N=min(12,2*floor(N/2)) ! m=N/2 ! ds=60
dim v$(N,N)
gui_init()
do
  make_new_puzzle()
  do
    fld$=get_user_event$()
    event_handler(fld$)
    until puzzle_solved()
  until next_puzzle()
end

def gui_init()
graphics ! graphics clear 1,1,1 ! draw color 0,0,0 ! draw size 3
for i=0 to .N
  draw line 20,20+60*i to 20+60*.N,20+60*i
  draw line 20+60*i,20 to 20+60*i,20+60*.N
  next i
for i=1 to .N ! for j=1 to .N
  field i&"-"&j text "" at 21+60*(j-1),21+60*(i-1) size 58,58
  field i&"-"&j font size 30
  next j ! next i
end def

def make_new_puzzle()    ' generate a new valid digipuzzle
end def

def get_user_event$()    ' returns touched field or stop
end def

def event_handler(f$)    ' process puzzle progress
end def

def puzzle_solved()      ' check if correctly completed
end def

def next_puzzle()        ' returns 1 if user wants another one
end def

Re: Birth of a game

Posted: Tue Sep 18, 2018 1:11 pm
by Henko
This is the second and forelast version of the game.
I coded the functions, except the puzzel generating function, which turns out to be a really tough one. I need more time for that one.
Included is now one fixed puzzle, 10x10. The only variation is due to the placing of the empty cells.

Off topic: does anyone know how to stop the cursed (by me :twisted: ) automated text "corrections"?

149D1E33-C3B2-4942-AE71-AE1FEA873DFC.jpeg
149D1E33-C3B2-4942-AE71-AE1FEA873DFC.jpeg (685.3 KiB) Viewed 2179 times

Code: Select all

' digipuzzle   (from internet)
' rules of the game:   
' - fill each empty cell with a 0 or 1
' - at most two equal numbers next to each other (hor. or vert.)
' - each row and column must have an equal number of 0's and 1's
' - rows and columns must be unique
' touching a cell changes its content cylclic into blank, 0, and 1
'
option base 1
/*
input "size of puzzle: ":N   ' = number of rows and columns
N=max(4,min(12,2*floor(N/2))) ! m=N/2 ! ds=60
*/
N=10        ' fixed for testing purposes
p_empty=.5  ' fraction of initial empty cells
dim v$(N,N),sol(N,N),nop(N,N),p(N,N)
gui_init(N)
do
  make_new_puzzle(N)
  do
    fld$=get_user_event$(N) 
    event_handler(fld$)
    until puzzle_solved(N)
  until forever
end

def gui_init(n)
graphics ! graphics clear 1,1,1 ! draw color 0,0,0 ! draw size 3
for i=0 to n
  draw line 20,20+60*i to 20+60*n,20+60*i
  draw line 20+60*i,20 to 20+60*i,20+60*n
  next i
set buttons font size 30
for i=1 to n ! for j=1 to n
  button i&"-"&j text "" at 21+60*(j-1),21+60*(i-1) size 58,58
  next j ! next i
fw=400 ! fx=max(20,20+30*n-fw/2) ! fy=50+60*n
field "comment" text "" at fx,fy size fw,40
field "comment" back color .8,.8,.8
field "comment" font size 30 ! field "comment" font color 0,0,1
bw=120 ! bx= max(20,20+30*n-bw/2) ! by=120+60*n
button "stop" text "Quit" at bx,by size bw,40
end def

def make_new_puzzle(n)         ' fixed puzzle for test purposes
restore to test_puzzle
for i=1 to n ! for j=1 to n ! read .sol(i,j) ! next j ! next i
test_puzzle:
data 0,1,0,0,1,0,1,1,0,1
data 0,1,1,0,1,1,0,0,1,0
data 1,0,0,1,0,1,0,0,1,1
data 1,1,0,0,1,0,1,1,0,0
data 0,0,1,1,0,1,0,1,0,1
data 1,1,0,0,1,0,1,0,1,0
data 1,0,1,1,0,0,1,0,1,0
data 0,0,1,0,1,1,0,1,0,1
data 0,1,0,1,0,1,0,1,1,0
data 1,0,1,1,0,0,1,0,0,1
for i=1 to n ! for j=1 to n
  .p(i,j)=.sol(i,j) ! .nop(i,j)=1
  if rnd(1)<.p_empty then
    .p(i,j)=-1 ! .nop(i,j)=0 ! button i&"-"&j text ""
    else ! button i&"-"&j text " "&.p(i,j)
    end if
  next j ! next i
field "comment" text "    No proper solution yet"
end def

def get_user_event$(n)    ' returns touched button or stop or none
do slowdown
  for i=1 to n ! for j=1 to n
    if .nop(i,j) then continue
    f$= i&"-"&j
    if bp(f$) then return f$
    next j ! next i
  if bp("stop") then return "stop"
  until forever
end def

def event_handler(f$)    ' process puzzle progress
if f$="stop" then stop
pos=instr(f$,"-")
i=val(left$(f$,pos-1)) ! j=val(right$(f$,len(f$)-pos))
t=.p(i,j) ! t+=1 ! if t=2 then t=-1 ! .p(i,j)=t
if t=-1 then button f$ text "" 
if t=0  then button f$ text " "&0
if t=1  then button f$ text " "&1
end def

def puzzle_solved(n)         ' check if correctly completed
dim t$(n,n)                  ' copy of fields content
dim h0(n),h1(n),v0(n),v1(n)  ' row and column totals
for i=1 to n         ' check equal amounts of zeros and ones
  for j=1 to n
    t=.p(i,j) ! if t=-1 then return 0
    if t=0 then ! h0(i)+=1 ! v0(j)+=1
      else ! h1(i)+=1 ! v1(j)+=1
      end if
    next j
  next i
for i=1 to n
  if h0(i)<>5 or h1(i)<>5 or v0(i)<>5 or v1(i)<>5 then return 0
  next i
for i=1 to n
  c1=.p(i,1) ! c2=.p(i,2) ! d1=.p(1,i) ! d2=.p(2,i)
  for j=3 to n
    c3=.p(i,j) ! ctot=c1+c2+c3 ! d3=.p(i,j) ! dtot=c1+c2+c3
    if ctot=0 or ctot=3 or dtot=0 or dtot=3 then return 0
    c1=c2 ! c2=c3 ! d1=d2 ! d2=d3
    next j
  next i
field "comment" text "            Puzzle solved!" ! pause 2
field "comment" text "  Next puzzle in 2 seconds" ! pause 2
return 1
end def

def bp(a$) = button_pressed(a$)

Re: Birth of a game

Posted: Tue Sep 18, 2018 3:36 pm
by rbytes
I look forward to playing DigiPuzzle when it is ready.

Typing correction might be caused by one or more of three settings that I know of.

1. If Capsyntax is on, you can switch it off with SET CAPSYNTAX OFF.
2. In Settings on your device, look in the General folder and you will see a switch to turn Auto-Correction off.
3. In the same folder, there is a switch to turn Smart Punctuation off. As we have discovered, it isn't that smart when programming! :lol: