Finds and displays the minimum number of moves required to reach the goal state of any solvable 8-Puzzle.
This file contains code to solve valid 8-Puzzles using a choice of two heuristics. On completion, it outputs the moves taken to reach a user-provided goal state from a user-provided start state.
The PriorityQueue class from Python's queue module. No installation is required, the queue module is part of Python's standard library.
-
Open a terminal.
-
Navigate to the directory 8-Puzzle.py is located.
-
Execute the Python file.
On Windows:
./8-Puzzle.pyOn Mac:
python3 8-Puzzle.py -
Enter a start state. This must be an 8-Puzzle, at the moment, no other size will work. Any configuration can be used, as long as the state contains an even number of inversions. The file
puzzle-config.txtcontains two start and goal states that can be copy and pasted into the terminal. Use the second pair for a quicker response time. If you wish to enter your own, follow the format of the states in thepuzzle-config.txtfile by leaving a space between the first and second, and second and third tiles in each row. Seperate the rows onto their own lines and use an underscore_to represent the blank space/tile. Please note, the more complex the start and goal states, the longer the program may take to run. -
Enter a goal state. Follow the format for entering a start state.
-
Choose a heuristic. Into the terminal, type
1to use the sum of Manhattan distances of the tiles or2for the Hamming distance. You can only use one heuristic at a time. To use a different heuristic, rerun the program. Both are admissable heuristics, so both will return the same moves taken to solve the puzzle (Manhattan tends to be faster for complex puzzles).