15 puzzle game: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
Line 27: Line 27:
{{trans|Python: Original, with output}}
{{trans|Python: Original, with output}}


<syntaxhighlight lang=11l>T Puzzle
<syntaxhighlight lang="11l">T Puzzle
position = 0
position = 0
[Int = String] items
[Int = String] items
Line 149: Line 149:
This is an entire Sega Genesis game, tested in the Fusion emulator. Thanks to Keith S. of Chibiakumas for the cartridge header, font routines, and printing logic. I programmed the actual game logic. This code can be copied and pasted into a text file and assembled as-is using vasmm68k_mot_win32.exe, no includes or incbins necessary (even the bitmap font is here too.)
This is an entire Sega Genesis game, tested in the Fusion emulator. Thanks to Keith S. of Chibiakumas for the cartridge header, font routines, and printing logic. I programmed the actual game logic. This code can be copied and pasted into a text file and assembled as-is using vasmm68k_mot_win32.exe, no includes or incbins necessary (even the bitmap font is here too.)


<syntaxhighlight lang=68000devpac>;15 PUZZLE GAME
<syntaxhighlight lang="68000devpac">;15 PUZZLE GAME
;Ram Variables
;Ram Variables
Cursor_X equ $00FF0000 ;Ram for Cursor Xpos
Cursor_X equ $00FF0000 ;Ram for Cursor Xpos
Line 811: Line 811:
=={{header|AArch64 Assembly}}==
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang=AArch64 Assembly>
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program puzzle15_64.s */
/* program puzzle15_64.s */
Line 1,435: Line 1,435:


=={{header|Action!}}==
=={{header|Action!}}==
<syntaxhighlight lang=Action!>DEFINE BOARDSIZE="16"
<syntaxhighlight lang="action!">DEFINE BOARDSIZE="16"
DEFINE X0="13"
DEFINE X0="13"
DEFINE Y0="6"
DEFINE Y0="6"
Line 1,656: Line 1,656:
We fist define a generic package Generic_Puzzle. Upon instantiation, it can take any number of rows, any number of columns for a rows*columns-1 game. Instead of plain numbers, the tiles on the board can have arbitrary names (but they should all be of the same length). The package user can request the name for the tile at a certain (row,column)-point, and the set of possible moves. The user can move the empty space up, down, left and right (if possible). If the user makes the attempt to perform an impossible move, a Constraint_Error is raised.
We fist define a generic package Generic_Puzzle. Upon instantiation, it can take any number of rows, any number of columns for a rows*columns-1 game. Instead of plain numbers, the tiles on the board can have arbitrary names (but they should all be of the same length). The package user can request the name for the tile at a certain (row,column)-point, and the set of possible moves. The user can move the empty space up, down, left and right (if possible). If the user makes the attempt to perform an impossible move, a Constraint_Error is raised.


<syntaxhighlight lang=Ada>generic
<syntaxhighlight lang="ada">generic
Rows, Cols: Positive;
Rows, Cols: Positive;
with function Name(N: Natural) return String; -- with Pre => (N < Rows*Cols);
with function Name(N: Natural) return String; -- with Pre => (N < Rows*Cols);
Line 1,675: Line 1,675:
The package implementation is as follows.
The package implementation is as follows.


<syntaxhighlight lang=Ada>package body Generic_Puzzle is
<syntaxhighlight lang="ada">package body Generic_Puzzle is
Field: array(Row_Type, Col_Type) of Natural;
Field: array(Row_Type, Col_Type) of Natural;
Line 1,735: Line 1,735:
The main program reads the level from the command line. A larger level implies a more difficult instance. The default level is 10, which is fairly simple. After randomizing the board, the user can move the tiles.
The main program reads the level from the command line. A larger level implies a more difficult instance. The default level is 10, which is fairly simple. After randomizing the board, the user can move the tiles.


<syntaxhighlight lang=Ada>with Generic_Puzzle, Ada.Text_IO,
<syntaxhighlight lang="ada">with Generic_Puzzle, Ada.Text_IO,
Ada.Numerics.Discrete_Random, Ada.Command_Line;
Ada.Numerics.Discrete_Random, Ada.Command_Line;


Line 1,848: Line 1,848:
4 moves!</pre>
4 moves!</pre>


For other puzzles, one must just the single line with the package instantiation. E.g., for an 8-puzzle, we would write the following. <syntaxhighlight lang=Ada> package Puzzle is new Generic_Puzzle(Rows => 3, Cols => 3, Name => Image);</syntaxhighlight>
For other puzzles, one must just the single line with the package instantiation. E.g., for an 8-puzzle, we would write the following. <syntaxhighlight lang="ada"> package Puzzle is new Generic_Puzzle(Rows => 3, Cols => 3, Name => Image);</syntaxhighlight>


