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.
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$)