16 puzzle game: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(10 intermediate revisions by 5 users not shown)
Line 13:
</pre>
 
How to Play: The aim is to get the pieces back in order by clicking on the yellow arrows (choosing a location to rotate a row or column) to slide the pieces left or right, up or down. The Easy puzzle target is 3 moves, for the Hard puzzle it is 12 moves (or less!). Can it be that simple?
 
<pre>
1 14 3 4 1 2 3 4
5 2 7 8 --> 5 6 7 8
9 6 11 12 9 10 11 12
13 10 15 16 13 14 15 16
^
 
</pre>
The Easy puzzle target is 3 moves, for the Hard puzzle it is 12 moves (or less!). Can it be that simple?
 
;Task:
Line 31 ⟶ 41:
=={{header|AutoHotkey}}==
With Solver, See [[16_puzzle_game/autohotkey]]
 
=={{header|FreeBASIC}}==
{{trans|Wren}}
<syntaxhighlight lang="vb">Const easy = 1, hard = 4
Dim Shared As Byte n(1 To 16)
 
Sub initGrid ()
For i As Byte = 0 To 15
n(i) = i + 1
Next
End Sub
 
Sub rotate (ix() As Byte)
Dim As Byte last = n(ix(3))
For i As Byte = 3 To 1 Step -1
n(ix(i)) = n(ix(i-1))
Next
n(ix(0)) = last
End Sub
 
Function hasWon () As Boolean
For i As Byte = 0 To 15
If n(i) <> i+1 Then Return False
Next
Return True
End Function
 
Sub setDiff (level As Byte)
Dim As Byte moves = Iif(level = easy, 3, 12)
Dim As Byte rc(), j, i
For i = 0 To moves
Redim As Byte rc(20)
Dim As Byte r = Int(Rnd * 2)
Dim As Byte s = Int(Rnd * 4)
If r = 0 Then ' rotate random row
For j = s*4 To (s+1)*4
rc(j) = j
Next
Else ' rotate random column
For j = s To s+16 Step 4
rc(j) = j
Next
End If
rotate(rc())
If hasWon() Then ' do it again
i = -1
End If
i += 1
Next
Print Using !"\nTarget is ## moves."; moves
End Sub
 
Sub drawGrid ()
Print !"\n D1 D2 D3 D4"
Print " +-------------------+"
Print Using "R1 | ## | ## | ## | ## | L1"; n(0); n(1); n(2); n(3)
Print " |----+----+----+----|"
Print Using "R2 | ## | ## | ## | ## | L2"; n(4); n(5); n(6); n(7)
Print " |----+----+----+----|"
Print Using "R3 | ## | ## | ## | ## | L3"; n(8); n(9); n(10); n(11)
Print " |----+----+----+----|"
Print Using "R4 | ## | ## | ## | ## | L4"; n(12); n(13); n(14); n(15)
Print " +-------------------+"
Print !" U1 U2 U3 U4\n"
End Sub
 
'--- Programa Principal ---
Randomize Timer
Dim As Byte level = easy
Dim As String diff
Print "Enter difficulty level easy or hard E/H: ";
Do
Input ; "", diff
Loop Until Instr("eEhH", diff)
If Ucase(diff) = "H" Then level = hard
 
Cls
Print "Sixteen Puzzle"
initGrid()
setDiff(level)
 
Dim As Byte ix(0 To 3)
Print "When entering moves, you can also enter Q to quit or S to start again."
Dim As Byte c, moves = 0
Dim As String*2 move
Do
drawGrid()
If hasWon() Then
Print "Congratulations, you have won the game in"; moves; " moves!!"
While Inkey = "": Wend
Exit Do
End If
Do
Print "Moves so far = "; moves
Input "Enter move : ", move
Select Case Trim(Ucase(move))
Case "D1", "D2", "D3", "D4"
c = Cint(Right(move,1)) - 49
ix(0) = 0 + c
ix(1) = 4 + c
ix(2) = 8 + c
ix(3) = 12 + c
rotate(ix())
moves += 1
Exit Do
Case "L1", "L2", "L3", "L4"
c = Cint(Right(move,1)) - 49
ix(0) = 3 + 4*c
ix(1) = 2 + 4*c
ix(2) = 1 + 4*c
ix(3) = 0 + 4*c
rotate(ix())
moves += 1
Exit Do
Case "U1", "U2", "U3", "U4"
c = Cint(Right(move,1)) - 49
ix(0) = 12 + c
ix(1) = 8 + c
ix(2) = 4 + c
ix(3) = 0 + c
rotate(ix())
moves += 1
Exit Do
Case "R1", "R2", "R3", "R4"
c = Cint(Right(move,1)) - 49
ix(0) = 0 + 4*c
ix(1) = 1 + 4*c
ix(2) = 2 + 4*c
ix(3) = 3 + 4*c
rotate(ix())
moves += 1
Exit Do
Case "Q"
Exit Do, Do
Case "S"
Cls
initGrid()
setDiff(level)
moves = 0
Exit Do
Case Else
Print "Invalid move, try again."
End Select
Loop
Loop
'--------------------------
Sleep</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="future basic">
begin enum
_down = 1
_right
_up
_left
_new = 100
_restrt
_help
_end
end enum
str63 board, startPos, winBoard
 