=={{header|APL}}==
=={{header|APL}}==
{{works with|Dyalog APL|16.0}}
{{works with|Dyalog APL|16.0}}
<syntaxhighlight lang=APL>fpg←{⎕IO←0
<syntaxhighlight lang="apl">fpg←{⎕IO←0
⍺←4 4
⍺←4 4
(s∨.<0)∨2≠⍴s←⍺:'invalid shape:'s
(s∨.<0)∨2≠⍴s←⍺:'invalid shape:'s
Line 1,925: Line 1,925:
=={{header|ARM Assembly}}==
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang=ARM Assembly>
<syntaxhighlight lang="arm assembly">


/* ARM assembly Raspberry PI */
/* ARM assembly Raspberry PI */
Line 2,566: Line 2,566:


=={{header|Astro}}==
=={{header|Astro}}==
<syntaxhighlight lang=python>type Puzzle(var items: {}, var position: -1)
<syntaxhighlight lang="python">type Puzzle(var items: {}, var position: -1)


fun mainframe(puz):
fun mainframe(puz):
Line 2,683: Line 2,683:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey>Size := 20
<syntaxhighlight lang="autohotkey">Size := 20
Grid := [], Deltas := ["-1,0","1,0","0,-1","0,1"], Width := Size * 2.5
Grid := [], Deltas := ["-1,0","1,0","0,-1","0,1"], Width := Size * 2.5
Gui, font, S%Size%
Gui, font, S%Size%
Line 2,764: Line 2,764:


==={{header|Commodore BASIC}}===
==={{header|Commodore BASIC}}===
<syntaxhighlight lang=basic>10 REM 15-PUZZLE GAME
<syntaxhighlight lang="basic">10 REM 15-PUZZLE GAME
20 REM COMMODORE BASIC 2.0
20 REM COMMODORE BASIC 2.0
30 REM ********************************
30 REM ********************************
Line 2,882: Line 2,882:
=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}{{works with|ARM BBC BASIC}}{{works with|Brandy BASIC|Matrix Brandy}}
{{works with|BBC BASIC for Windows}}{{works with|ARM BBC BASIC}}{{works with|Brandy BASIC|Matrix Brandy}}
<syntaxhighlight lang=bbcbasic> IF INKEY(-256)=77 OR (INKEY(-256) AND &F0)=&A0 THEN MODE 1: COLOUR 0: COLOUR 143: *FX4,1
<syntaxhighlight lang="bbcbasic"> IF INKEY(-256)=77 OR (INKEY(-256) AND &F0)=&A0 THEN MODE 1: COLOUR 0: COLOUR 143: *FX4,1


SIZE=4 : DIFFICULTY=3
SIZE=4 : DIFFICULTY=3
Line 2,930: Line 2,930:
=={{header|BQN}}==
=={{header|BQN}}==
{{trans|APL}}
{{trans|APL}}
<syntaxhighlight lang=bqn>_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
<syntaxhighlight lang="bqn">_while_ ← {𝔽⍟𝔾∘𝔽_𝕣_𝔾∘𝔽⍟𝔾𝕩}
FPG←{
FPG←{
𝕊𝕩: 4‿4𝕊𝕩;
𝕊𝕩: 4‿4𝕊𝕩;
Line 2,972: Line 2,972:
@
@
}</syntaxhighlight>
}</syntaxhighlight>
<syntaxhighlight lang=bqn> )ex 15_puzzle.bqn
<syntaxhighlight lang="bqn"> )ex 15_puzzle.bqn
FPG 10
FPG 10
┌─
┌─
Line 3,007: Line 3,007:
===C89, 22 lines version===
===C89, 22 lines version===
The task, as you can see, can be resolved in 22 lines of no more than 80 characters. Of course, the source code in C is not very readable. The second example works exactly the same way, but it was written in much more human readable way. The program also works correctly for non-standard number of rows and/or columns.
The task, as you can see, can be resolved in 22 lines of no more than 80 characters. Of course, the source code in C is not very readable. The second example works exactly the same way, but it was written in much more human readable way. The program also works correctly for non-standard number of rows and/or columns.
<syntaxhighlight lang=C>/* RosettaCode: Fifteen puzle game, C89, plain vanillia TTY, MVC, § 22 */
<syntaxhighlight lang="c">/* RosettaCode: Fifteen puzle game, C89, plain vanillia TTY, MVC, § 22 */
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdio.h>
Line 3,031: Line 3,031:


===C89, short version, TTY mode===
===C89, short version, TTY mode===
<syntaxhighlight lang=C>/*
<syntaxhighlight lang="c">/*
* RosettaCode: Fifteen puzle game, C89, plain vanillia TTY, MVC
* RosettaCode: Fifteen puzle game, C89, plain vanillia TTY, MVC
*/
*/
Line 3,186: Line 3,186:


