joserc87 / pawn-puzzle

A solver for a simple puzzle

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pawn puzzle

Python script, in reply to the problem described in this 9gag.

The problem

problem definition

The solution

This program tries to find just the first solution to the problem. It loops through every position for every pawn, checking whether it's a valid position to place the pawn according to the restrictions described above, and places the pawns recursively. Finally, it checks wheter the coverage of the pawns is complete, i.e., all the positions are controlled by a pawn.

It is possible to add more pawns, creating subclasses with different behaviors and adding an instance to the initialization array. In the same way, the size of the board or the skulls can be changed too.

The solutions is printed on the console in UTF using ANSII colors. This was tested in an UNIX machine, but it may not work in windows (pull request? ;). Because of this, the colors are not true RGB, so it's difficult to differentiate the pawns (e.g. the pink pawn may appear as violet. See the image of the solution).

** SPOILER ALERT! **

example of output

Improvements

The program takes about 30 seconds to find the solution, and it returns inmediately after it has been found. It can continue looking for solutions (see comments of solver.py) but it will take way too much time with the current implementation. Feel free to improve the performance, making optimizations to it or changing the algorithm to be more efficient.

Use

To run the program you need python 2.7+ or python 3+.

$ python solver.py

To run the tests you need nosetests

$ nosetests

Here is a nerd potato:

nerd potato

About

A solver for a simple puzzle


Languages

Language:Python 100.0%