Coder Social home page Coder Social logo

cpp_8-puzzle_search's Introduction

8-Puzzle Solver

Project Description

This project implements a solver for the classic 8-puzzle game. The program takes an initial configuration and a search strategy as input and aims to find a path from the initial state to the goal state.

User Input :

Initial configuration: A 3x3 representation of the starting state of the 8-puzzle (using 0 to represent the blank tile), as a string of numbers seperated by a space.

Search strategy: Choose from the following :

Depth-first search (DFS) Breadth-first search (BFS) Best-first search (with three different heuristic options)

Output :

Series of states (path): The sequence of board configurations from the initial state to the goal state. Number of generated nodes (states): The total number of board states explored during the search.

Search Strategies :

Depth-first search (DFS): Explores the search tree in a depth-wise manner. Breadth-first search (BFS): Explores the search tree in a level-by-level manner. Best-first search: Prioritizes exploration based on a heuristic evaluation. The following heuristics are implemented: Heuristic 1: Depth in the search space + Number of tiles out of place. Heuristic 2: Depth in the search space + Minimum number of moves to reach the goal state (often calculated using Manhattan distance). Heuristic 3: Depth in the search space + H, where H is a custom heuristic defined below.

Custom Heuristic (H)

H combines two measures: totdist: The total Manhattan distance of tiles from their goal positions. Manhattan distance is the sum of horizontal and vertical distances between two positions. seq: The sequence score, which measures how well tiles are ordered. Rules for calculation are provided in the detailed description.

Example output :

Welcome to the 8-puzzle solver program! 
To start, please take a look at what a completed 8-puzzle looks like: 
---------
| 1 2 3 |
| 8 0 4 |
| 7 6 5 |
---------
To enter your own 8-puzzle, please enter the numbers from left to right,
top to bottom, with a space between each number. 
For example, the 8-puzzle above would be entered as : 1 2 3 8 0 4 7 6 5 
With the 0 representing the empty space. 
Please enter your 8-puzzle : 1 3 4 8 0 2 7 6 5

Starting state: 
---------
| 1 3 4 |
| 8 0 2 |
| 7 6 5 |
---------

Now, Please choose from the options below: 
1. Depth First Search 
2. Breadth First Search 
3. Best-First Search 
4. End program 

Enter your choice using a number: 2
Solving with Breadth First Search... 
--- Starting Breadth First Search--- 
Breadth First Search completed! 
Number of Nodes: 5
Path from the intial state to the goal: 
---------  ---------  ---------  ---------  ---------  
| 1 3 4 |  | 1 3 4 |  | 1 3 0 |  | 1 0 3 |  | 1 2 3 |  
| 8 0 2 |  | 8 2 0 |  | 8 2 4 |  | 8 2 4 |  | 8 0 4 |  
| 7 6 5 |  | 7 6 5 |  | 7 6 5 |  | 7 6 5 |  | 7 6 5 |  
---------  ---------  ---------  ---------  ---------  

Now, Please choose from the options below: 
1. Depth First Search 
2. Breadth First Search 
3. Best-First Search 
4. End program 

Enter your choice using a number: 3
Please select a heuristic: 
1. Number of misplaced tiles 
2. Manhattan distance 
3. The Heuristic H (H = totdist + 3*seq) 
4. Go back 

Enter your choice using a number: 1
Solving with Best-First Search, Heuristic 1... 
--- Starting Best First Search--- 
Best-First Search completed! 
Number of Nodes: 5
Path from the intial state to the goal: 
---------  ---------  ---------  ---------  ---------  
| 1 3 4 |  | 1 3 0 |  | 1 0 3 |  | 1 2 3 |  | 1 2 3 |  
| 8 2 0 |  | 8 2 4 |  | 8 2 4 |  | 8 0 4 |  | 8 0 4 |  
| 7 6 5 |  | 7 6 5 |  | 7 6 5 |  | 7 6 5 |  | 7 6 5 |  
---------  ---------  ---------  ---------  ---------  
Please select a heuristic: 
1. Number of misplaced tiles 
2. Manhattan distance 
3. The Heuristic H (H = totdist + 3*seq) 
4. Go back 

Enter your choice using a number: 4

Now, Please choose from the options below: 
1. Depth First Search 
2. Breadth First Search 
3. Best-First Search 
4. End program 

Enter your choice using a number: 4
Goodbye!

cpp_8-puzzle_search's People

Contributors

mrtoast72 avatar

Watchers

 avatar

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.