===C89, long version, TTY/Winapi/ncurses modes===
===C89, long version, TTY/Winapi/ncurses modes===
<syntaxhighlight lang=C>/**
<syntaxhighlight lang="c">/**
* RosettaCode: Fifteen puzle game, C89, MS Windows Console API, MVC
* RosettaCode: Fifteen puzle game, C89, MS Windows Console API, MVC
*
*
Line 3,502: Line 3,502:
{{libheader|System.Drawing}}
{{libheader|System.Drawing}}
{{works with|C sharp|6}}
{{works with|C sharp|6}}
<syntaxhighlight lang=csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Drawing;
using System.Linq;
using System.Linq;
Line 3,664: Line 3,664:


=={{header|C++}}==
=={{header|C++}}==
<syntaxhighlight lang=cpp>
<syntaxhighlight lang="cpp">
#include <time.h>
#include <time.h>
#include <stdlib.h>
#include <stdlib.h>
Line 3,777: Line 3,777:
Tested with GnuCOBOL
Tested with GnuCOBOL


<syntaxhighlight lang=cobol> >>SOURCE FORMAT FREE
<syntaxhighlight lang="cobol"> >>SOURCE FORMAT FREE
*> This code is dedicated to the public domain
*> This code is dedicated to the public domain
*> This is GNUCOBOL 2.0
*> This is GNUCOBOL 2.0
Line 3,946: Line 3,946:
Credit to this post for help with the inversions-counting function: [http://www.lispforum.com/viewtopic.php?f=2&t=3422]
Credit to this post for help with the inversions-counting function: [http://www.lispforum.com/viewtopic.php?f=2&t=3422]


Run it (after loading the file) with <syntaxhighlight lang=lisp>|15|::main</syntaxhighlight>.
Run it (after loading the file) with <syntaxhighlight lang="lisp">|15|::main</syntaxhighlight>.


<syntaxhighlight lang=lisp>(defpackage :15
<syntaxhighlight lang="lisp">(defpackage :15
(:use :common-lisp))
(:use :common-lisp))
(in-package :15)
(in-package :15)
Line 4,052: Line 4,052:
[https://easylang.online/apps/15-puzzle.html Run it]
[https://easylang.online/apps/15-puzzle.html Run it]


<lang>background 432
<syntaxhighlight lang="text">background 432
textsize 13
textsize 13
len f[] 16
len f[] 16
Line 4,136: Line 4,136:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang=fsharp>
<syntaxhighlight lang="fsharp">
// 15 Puzzle Game. Nigel Galloway: August 9th., 2020
// 15 Puzzle Game. Nigel Galloway: August 9th., 2020
let Nr,Nc,RA,rnd=[|3;0;0;0;0;1;1;1;1;2;2;2;2;3;3;3|],[|3;0;1;2;3;0;1;2;3;0;1;2;3;0;1;2|],[|for n in [1..16]->n%16|],System.Random()
let Nr,Nc,RA,rnd=[|3;0;0;0;0;1;1;1;1;2;2;2;2;3;3;3|],[|3;0;1;2;3;0;1;2;3;0;1;2;3;0;1;2|],[|for n in [1..16]->n%16|],System.Random()
Line 4,186: Line 4,186:
</pre>
</pre>
=={{header|Factor}}==
=={{header|Factor}}==
<syntaxhighlight lang=factor>USING: accessors combinators combinators.extras
<syntaxhighlight lang="factor">USING: accessors combinators combinators.extras
combinators.short-circuit grouping io kernel literals math
combinators.short-circuit grouping io kernel literals math
math.matrices math.order math.parser math.vectors prettyprint qw
math.matrices math.order math.parser math.vectors prettyprint qw
Line 4,297: Line 4,297:
See [[15_puzzle_solver#Forth]] for a solver based on the same code.
See [[15_puzzle_solver#Forth]] for a solver based on the same code.


<syntaxhighlight lang=forth>#! /usr/bin/gforth
<syntaxhighlight lang="forth">#! /usr/bin/gforth


cell 8 <> [if] s" 64-bit system required" exception throw [then]
cell 8 <> [if] s" 64-bit system required" exception throw [then]
Line 4,379: Line 4,379:
The game plan is to start with an ordered array so that each cell definitely has a unique code, then jumble them via "random" swaps. Possible arrangements turn out to have either odd or even parity based on the number of out-of-sequence squares, and as the allowed transformations do not change the parity and the solution state has even parity, odd parity starting states should not be presented except by those following Franz Kafka. The calculation is simplified by always having the blank square in the last position, thus in the last row. Once an even-parity starting state is floundered upon, the blank square is re-positioned using allowable moves so that the parity is not altered thereby. Then the game begins: single-square moves only are considered, though in practice groups of squares could be moved horizontally or vertically rather than one-step-at-a-time - a possible extension.
The game plan is to start with an ordered array so that each cell definitely has a unique code, then jumble them via "random" swaps. Possible arrangements turn out to have either odd or even parity based on the number of out-of-sequence squares, and as the allowed transformations do not change the parity and the solution state has even parity, odd parity starting states should not be presented except by those following Franz Kafka. The calculation is simplified by always having the blank square in the last position, thus in the last row. Once an even-parity starting state is floundered upon, the blank square is re-positioned using allowable moves so that the parity is not altered thereby. Then the game begins: single-square moves only are considered, though in practice groups of squares could be moved horizontally or vertically rather than one-step-at-a-time - a possible extension.


The source style uses F90 for its array arithmetic abilities, especially the functions ALL, ANY and COUNT. A statement <syntaxhighlight lang=Fortran>LOCZ = MINLOC(BOARD) !Find the zero. 0 = BOARD(LOCZ(1),LOCZ(2)) == BOARD(ZC,ZR)</syntaxhighlight> could be used but is unnecessary thanks to tricks with EQUIVALENCE. For earlier Fortran, various explicit DO-loops would have to be used. This would at least make clear whether or not the equivalents of ANY and ALL terminated on the first failure or doggedly scanned the entire array no matter what. <syntaxhighlight lang=Fortran> SUBROUTINE SWAP(I,J) !Alas, furrytran does not provide this.
The source style uses F90 for its array arithmetic abilities, especially the functions ALL, ANY and COUNT. A statement <syntaxhighlight lang="fortran">LOCZ = MINLOC(BOARD) !Find the zero. 0 = BOARD(LOCZ(1),LOCZ(2)) == BOARD(ZC,ZR)</syntaxhighlight> could be used but is unnecessary thanks to tricks with EQUIVALENCE. For earlier Fortran, various explicit DO-loops would have to be used. This would at least make clear whether or not the equivalents of ANY and ALL terminated on the first failure or doggedly scanned the entire array no matter what. <syntaxhighlight lang="fortran"> SUBROUTINE SWAP(I,J) !Alas, furrytran does not provide this.
INTEGER I,J,T !So, we're stuck with supplying the obvious.
INTEGER I,J,T !So, we're stuck with supplying the obvious.
T = I !And, only for one type at a go.
T = I !And, only for one type at a go.
Line 4,529: Line 4,529:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang=freebasic>sub drawboard( B() as ubyte )
<syntaxhighlight lang="freebasic">sub drawboard( B() as ubyte )
dim as string outstr = ""
dim as string outstr = ""
for i as ubyte = 0 to 15
for i as ubyte = 0 to 15
Line 4,605: Line 4,605:


=={{header|Gambas}}==
=={{header|Gambas}}==
<syntaxhighlight lang=gambas>'Charlie Ogier (C) 15PuzzleGame 24/04/2017 V0.1.0 Licenced under MIT
<syntaxhighlight lang="gambas">'Charlie Ogier (C) 15PuzzleGame 24/04/2017 V0.1.0 Licenced under MIT
'Inspiration came from: -
'Inspiration came from: -
''http://rosettacode.org/wiki/15_Puzzle_Game
''http://rosettacode.org/wiki/15_Puzzle_Game
Line 4,908: Line 4,908:


=={{header|Go}}==
=={{header|Go}}==
<syntaxhighlight lang=go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 5,064: Line 5,064:


=={{header|Harbour}}==
=={{header|Harbour}}==
<syntaxhighlight lang=Harbour>
<syntaxhighlight lang="harbour">


#include "inkey.ch"
#include "inkey.ch"
Line 5,252: Line 5,252:


=={{header|Haskell}}==
=={{header|Haskell}}==
<syntaxhighlight lang=haskell>import Data.Array
<syntaxhighlight lang="haskell">import Data.Array
import System.Random
import System.Random


Line 5,354: Line 5,354:
Implementation:
Implementation:


<syntaxhighlight lang=J>require'general/misc/prompt'
<syntaxhighlight lang="j">require'general/misc/prompt'


genboard=:3 :0
genboard=:3 :0
Line 5,424: Line 5,424:
A full game would be too big to be worth showing here, so for the purpose of giving a glimpse of what this looks like in action we replace the random number generator with a constant:
A full game would be too big to be worth showing here, so for the purpose of giving a glimpse of what this looks like in action we replace the random number generator with a constant:


<syntaxhighlight lang=J> game''
<syntaxhighlight lang="j"> game''
15 puzzle
15 puzzle
h for help, q to quit
h for help, q to quit
Line 5,465: Line 5,465:
=={{header|Java}}==
=={{header|Java}}==
{{works with|Java|8}}
{{works with|Java|8}}
<syntaxhighlight lang=java>package fifteenpuzzle;
<syntaxhighlight lang="java">package fifteenpuzzle;


import java.awt.*;
import java.awt.*;
Line 5,682: Line 5,682:
=={{header|Javascript}}==
=={{header|Javascript}}==
Play it [http://paulo-jorente.de/webgames/15p/ here]
Play it [http://paulo-jorente.de/webgames/15p/ here]
<syntaxhighlight lang=javascript>
<syntaxhighlight lang="javascript">
var board, zx, zy, clicks, possibles, clickCounter, oldzx = -1, oldzy = -1;
var board, zx, zy, clicks, possibles, clickCounter, oldzx = -1, oldzy = -1;
function getPossibles() {
function getPossibles() {
Line 5,820: Line 5,820:


=={{header|Julia}}==
=={{header|Julia}}==
<syntaxhighlight lang=julia>
<syntaxhighlight lang="julia">
using Random
using Random


Line 5,948: Line 5,948:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<syntaxhighlight lang=scala>// version 1.1.3
<syntaxhighlight lang="scala">// version 1.1.3


import java.awt.BorderLayout
import java.awt.BorderLayout
Line 6,101: Line 6,101:
{{trans|Commodore BASIC}}
{{trans|Commodore BASIC}}
{{works with|Just BASIC}}
{{works with|Just BASIC}}
<syntaxhighlight lang=lb>
<syntaxhighlight lang="lb">
' 15-PUZZLE GAME
' 15-PUZZLE GAME
' ********************************
' ********************************
Line 6,238: Line 6,238:


=={{header|LiveCode}}==
=={{header|LiveCode}}==
<syntaxhighlight lang=liveCode>
<syntaxhighlight lang="livecode">
#Please note that all this code can be performed in livecode with just few mouse clicks
#Please note that all this code can be performed in livecode with just few mouse clicks
#This is just a pure script exampe
#This is just a pure script exampe
Line 6,305: Line 6,305:


=={{header|Lua}}==
=={{header|Lua}}==
<syntaxhighlight lang=lua>
<syntaxhighlight lang="lua">
math.randomseed( os.time() )
math.randomseed( os.time() )
local puz = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 }
local puz = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0 }
Line 6,434: Line 6,434:
Also the code is not the best, because we can move from 4 position to 5 (we can't do that with real puzzle)
Also the code is not the best, because we can move from 4 position to 5 (we can't do that with real puzzle)


<syntaxhighlight lang=M2000 Interpreter>
<syntaxhighlight lang="m2000 interpreter">
Module Puzzle15 {
Module Puzzle15 {
Line 6,550: Line 6,550:


=={{header|Mathematica}} / {{header|Wolfram Language}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang=mathematica>grid = MapThread[{#1,#2} &, {Range @ 16, Range @ 16}]
<syntaxhighlight lang="mathematica">grid = MapThread[{#1,#2} &, {Range @ 16, Range @ 16}]


Move[x_] := (empty = Select[grid, #[[1]]==16 &][[1,2]];
Move[x_] := (empty = Select[grid, #[[1]]==16 &][[1,2]];
Line 6,568: Line 6,568:
The ideal in Mercury is to have a declarative module that encodes the game logic, and then separate modules to implement a human player with text commands (here), or keyed commands, or some kind of AI player, and so on. fifteen.print/3 is a arguably a smudge on fifteen's interface:
The ideal in Mercury is to have a declarative module that encodes the game logic, and then separate modules to implement a human player with text commands (here), or keyed commands, or some kind of AI player, and so on. fifteen.print/3 is a arguably a smudge on fifteen's interface:
<syntaxhighlight lang=Mercury>:- module fifteen.
<syntaxhighlight lang="mercury">:- module fifteen.
:- interface.
:- interface.
:- use_module random, io.
:- use_module random, io.
Line 6,673: Line 6,673:
As used:
As used:


<syntaxhighlight lang=Mercury>:- module play_fifteen.
<syntaxhighlight lang="mercury">:- module play_fifteen.
:- interface.
:- interface.
:- import_module io.
:- import_module io.
Line 6,802: Line 6,802:


=={{header|Nim}}==
=={{header|Nim}}==
<syntaxhighlight lang=nim>import random, terminal
<syntaxhighlight lang="nim">import random, terminal


type
type
Line 6,958: Line 6,958:


=={{header|OCaml}}==
=={{header|OCaml}}==
<syntaxhighlight lang=ocaml>module Puzzle =
<syntaxhighlight lang="ocaml">module Puzzle =
struct
struct
type t = int array
type t = int array
Line 7,046: Line 7,046:
=={{header|Pascal}}==
=={{header|Pascal}}==
This is Free Pascal(version >= 3.0.4) text mode implementation. To make a move, the user needs to enter the number of the selected tile.
This is Free Pascal(version >= 3.0.4) text mode implementation. To make a move, the user needs to enter the number of the selected tile.
<syntaxhighlight lang=Pascal>
<syntaxhighlight lang="pascal">
program fifteen;
program fifteen;
{$mode objfpc}
{$mode objfpc}
Line 7,345: Line 7,345:
On verbosity shows how the solvability is calculated. The program has some extra feature like font size and color scheme but also the possibility to set the intial board disposition.
On verbosity shows how the solvability is calculated. The program has some extra feature like font size and color scheme but also the possibility to set the intial board disposition.
This program was originally posted by me at [http://www.perlmonks.org/?node_id=1192660 perlmonks]
This program was originally posted by me at [http://www.perlmonks.org/?node_id=1192660 perlmonks]
<syntaxhighlight lang=perl>
<syntaxhighlight lang="perl">


use strict;
use strict;
Line 7,600: Line 7,600:
===console version===
===console version===
This short console program just poses solvable puzzles: it achieves this shuffling a solved board n times, where n defaults to 1000 but can be passed as first argument of the program in the command line. It was originally posted by me at [http://www.perlmonks.org/?node_id=1192865 perlmonks] but here a little modification was inserted to prevent wrong numbers to make the board messy.
This short console program just poses solvable puzzles: it achieves this shuffling a solved board n times, where n defaults to 1000 but can be passed as first argument of the program in the command line. It was originally posted by me at [http://www.perlmonks.org/?node_id=1192865 perlmonks] but here a little modification was inserted to prevent wrong numbers to make the board messy.
<syntaxhighlight lang=perl>
<syntaxhighlight lang="perl">
use strict;
use strict;
use warnings;
use warnings;
Line 7,637: Line 7,637:
=== console only ===
=== console only ===
Kept simple. Obviously, increase the 5 random moves for more of a challenge.
Kept simple. Obviously, increase the 5 random moves for more of a challenge.
<!--<syntaxhighlight lang=Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">constant</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;">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 style="color: #0000FF;">,</span> <span style="color: #000000;">DOWN<span style="color: #0000FF;">=<span style="color: #000000;">336</span>
<span style="color: #008080;">constant</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;">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 style="color: #0000FF;">,</span> <span style="color: #000000;">DOWN<span style="color: #0000FF;">=<span style="color: #000000;">336</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset<span style="color: #0000FF;">(<span style="color: #000000;">15<span style="color: #0000FF;">)<span style="color: #0000FF;">&<span style="color: #000000;">0<span style="color: #0000FF;">,</span> <span style="color: #000000;">solve</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">board</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset<span style="color: #0000FF;">(<span style="color: #000000;">15<span style="color: #0000FF;">)<span style="color: #0000FF;">&<span style="color: #000000;">0<span style="color: #0000FF;">,</span> <span style="color: #000000;">solve</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">board</span>
Line 7,703: Line 7,703:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/15game.htm here].
You can run this online [http://phix.x10.mx/p2js/15game.htm here].
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\15_puzzle_game.exw
-- demo\rosetta\15_puzzle_game.exw
Line 7,832: Line 7,832:
=={{header|PHP}}==
=={{header|PHP}}==
OOP and MVC design pattern has been used to facilitate any improvements, e.g. tiles with graphics instead of just numbers.
OOP and MVC design pattern has been used to facilitate any improvements, e.g. tiles with graphics instead of just numbers.
<syntaxhighlight lang=PHP><?php
<syntaxhighlight lang="php"><?php
// Puzzle 15 Game - Rosseta Code - PHP 7 as the server-side script language.
// Puzzle 15 Game - Rosseta Code - PHP 7 as the server-side script language.


Line 8,060: Line 8,060:


=={{header|Picat}}==
=={{header|Picat}}==
<syntaxhighlight lang=Picat>
<syntaxhighlight lang="picat">
import util.
import util.


Line 8,132: Line 8,132:
Board is always scrambled from a solved board so it's always solvable.
Board is always scrambled from a solved board so it's always solvable.
Written in ISE with ISESteroids. I'm sure the code could be improved on.
Written in ISE with ISESteroids. I'm sure the code could be improved on.
<syntaxhighlight lang=Powershell>
<syntaxhighlight lang="powershell">
#15 Puzzle Game
#15 Puzzle Game
$Script:Neighbours = @{
$Script:Neighbours = @{
Line 8,395: Line 8,395:


=={{header|Processing}}==
=={{header|Processing}}==
<syntaxhighlight lang=java>
<syntaxhighlight lang="java">
int number_of_grid_cells = 16; // Set the number of cells of the board here 9, 16, 25 etc
int number_of_grid_cells = 16; // Set the number of cells of the board here 9, 16, 25 etc
color piece_color = color(255, 175, 0);
color piece_color = color(255, 175, 0);
Line 8,515: Line 8,515:
==={{header|Processing Python mode}}===
==={{header|Processing Python mode}}===
{{trans|Processing}}
{{trans|Processing}}
<syntaxhighlight lang=Python># Set the number of cells of the board here 9, 16, 25 etc
<syntaxhighlight lang="python"># Set the number of cells of the board here 9, 16, 25 etc
num_grid_cells = 16
num_grid_cells = 16
piece_color = color(255, 175, 0)
piece_color = color(255, 175, 0)
Line 8,631: Line 8,631:
Numbers are displayed in Hexadecimal (ie 1 to F)
Numbers are displayed in Hexadecimal (ie 1 to F)
Default controls are u,d,l,r
Default controls are u,d,l,r
<syntaxhighlight lang=PureBasic>
<syntaxhighlight lang="purebasic">
#difficulty=10 ;higher is harder
#difficulty=10 ;higher is harder
#up="u"
#up="u"
Line 8,715: Line 8,715:
{{works with|Python|3.X}}
{{works with|Python|3.X}}
'''unoptimized'''
'''unoptimized'''
<syntaxhighlight lang=python>
<syntaxhighlight lang="python">
''' Structural Game for 15 - Puzzle with different difficulty levels'''
''' Structural Game for 15 - Puzzle with different difficulty levels'''
from random import randint
from random import randint
Line 8,874: Line 8,874:


===Python: using tkinter===
===Python: using tkinter===
<syntaxhighlight lang=python>
<syntaxhighlight lang="python">
''' Python 3.6.5 code using Tkinter graphical user interface.'''
''' Python 3.6.5 code using Tkinter graphical user interface.'''


Line 9,127: Line 9,127:


=={{header|QB64}}==
=={{header|QB64}}==
<syntaxhighlight lang=QB64>
<syntaxhighlight lang="qb64">
_TITLE "GUI Sliding Blocks Game "
_TITLE "GUI Sliding Blocks Game "
RANDOMIZE TIMER
RANDOMIZE TIMER
Line 9,215: Line 9,215:
=={{header|Quackery}}==
=={{header|Quackery}}==
The inputs are w,s,a,d and they refer to the direction in which the empty place "moves". After every prompt you can input a string of characters and submit them by pressing enter. Any characters other than w,s,a,d will be ignored, and moves will be performed according to the correct commands. Commands that would "move" the empty space off the edge of the puzzle are also ignored. For example, on a solved puzzle "asgw" would move 15 to the right, ignore 's' and 'g' and move 11 down.
The inputs are w,s,a,d and they refer to the direction in which the empty place "moves". After every prompt you can input a string of characters and submit them by pressing enter. Any characters other than w,s,a,d will be ignored, and moves will be performed according to the correct commands. Commands that would "move" the empty space off the edge of the puzzle are also ignored. For example, on a solved puzzle "asgw" would move 15 to the right, ignore 's' and 'g' and move 11 down.
<syntaxhighlight lang=Quackery>
<syntaxhighlight lang="quackery">
( moves: 0 - up, 1 - down, 2 - left, 3 - right )
( moves: 0 - up, 1 - down, 2 - left, 3 - right )


Line 9,309: Line 9,309:
=={{header|R}}==
=={{header|R}}==
The inputs are w,a,s,d and they refer to the direction in which the adjacent piece moves into the empty space. For example, on a solved puzzle, "d" would move the 15 to the right.
The inputs are w,a,s,d and they refer to the direction in which the adjacent piece moves into the empty space. For example, on a solved puzzle, "d" would move the 15 to the right.
<syntaxhighlight lang=R>
<syntaxhighlight lang="r">
puz15<-function(scramble.length=100){
puz15<-function(scramble.length=100){
m=matrix(c(1:15,0),byrow=T,ncol=4)
m=matrix(c(1:15,0),byrow=T,ncol=4)
Line 9,445: Line 9,445:
It uses the <code>2htdp/universe</code> package.
It uses the <code>2htdp/universe</code> package.


<syntaxhighlight lang=racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base
(require 2htdp/universe 2htdp/image racket/list racket/match)
(require 2htdp/universe 2htdp/image racket/list racket/match)


Line 9,499: Line 9,499:
{{works with|Rakudo|2018.06}}
{{works with|Rakudo|2018.06}}
Most of this is interface code. Reused substantial portions from the [[2048#Raku|2048]] task. Use the arrow keys to slide tiles, press 'q' to quit or 'n' for a new puzzle. Requires a POSIX termios aware terminal. Ensures that the puzzle is solvable by shuffling the board with an even number of swaps, then checking for even taxicab parity for the empty space.
Most of this is interface code. Reused substantial portions from the [[2048#Raku|2048]] task. Use the arrow keys to slide tiles, press 'q' to quit or 'n' for a new puzzle. Requires a POSIX termios aware terminal. Ensures that the puzzle is solvable by shuffling the board with an even number of swaps, then checking for even taxicab parity for the empty space.
<syntaxhighlight lang=perl6>use Term::termios;
<syntaxhighlight lang="raku" line>use Term::termios;


constant $saved = Term::termios.new(fd => 1).getattr;
constant $saved = Term::termios.new(fd => 1).getattr;
Line 9,632: Line 9,632:


=={{header|Rebol}}==
=={{header|Rebol}}==
<syntaxhighlight lang=Rebol>rebol [] random/seed now g: [style t box red [
<syntaxhighlight lang="rebol">rebol [] random/seed now g: [style t box red [
if not find [0x108 108x0 0x-108 -108x0] face/offset - e/offset [exit]
if not find [0x108 108x0 0x-108 -108x0] face/offset - e/offset [exit]
x: face/offset face/offset: e/offset e/offset: x] across
x: face/offset face/offset: e/offset e/offset: x] across
Line 9,640: Line 9,640:


=={{header|Red}}==
=={{header|Red}}==
<syntaxhighlight lang=Red>
<syntaxhighlight lang="red">
Red [Needs: 'view]
Red [Needs: 'view]
system/view/screens/1/pane/-1/visible?: false ;; suppress console window
system/view/screens/1/pane/-1/visible?: false ;; suppress console window
Line 9,715: Line 9,715:


Over half of the REXX program has to do with input validation and presentation of the puzzle (grid).
Over half of the REXX program has to do with input validation and presentation of the puzzle (grid).
<syntaxhighlight lang=rexx>/*REXX pgm implements the 15─puzzle (AKA: Gem Puzzle, Boss Puzzle, Mystic Square, 14─15)*/
<syntaxhighlight lang="rexx">/*REXX pgm implements the 15─puzzle (AKA: Gem Puzzle, Boss Puzzle, Mystic Square, 14─15)*/
parse arg N seed . /*obtain optional arguments from the CL*/
parse arg N seed . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N=4 /*Not specified? Then use the default.*/
if N=='' | N=="," then N=4 /*Not specified? Then use the default.*/
Line 9,824: Line 9,824:
=={{header|Ring}}==
=={{header|Ring}}==


