This respository contains the Javascript solution for the Eight Puzzle Problem. The current solution utilizes the BFS, DFS, Greedy Search, Dijkstra and A* algorithm (Manhattan cost).
- 0 is the slider or non-number block
- See the result by simply running
npm install js-priority-queue and node main.js
- Modify
var sol = new Solution([1, 2, 3, 4, 5, 0, 6, 7, 8])
intovar solution = new Solution(positions_array)
to solve custom initial state
Note: [1, 0, 2, 3, 4, 5, 6, 7, 8] will be presented in the format:
1 0 2
3 4 5
6 7 8
var sol = new Solution([1, 0, 2, 3, 4, 5, 6, 7, 8]);
sol.mainBFS(); // Call sol.mainDFS, sol.mainAStar, sol.mainGreedySearch, sol.mainDS for other solving options
sol.printSolutionPath();
0 1 2
3 4 5
6 7 8
1 0 2
3 4 5
6 7 8
getSolutionPath() returns the sequence of moves from the current position of the slider ( 0 )
0: Do Nothing
1 : Up
2: Down
3: Left
4: Right
var sol = new Solution([1, 0, 2, 3, 4, 5, 6, 7, 8]);
sol.mainBFS();
let path = sol.getSolutionPath().reverse()
console.log(path) // [0, 3]