void local fn init
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
for x = 1 to 16
winBoard += chr$(x)
next
board = winBoard
end fn
 
void 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
 
void local fn move( tag as int )
int r, d, rc = (tag mod 10)
select tag / 10
case _up : d = +4
case _right : d = -1 : rc *= 4
case _down : d = -4 : rc += 12
case else : d = +1 : rc = rc * 4 - 3
end select
for r = rc to rc + 2 * d step d
swap board[r], board[r+d]
next
if board == winBoard then window 1, @"!!! YOU WON !!!" : text,,fn colorRed
fn drawBoard
end fn
 
void local fn newGame
window 1, @"16 PUZZLE GAME"
int r
for r = 1 to 16
swap board[r], board[rnd(16)]
next
startPos = board
text @"Arial bold", 32, fn colorblue
fn drawBoard
end fn
 
void 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
 
void local fn doDialog(ev as long, tag as long)
select ev
case _btnClick : if tag < _new 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}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 210 ⟶ 481:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 283 ⟶ 554:
 
=={{header|J}}==
Assumes a recent release of jqt:<langsyntaxhighlight Jlang="j">require'ide/qt/gl2'
coinsert'jgl2'
 
Line 359 ⟶ 630:
 
NB. start:
game_reset_button''</langsyntaxhighlight>
 
=={{header|Javascript}}==
Line 450 ⟶ 721:
}
</pre>
<langsyntaxhighlight lang="javascript">
class Puzzle {
constructor() {
Line 604 ⟶ 875:
 
new Puzzle();
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Gtk, Random
 
function puzzle16app(bsize)
Line 717 ⟶ 988:
 
puzzle16app(4)
</syntaxhighlight>
</lang>
 
=={{header|Nim}}==
Line 723 ⟶ 994:
{{libheader|gintro}}
Not a direct translation as there are a lot of differences, but, at least, the graphical interface is similar and the logic is the same.
<langsyntaxhighlight Nimlang="nim">import random, sequtils, strutils
import gintro/[glib, gobject, gtk, gio]
 
Line 916 ⟶ 1,187:
let app = newApplication(PuzzleApp, "Rosetta.Puzzle16Game")
discard app.connect("activate", activate)
discard app.run()</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/16_Puzzle_Game
Line 1,006 ⟶ 1,277:
@again = @board;
$message = $steps = 0;
}</langsyntaxhighlight>
 
=={{header|Phix}}==
NB arrow keys not tested on linux, but "UDLR" should work...
 
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">level</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5<span style="color: #0000FF;">,</span>
<span style="color: #000000;">ESC<span style="color: #0000FF;">=<span style="color: #000000;">27<span style="color: #0000FF;">,</span> <span style="color: #000000;">UP<span style="color: #0000FF;">=<span style="color: #000000;">328<span style="color: #0000FF;">,</span> <span style="color: #000000;">DOWN<span style="color: #0000FF;">=<span style="color: #000000;">336<span style="color: #0000FF;">,</span> <span style="color: #000000;">LEFT<span style="color: #0000FF;">=<span style="color: #000000;">331<span style="color: #0000FF;">,</span> <span style="color: #000000;">RIGHT<span style="color: #0000FF;">=<span style="color: #000000;">333</span>
Line 1,081 ⟶ 1,352:
<span style="color: #008080;">if</span> <span style="color: #000000;">d<span style="color: #0000FF;">=<span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while
<!--</langsyntaxhighlight>-->
{{out}}
(a level 2 game)
Line 1,113 ⟶ 1,384:
For brevity, changed to zero-based and skipped some error handling.
{{trans|Go}}
<syntaxhighlight lang="raku" perl6line># 20210914 Raku programming solution
 
my (\easy,\hard) = 1,4 ; my @n = ^16; my \level = $ = easy ; my \moves = $ = 0;
Line 1,174 ⟶ 1,445:
default { say "\nInvalid move, try again." and moves-- }
}
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,182 ⟶ 1,453:
and the answer can be in any case (lower or uppercase).
<br>Not all errors are checked so as to keep the program simpler.
<langsyntaxhighlight lang="rexx">/*REXX pgm implements the 16 game; displays game grid, prompts for a move, game won? */
sep= copies("─",8); pad=left('',1+length(sep) ) /*pad=9 blanks. SEP is used for msgs.*/
parse arg N hard seed . /*obtain optional arguments from the CL*/
Line 1,268 ⟶ 1,539:
say pad ind bot; say;
say translate(col, '+', "-") ind ind ind "+ means shift a column up"; say
return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,323 ⟶ 1,594:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Sixteen Puzzle Game
 
Line 1,851 ⟶ 2,122:
psave()
return
</syntaxhighlight>
</lang>
Output image:
 
Line 1,858 ⟶ 2,129:
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-traititerate}}
{{libheader|Wren-fmt}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "random" for Random
import "./traititerate" for Stepped
import "./fmt" for Fmt
import "./ioutil" for Input
import "./str" for Str
 
var rand = Random.new()
Line 1,994 ⟶ 2,265:
}
}
}</langsyntaxhighlight>
 
{{out}}
9,476

edits