Skip to content

Solution for the classic 8-puzzle with multiple search approaches

Notifications You must be signed in to change notification settings

akinguyen/EightPuzzleProblem

Repository files navigation

EightPuzzleProblem

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]) into var 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

Running example for case [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

Get sequence of moves from getSolutionPath()

getSolutionPath() returns the sequence of moves from the current position of the slider ( 0 )

Move dictionary:

0: Do Nothing
1 : Up
2: Down
3: Left
4: Right

getSolutionPath() Usage

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]

About

Solution for the classic 8-puzzle with multiple search approaches

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published