RosettaCodeData/Task/15-puzzle-game/Craft-Basic/15-puzzle-game.basic
2023-07-01 13:44:08 -04:00

311 lines
3.3 KiB
Text

rem 15 Puzzle example game
rem written in Craft Basic
rem by Gemino Smothers 2023
rem www.lucidapogee.com
define size = 16, correct = 0, moves = 0
define click = 0, start = 0
dim list[size]
gosub setup
gosub game
end
sub setup
title "15 Puzzle"
bgcolor 0,128,0
cls graphics
resize 0, 0, 170, 270
center
let x = 0
let y = 30
for i = 0 to size - 1
if x = 112 then
let x = 0
let y = y + 25
endif
let x = x + 28
formid i + 1
formtext ""
buttonform x, y, 25, 20
next i
formid 17
formtext "
staticform 40, 130, 100, 20
bgcolor 0, 128, 0
fgcolor 255, 255, 0
colorform
formid 18
formtext ""
staticform 40, 150, 100, 20
bgcolor 0, 128, 0
fgcolor 255, 255, 0
colorform
formid 19
formtext "New"
buttonform 1, 1, 50, 20
formid 20
formtext "Help"
buttonform 55, 1, 50, 20
formid 21
formtext "About"
buttonform 110, 1, 50, 20
formid 22
formtext "Welcome."
staticform 40, 170, 120, 20
bgcolor 0, 128, 0
fgcolor 255, 255, 0
colorform
return
sub shuffle
let start = 1
formid 22
formtext "shuffling..."
updateform
for i = 0 to size - 1
formid i + 1
formtext ""
updateform
let list[i] = 0
next i
let t = 0
let i = 0
do
if i = 14 then
let n = 120 - t
formid i + 1
formtext n
updateform
let list[i] = n
break
endif
for f = 0 to size - 1
let n = int(rnd * 15) + 1
let s = 0
for c = 0 to i - 1
if n = list[c] then
let s = 1
break c
endif
next c
if s = 0 and list[i] = 0 then
formid i + 1
formtext n
updateform
let list[i] = n
let t = t + n
let i = i + 1
endif
wait
next f
loop i < size - 1
formid 22
formtext ""
updateform
return
sub game
do
let click = forms
if click > 0 and click < 17 and start = 1 then
let moves = moves + 1
formid 17
formtext "Moves: ", moves
updateform
gosub checkspaces
gosub checkorder
endif
if click = 19 then
gosub shuffle
let moves = 0
let correct = 0
formid 17
formtext "Moves:"
updateform
formid 18
formtext "Correct:"
updateform
endif
if click = 20 then
alert "Click the numbers to move them in the correct order."
endif
if click = 21 then
alert "15 Puzzle", newline, "by Gemino Smothers 2023 ", newline, " www.lucidapogee.com"
endif
button k, 27
wait
loop k = 0
return
sub checkspaces
let click = click - 1
let top = click - 4
let right = click + 1
let bottom = click + 4
let left = click - 1
if top >= 0 then
if list[top] = 0 then
let n = top
gosub swap
endif
endif
if right <= size - 1 then
if list[right] = 0 then
let n = right
gosub swap
endif
endif
if bottom <= size - 1 then
if list[bottom] = 0 then
let n = bottom
gosub swap
endif
endif
if left >= 0 then
if list[left] = 0 then
let n = left
gosub swap
endif
endif
return
sub swap
let t = list[click]
let list[n] = list[click]
let list[click] = 0
let click = click + 1
formid click
formtext ""
updateform
let n = n + 1
formid n
formtext t
updateform
return
sub checkorder
let correct = 0
for i = 0 to size - 2
if list[i] = i + 1 then
let correct = correct + 1
endif
next i
formid 18
formtext "Correct: ", correct
updateform
if correct = size - 1 then
wait
alert "You win! Moves: ", moves
endif
return