<syntaxhighlight lang=ring>
<syntaxhighlight lang="ring">
# Project : CalmoSoft Fifteen Puzzle Game
# Project : CalmoSoft Fifteen Puzzle Game
# Date : 2018/12/01
# Date : 2018/12/01
Line 10,598: Line 10,598:


=={{header|Ruby}}==
=={{header|Ruby}}==
<syntaxhighlight lang=ruby>require 'io/console'
<syntaxhighlight lang="ruby">require 'io/console'


class Board
class Board
Line 10,707: Line 10,707:
{{libheader|JRubyArt}}
{{libheader|JRubyArt}}
Gui Version:-
Gui Version:-
<syntaxhighlight lang=ruby>DIM = 100
<syntaxhighlight lang="ruby">DIM = 100
SOLUTION = %w[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0].freeze
SOLUTION = %w[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0].freeze


Line 10,813: Line 10,813:


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==
<syntaxhighlight lang=runbasic>call SetCSS
<syntaxhighlight lang="runbasic">call SetCSS
' ---- fill 15 squares with 1 to 15
' ---- fill 15 squares with 1 to 15
dim sq(16)
dim sq(16)
Line 10,887: Line 10,887:
=={{header|Rust}}==
=={{header|Rust}}==
{{libheader|rand}}
{{libheader|rand}}
<syntaxhighlight lang=rust>extern crate rand;
<syntaxhighlight lang="rust">extern crate rand;
use std::collections::HashMap;
use std::collections::HashMap;
Line 11,094: Line 11,094:


