Anonymous user
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)
dim ds$(16) ' Board pieces
dim sh(3)
global x, y, z
call introAndLevel v
call buildBoard v
call printPuzzle
do
print "To move a piece, enter its number: "
input x
while isMoveValid() = 0
print "Wrong move."
call printPuzzle
print "To move a piece, enter its number: "
input x
wend
d(z) = x
d(y) = 0
call printPuzzle
loop until isPuzzleComplete()
print "YOU WON!"
end
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
cls
sh(
sh(
print "15 PUZZLE GAME"
print
print
print "Please enter level of difficulty,"
do
print "1 (easy), 2 (medium) or 3 (hard): ";
input level
loop while (level < 1) or (level > 3)
end sub
' Set pieces in correct order first
for p = 1 to 15
d(p) = p
next p
d(
z = 16 ' z = empty position
print
print "Shuffling pieces";
for n = 1 to sh(level)
print "
x = int(rnd(0) * 4) + 1
select case x
r = z - 4
case
r = z
case
r = z - 1
r = z + 1
end
loop while (r < 1) or (r > 16)
d(z) = d(r)
z = r
d(z) =
cls
end sub
function isMoveValid()
mv = 0
if (x
' 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
isMoveValid = mv
end function
function isPuzzleComplete()
pc = 0
p = 1
[lab1000]
if d(p) <> p then
p = p + 1
if p < 16 then [lab1000]
pc = 1
[labRetA]
isPuzzleComplete = pc
end function
</lang>
|