15 puzzle game: Difference between revisions

→‎{{header|Liberty BASIC}}: GOSUB statements changed into subroutine calls. Some GOTO statements changed into structured ones.
(→‎{{header|Liberty BASIC}}: It will be gradually changed into a structured version (without GOTO statement).)
(→‎{{header|Liberty BASIC}}: GOSUB statements changed into subroutine calls. Some GOTO statements changed into structured ones.)
Line 4,204:
' 15-PUZZLE GAME
' ********************************
dim d(16)
gosub [introAndLevel]
dim ds$(16) ' Board pieces
gosub [buildBoard]
dim sh(3)
[lab60]
global x, y, z
gosub [printPuzzle]
call introAndLevel v
[lab70]
call buildBoard v
print "To move a piece, enter its number: "
call printPuzzle
input x
do
gosub [checkIfMoveIsValid]
print "To move a piece, enter its number: "
if mv = 0 then
input x
print "Wrong move."
while isMoveValid() = 0
goto [lab60]
print "Wrong move."
end if
call printPuzzle
d(z) = x
print "To move a piece, enter its number: "
d(y) = 0
input x
gosub [printPuzzle]
wend
gosub [checkIfPuzzleComplete]
d(z) = x
if pc then [lab160]
d(y) = 0
goto [lab70]
call printPuzzle
[lab160]
loop until isPuzzleComplete()
print "YOU WON!"
end
 
[sub printPuzzle]
for p = 1 to 16
if d(p) = 0 then
ds$(p) = " "
else
s$ = str$(d(p))
ds$(p) = left$(" ", 3 - len(s$)) + s$ + " "
end if
next p
print "+-----+-----+-----+-----+"
print "|"; ds$(1); "|"; ds$(2); "|"; ds$(3); "|"; ds$(4); "|"
print "+-----+-----+-----+-----+"
print "|"; ds$(5); "|"; ds$(6); "|"; ds$(7); "|"; ds$(8); "|"
print "+-----+-----+-----+-----+"
print "|"; ds$(9); "|"; ds$(10); "|";ds$(11); "|"; ds$(12); "|"
print "+-----+-----+-----+-----+"
print "|"; ds$(13); "|"; ds$(14); "|"; ds$(15); "|"; ds$(16); "|"
print "+-----+-----+-----+-----+"
end sub
return
 
[sub introAndLevel] byref level
cls
dim sh(31) = 10
sh(12) = 1050
sh(23) = 50100
print "15 PUZZLE GAME"
sh(3) = 100
print
print "15 PUZZLE GAME"
print
print "Please enter level of difficulty,"
print
do
print "Please enter level of difficulty,"
print "1 (easy), 2 (medium) or 3 (hard): ";
[lab440]
input level
print "1 (easy), 2 (medium) or 3 (hard): ";
loop while (level < 1) or (level > 3)
input v
end sub
if (v < 1) or (v > 3) then [lab440]
return
 
[sub buildBoard] level
' Set pieces in correct order first
dim d(16)
for p = 1 to 15
dim ds$(16) ' Board pieces
d(p) = p
' Set pieces in correct order first
next p
for p = 1 to 15
d(p16) = p0 ' 0 = empty piece/slot
z = 16 ' z = empty position
next p
print
d(16) = 0 ' 0 = empty piece/slot
print "Shuffling pieces";
z = 16 ' z = empty position
for n = 1 to sh(level)
print
print "Shuffling pieces.";
for n = 1 to sh(v)do
x = int(rnd(0) * 4) + 1
print ".";
select case x
[lab610]
x = int(rnd(0) * 4) + case 1
r = z - 4
select case x
case 12
r = z -+ 4
case 23
r = if (z +- 1) mod 4 <> 0 then
r = z - 1
case 3
if (z - 1) modend 4 <> 0 thenif
rcase = z - 14
end if z mod 4 <> 0 then
r = z + 1
case 4
if z mod 4 <>end 0 thenif
end r = z + 1select
loop while (r < 1) or (r > 16)
end if
d(z) = d(r)
end select
z = r
if (r < 1) or (r > 16) then [lab610]
d(z) = d(r)0
znext = rn
cls
d(z) = 0
end sub
next n
cls
return
 
function isMoveValid()
[checkIfMoveIsValid]
mv = 0
if (x <>= 1) or (x ><= 15) then return
' Find position of piece x
p = 1
[lab800]
if d(p) = x then
y = p
goto [lab840]
end if
p = p + 1
if p > 16 then
print "UH OH!"
STOP
end if
goto [lab800]
' Find position of empty piece
[lab840]
p = 1
[lab850]
if d(p) = 0 then
z = p
goto [lab890]
end if
p = p + 1
if p > 16 then
print "UH OH!"
STOP
end if
goto [lab850]
print y; z
[lab890]
' Check if empty piece is above, below, left or right to piece x
if (y - 4 = z) or _
(y + 4 = z) or _
((y - 1 = z) and (z mod 4 <> 0)) or _
((y + 1 = z) and (y mod 4 <> 0)) then
mv = 1
end if
end if
return
isMoveValid = mv
end function
 
function isPuzzleComplete()
[checkIfPuzzleComplete]
pc = 0
p = 1
[lab1000]
if d(p) <> p then return[labRetA]
p = p + 1
if p < 16 then [lab1000]
pc = 1
[labRetA]
return
isPuzzleComplete = pc
end function
</lang>
 
Anonymous user