Code Monkey home page Code Monkey logo

8-puzzle-solver's Introduction

8-Puzzle-Solver

Using A* Algorithm to find the shortest solution to the 8 Puzzle.

ezgif com-gif-maker(2)


Problem Statement

-- Given a 3x3 board with 8 tiles (1 to 8) and one empty space. The objective is to place the numbers on tiles to match final configuration using the empty space. We can slide four adjacent (left, right, above and below) tiles into the empty space.

Problem Mapping

The problem can be mapped into a search problem as follows:

  • State space: All possible configurations of the given board
  • Initial state: The given board with one tile missing
  • Goal state: The final configuration of the board
  • Successor function: All possible moves that can be made from a given state
  • Path cost: Number of moves so far to reach the current state

Algorithm

For this problem, we will use A* algorithm to find the shortest path from the initial state to the goal state. There's better algorithm like IDA* but it's not implemented in this project. The algorithm is as follows:

  • Create a decision tree with the initial state as the root node
  • Expand the node with the lowest f(n) value
  • Repeat until the goal state is reached or the tree is exhausted

Heuristic Function

The heuristic function is used to calculate the cost of a given state. The cost is calculated by finding the distance of each tile from its goal position. The distance is calculated based on their row and column position, only taking into account the number of moves required to reach the goal position.

How to run

  • Clone the repository
  • Open index.html in your browser and the app will run.

Notes:

  • The app is not optimized for mobile devices yet. But it will work on mobile devices.
  • If a solution is not found, the app will show a message saying "No solution found". However, the app will still run in the background, if it's taking too long to find a solution, I suggest you refresh the page.
  • If needed a custom board can be created by changing the initial state in the code, or using the drawGrid() function in the console, which takes an Int Array as input.

Screenshots

Screen Shot 2022-10-16 at 18 08 45 heuristic_A_Screen Shot 2022-10-14 at 20 51 42 heuristic_C_Screen Shot 2022-10-16 at 17 57 39

8-puzzle-solver's People

Contributors

reavnail avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

cesartt2

8-puzzle-solver's Issues

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.