How to resolve the algorithm 15 puzzle game step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 15 puzzle game step by step in the J programming language
Table of Contents
Problem Statement
Implement the Fifteen Puzzle Game.
The 15-puzzle is also known as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 15 puzzle game step by step in the J programming language
Source code in the j programming language
require'general/misc/prompt'
genboard=:3 :0
b=. ?~16
if. 0<C.!.2 b do.
b=. (<0 _1)C. b
end.
a: (b i.0)} <"0 b
)
done=: (<"0]1+i.15),a:
shift=: |.!._"0 2
taxi=: |:,/"2(_1 1 shift i.4 4),_1 1 shift"0 1/ i.4 4
showboard=:3 :0
echo 'current board:'
echo 4 4$y
)
help=:0 :0
Slide a number block into the empty space
until you get:
┌──┬──┬──┬──┐
│1 │2 │3 │4 │
├──┼──┼──┼──┤
│5 │6 │7 │8 │
├──┼──┼──┼──┤
│9 │10│11│12│
├──┼──┼──┼──┤
│13│14│15│ │
└──┴──┴──┴──┘
Or type 'q' to quit.
)
getmove=:3 :0
showboard y
blank=. y i. a:
options=. /:~ ;y {~ _ -.~ blank { taxi
whilst. -. n e. options do.
echo 'Valid moves: ',":options
t=. prompt 'move which number? '
if. 'q' e. t do.
echo 'giving up'
throw.
elseif. 'h' e. t do.
echo help
showboard y
end.
n=. {._".t
end.
(<blank,y i.<n) C. y
)
game=: 3 :0
echo '15 puzzle'
echo 'h for help, q to quit'
board=. genboard''
whilst. -. done-:board do.
board=. getmove board
end.
showboard board
echo 'You win.'
)
game''
15 puzzle
h for help, q to quit
current board:
┌──┬──┬──┬──┐
│1 │2 │3 │4 │
├──┼──┼──┼──┤
│5 │6 │7 │8 │
├──┼──┼──┼──┤
│9 │10│ │11│
├──┼──┼──┼──┤
│13│14│15│12│
└──┴──┴──┴──┘
Valid moves: 7 10 11 15
move which number? 11
current board:
┌──┬──┬──┬──┐
│1 │2 │3 │4 │
├──┼──┼──┼──┤
│5 │6 │7 │8 │
├──┼──┼──┼──┤
│9 │10│11│ │
├──┼──┼──┼──┤
│13│14│15│12│
└──┴──┴──┴──┘
Valid moves: 8 11 12
move which number? 12
current board:
┌──┬──┬──┬──┐
│1 │2 │3 │4 │
├──┼──┼──┼──┤
│5 │6 │7 │8 │
├──┼──┼──┼──┤
│9 │10│11│12│
├──┼──┼──┼──┤
│13│14│15│ │
└──┴──┴──┴──┘
You win.
You may also check:How to resolve the algorithm Chaos game step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Remove duplicate elements step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Case-sensitivity of identifiers step by step in the GAP programming language
You may also check:How to resolve the algorithm Sorting algorithms/Counting sort step by step in the Fortran programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the ALGOL-M programming language