Solving 8-puzzle using A* search
To install depedencies, execute de the following command in the directory.
python -m pip install -r requirements.txt
You must have a file ending in the .start extension.
In it the file the starting puzzle must be specified this way.
3 | 1 | 2
4 | 7 | 5
6 | 8 |
The name of the file must be the same as the one specified inside the game_name variable inside the program.
You can them run the program with the following command.
python puzzle.py
You can find the solution and generated tree in the corresponding .log file.
For example game.log for a game using game.start
The main() function is the most important in the program.
It follows this pattern
- Initialize the script with the given parameters
- A while loop :
- Check if no fringe can be expanded
- End the script with no solution
- Choose the best fringe according to the A star strategy, in this case this is done by sorting the fringes by f score and then selecting the first one
- Updating the puzzle
- Updating the tree
- Updating the fringes
- Calling f on the fringes
- dumping current state of the game to the log file
- Checking for goal stat
- Find the solution
- Display the solution on the terminal and the log file
- End script
- Check if no fringe can be expanded
The tree structure is implemented through the anytree library
You can find result of both runs in the .log files.