Giter Club home page Giter Club logo

8-puzzle-solver's Introduction

8-Puzzle Solver

My implementation of a 8-Puzzle Solver using A* search algorithm.

How it works

The program uses A* algorithm to solve the puzzle. The g score each state is calculated by parent state’s total number of move + 1, while the heuristic value is the total Manhattan distance of all tiles from their current position to goal position, and f score is the sum of g score and h score.

The logic for the AI algorithm is as follows:

Step 1

There is a class call State, which stores the different states of the board/puzzle, the parent of that state, the blank’s position as well as its neighbors’ position, and also the g score, h score and f score of the state

Step 2

In the search function, two lists called openSet and closeSet is defined, and then the input puzzle is pushed into the openSet.

Step 3

Then, while the openSet is not empty, do the following:

▸ First, get the state with the lowest f score in the openSet, once find, set the currentState as that state, the push that state into the closeSet and remove it from the openSet.

▸ Check if the currentState is the same as the goal state (h score = 0), if yes, then quit out of the loop

▸ Then, check all the neighbors of the currentState (up, down, left, right), and swap the blank with the neighbor in each loop, and set nextState to that state.

▸ Then, check if nextState exists in closeSet already (already visited), and if true, skip this neighbor and move on to others.

▸ Else, check if the neighbour is in openSet already, if yes, get a reference of it. Then, check if the nextState’s h score is lower then currentState’s h score, or if it is in openSet already, if yes, then use the last element in the closeSet and set it as the parent of nextState. If nextState doesn’t exist in openSet, then push it into openSet, else just update the score of the reference.

Step 4

Once the search is complete, get the closeSet’s last element and backtrack to start using its parent, then convert the blank’s position into a char and store the path in a string.

Step 5

At last, print the string in reverse to get the move sequence.

Code references

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.