GitXplorerGitXplorer
e

fifteen_puzzle

public
0 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
1eac7f934ed06226f9273f015a8a2820d0df0f24

added sample puzzles and solutions

ees80 committed 5 years ago
Unverified
503462ed963d6ec545f0bb4572248bdcdcd9089b

added reflections about diagonal to improve heuristic

ees80 committed 5 years ago
Unverified
dd112f108339bcfb09fc79dcdb9efabdaa53e961

changed some comments

ees80 committed 5 years ago
Unverified
2e81cf1d567f2332059e1df7a776c5d7e517e03d

make valgrind happy

ees80 committed 5 years ago
Unverified
ca1a020062d49b7e5f50e3139cbb829527f7a1e5

Merge branch 'master' of https://github.com/es80/fifteen_puzzle

ees80 committed 5 years ago
Unverified
635dae18f7c013ceb263d8119899c66a1d29097c

added screenshot

ees80 committed 5 years ago

README

The README file for this repository.

Fifteen Puzzle and Solver

This is a simple ncurses implementation of the 'Game of Fifteen' puzzle, also known as the fifteen-puzzle or 4x4 sliding tile puzzle. It inlcudes options to play other square dimensions from 3x3 to 9x9. There is also a solver which in the case of 3x3 and 4x4 puzzles can generate optimal solutions.

The code started from a solution to a problem set from the Harvard edX CS50 Introduction to Computer Science course and borrows a small amount of ncurses code from a related problem set.

Instructions

To make and run locally requires

gcc
make
ncurses

then to play a game

make
./fifteen

The arrow keys move tiles whilst 's' and 'r' start new puzzles with either the standard or a random tile configuration respectively. The numbers '2' to '9' will change the dimensions of the puzzle between 2x2 and 9x9. Pressing 'g' calls 'God mode' in which the solver takes over to complete the remainder of the puzzle.

Screenshot

Fifteen screenshot

Optimal Solutions

Initally the solver produces non-optimal solutions by making 'human-like' moves to complete the puzzle.

To generate optimal solutions in the 3x3 case, before running ./fifteen, first run

make generate_dim3_solutions
./generate_dim3_solutions

which will produce a small binary file dim3_solutions.bin. The solver can then lookup optimal solutions to any 3x3 puzzle.

To generate optimal solutions in the 4x4 case, before running ./fifteen, ensure 33.6MB of free space then run

make generate_dim4_heuristics
./generate_dim4_heuristics

This will take a few minutes to complete and will generate a database of heuristic values dim4_heuristics.bin to aid the solver.

The optimal solver for 4x4 puzzles works by employing an iterative deepening A* search using additive pattern database heuristics. Explanations and references are given in the source code, mostly in generate_dim4_heuristics.c.

Most random puzzles will be solved in around a second but some puzzles may require more time. For example the standard configuration takes around twenty seconds on my machine.

Standalone 4x4 Solver

The 4x4 solver can also be used as a standalone program which simply reads a line of numbers between 0 and 15 from standard input and outputs the optimal solution.

$ make standalone_dim4_solver
$ ./standalone_dim4_solver
9 10 11 13 3 0 2 14 15 5 4 7 6 8 12 1
60 moves: 2 14 13 11 10 2 14 13 7 1 12 8 6 15 3 14 13 4 1 7 4 1 5 3 14 13 3 6 15 14 13 9 2 3 1 5 8 15 14 13 9 1 5 10 11 4 7 8 10 11 3 2 1 5 6 10 11 7 8 12

The files in the sample_4x4_puzzles_and_solutions directory can be used for testing this program.