=={{header|Scala}}==
=={{header|Scala}}==
<syntaxhighlight lang=scala>import java.util.Random
<syntaxhighlight lang="scala">import java.util.Random


import jline.console._
import jline.console._
Line 11,174: Line 11,174:
=={{header|Scheme}}==
=={{header|Scheme}}==


<syntaxhighlight lang=scheme>
<syntaxhighlight lang="scheme">
(import (scheme base)
(import (scheme base)
(scheme read)
(scheme read)
Line 11,294: Line 11,294:
=={{header|Scilab}}==
=={{header|Scilab}}==


<lang>tiles=[1:15,0];
<syntaxhighlight lang="text">tiles=[1:15,0];
solution=[tiles(1:4);...
solution=[tiles(1:4);...
tiles(5:8);...
tiles(5:8);...
Line 11,448: Line 11,448:


=={{header|Simula}}==
=={{header|Simula}}==
<syntaxhighlight lang=simula>
<syntaxhighlight lang="simula">
BEGIN
BEGIN
CLASS FIFTEENPUZZLE(NUMTILES, SIDE, WIDTH, SEED);
CLASS FIFTEENPUZZLE(NUMTILES, SIDE, WIDTH, SEED);
Line 11,703: Line 11,703:
{{works with|SML/NJ}}
{{works with|SML/NJ}}
{{works with|Moscow ML}}
{{works with|Moscow ML}}
<syntaxhighlight lang=sml>
<syntaxhighlight lang="sml">
(* Load required Modules for Moscow ML *)
(* Load required Modules for Moscow ML *)
load "Int";
load "Int";
Line 11,971: Line 11,971:
The window-title is used to show messages.
The window-title is used to show messages.


<syntaxhighlight lang=tcl> # 15puzzle_21.tcl - HaJo Gurt - 2016-02-16
<syntaxhighlight lang="tcl"> # 15puzzle_21.tcl - HaJo Gurt - 2016-02-16
# http://wiki.tcl.tk/14403
# http://wiki.tcl.tk/14403


Line 12,114: Line 12,114:
This plays the game in an ANSI-compliant terminal using vi/nethack movement keys to slide the tiles.
This plays the game in an ANSI-compliant terminal using vi/nethack movement keys to slide the tiles.


<syntaxhighlight lang=bash>#!/usr/bin/env bash
<syntaxhighlight lang="bash">#!/usr/bin/env bash
main() {
main() {
local puzzle=({1..15} " ") blank moves_kv i key count total last
local puzzle=({1..15} " ") blank moves_kv i key count total last
Line 12,228: Line 12,228:
The last move is displayed on the input box, or an error message if an invalid move is attempted. When the puzzle is solved, the move count is displayed.
The last move is displayed on the input box, or an error message if an invalid move is attempted. When the puzzle is solved, the move count is displayed.


<syntaxhighlight lang=vb>
<syntaxhighlight lang="vb">
Public iSide As Integer
Public iSide As Integer
Public iSize As Integer
Public iSize As Integer
Line 12,424: Line 12,424:


=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
<syntaxhighlight lang=vbnet>Public Class Board
<syntaxhighlight lang="vbnet">Public Class Board
Inherits System.Windows.Forms.Form
Inherits System.Windows.Forms.Form


Line 12,526: Line 12,526:
[https://youtu.be/rWy3AX5HjXM 15 Puzzle in Visual Prolog - video]
[https://youtu.be/rWy3AX5HjXM 15 Puzzle in Visual Prolog - video]


<syntaxhighlight lang=Visual Prolog>
<syntaxhighlight lang="visual prolog">
/* ------------------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------------


Line 13,464: Line 13,464:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang=VB>
<syntaxhighlight lang="vb">
'----------------15 game-------------------------------------
'----------------15 game-------------------------------------
'WARNING: this script uses ANSI escape codes to position items on console so
'WARNING: this script uses ANSI escape codes to position items on console so
Line 13,579: Line 13,579:
{{libheader|Wren-ioutil}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang=ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/dynamic" for Enum
import "/dynamic" for Enum
import "/ioutil" for Input
import "/ioutil" for Input
Line 13,761: Line 13,761:


=={{header|x86-64 Assembly}}==
=={{header|x86-64 Assembly}}==
<syntaxhighlight lang=assembly> ; Puzzle15 by grosged (march 2019)
<syntaxhighlight lang="assembly"> ; Puzzle15 by grosged (march 2019)
; How to play ?.. Just press one of the arrow keys then [enter] to valid
; How to play ?.. Just press one of the arrow keys then [enter] to valid
; ( press [Ctrl+C] to escape )
; ( press [Ctrl+C] to escape )
Line 13,847: Line 13,847:
{{trans|Liberty BASIC}}
{{trans|Liberty BASIC}}
{{works with|Windows XBasic}}
{{works with|Windows XBasic}}
<syntaxhighlight lang=xbasic>
<syntaxhighlight lang="xbasic">
PROGRAM "fifteenpuzzlegame"
PROGRAM "fifteenpuzzlegame"
VERSION "0.0001"
VERSION "0.0001"
Line 14,006: Line 14,006:


=={{header|XPL0}}==
=={{header|XPL0}}==
<syntaxhighlight lang=XPL0>int Box, Hole, I;
<syntaxhighlight lang="xpl0">int Box, Hole, I;
[Box:= [^ ,^F,^E,^D, \starting configuration
[Box:= [^ ,^F,^E,^D, \starting configuration
^C,^B,^A,^9, \slide digits into ascending order
^C,^B,^A,^9, \slide digits into ascending order
Line 14,047: Line 14,047:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<syntaxhighlight lang=Yabasic>dx = 4 : dy = 4 : dxy = dx * dy
<syntaxhighlight lang="yabasic">dx = 4 : dy = 4 : dxy = dx * dy
dim grid(dx, dy)
dim grid(dx, dy)


Line 14,127: Line 14,127:


Adaptation from Phix solution
Adaptation from Phix solution
<syntaxhighlight lang=Yabasic>board$ = "123456789ABCDEF0"
<syntaxhighlight lang="yabasic">board$ = "123456789ABCDEF0"
solve$ = board$
solve$ = board$
pos = 16
pos = 16