16 puzzle game: Difference between revisions

(Added FreeBasic)
Line 189:
Sleep</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="future basic">
begin enum
_new = 1
_restrt
_help
_end
_down = 1
_right
_up
_left
end enum
str63 board, startPos, winBoard
 
=local fn buildWnd
window 1,,(0,0,340,340)
int x
for x = 1 to 4
button 10+x,,,@"⬇️",(x*50+20,270,50,50),,NSBezelStyleTexturedSquare
ControlSetFontWithName( 10+x, @"Menlo", 32 )
button 30+x,,,@"⬆️",(x*50+20, 20,50,50),,NSBezelStyleTexturedSquare
ControlSetFontWithName( 30+x, @"Menlo", 32 )
button 25-x,,,@"➡️",( 20,x*50+20,50,50),,NSBezelStyleTexturedSquare
ControlSetFontWithName( 25-x, @"Menlo", 32 )
button 45-x,,,@"⬅️",(270,x*50+20,50,50),,NSBezelStyleTexturedSquare
ControlSetFontWithName( 45-x, @"Menlo", 32 )
next
button _new ,,,@"New", ( 20,270,50,50),,NSBezelStyleTexturedSquare
button _end ,,,@"Quit", ( 20, 20,50,50),,NSBezelStyleTexturedSquare
button _restrt,,,@"Redo", (270,270,50,50),,NSBezelStyleTexturedSquare
button _help ,,,@"Help", (270, 20,50,50),,NSBezelStyleTexturedSquare
end fn
 
local fn init
int r
for r = 1 to 16
winBoard += chr$(r)
next
board = winBoard
fn buildWnd
end fn
 
local fn drawBoard
int c = 1, x, y, z
cls
for y = 70 to 220 step 50
for x = 70 to 220 step 50
rect fill (x,y,48,48), fn coloryellow
if board[c] > 9 then z = x-3 else z = x+6
print %(z,y+6) str(board[c]);
c++
next
next
end fn
 
local fn move( tag as int )
int t, r, rc = (tag mod 10)
select tag / 10
case _left : rc*=4
mid$(board, rc-3, 4) = mid$(board, rc-2, 3) + mid$(board,rc -3,1)
case _right : rc*=4
mid$(board, rc-3, 4) = mid$(board, rc, 1) + mid$(board, rc - 3, 3)
case _up : t = board[rc]
for r = rc to 12 step 4
board[r] = board[r+4]
next
board[r] = t
case _down : t = board[rc]
t = board[rc+12]
for r = rc + 12 to 5 step -4
board[r] = board[r-4]
next
board[r] = t
end select
if board == winBoard then window 1, @"!!! YOU WON !!!" : text,,fn colorRed
fn drawBoard
end fn
 
local fn newGame
window 1, @"16 PUZZLE GAME"
int r, c, s
for r = 1 to 16
s = rnd(16)
c = board[s]
board[s] = board[r]
board[r] = c
next
startPos = board
text @"Arial bold", 32, fn colorblue
fn drawBoard
end fn
 
local fn ask( tag as long )
CFStringRef s1, s2 : int btn
select tag
case _help
s1 = @"Use the arrow buttons to move numbers in rows and columns.\n¬
Win by arranging the numbers in order, left to right, top to bottom:"
s2 = @" 1 2 3 4 \n 5 6 7 8 \n 9 10 11 12\n 13 14 15 16"
btn = alert 1, NSAlertStyleInformational, s1, s2, @"Okay"
case _new : if board == winBoard then fn newGame : exit fn
s1 = @"Leave this puzzle\nand start a new one?"
btn = alert 1, NSAlertStyleInformational, s1,,@"New puzzle;Cancel"
if btn == NSAlertFirstButtonReturn then fn newGame
case _restrt
s1 = @"Restart this puzzle\nfrom the beginning?"
btn = alert 1, NSAlertStyleInformational,s1,,@"Restart;Cancel"
if btn == NSAlertFirstButtonReturn then board = startPos : fn drawBoard
case _end
btn = alert 1, NSAlertStyleInformational,@"Quit 16 GAME?",,@"Quit;Cancel"
if btn == NSAlertFirstButtonReturn then end
end select
end fn
 
local fn doDialog(ev as long, tag as long)
select ev
case _btnClick : if tag > _end then fn move( tag ) else fn ask( tag )
case _windowWillClose : end
end select
end fn
 
on dialog fn doDialog
fn init
fn newGame
 
handleevents
</syntaxhighlight>
{{out}}
[[File:16 Puzzle Game.png]]
=={{header|Go}}==
<syntaxhighlight lang="go">package main
68

edits