Search algorithms for the 8-puzzle solution
Public Member Functions
BreadthFirstSolver Class Reference

8-puzzle exploration based on a breadth-first search strategy More...

#include <BreadthFirstSolver.hpp>

Inheritance diagram for BreadthFirstSolver:
Collaboration diagram for BreadthFirstSolver:

Public Member Functions

LinkedList< GameState * > solve (Game &game, GameState &g0)
 Explores the game tree in search of the goal state. More...
 
- Public Member Functions inherited from Solver
double getSecondsToSolve () const
 
int getVisitedNodes () const
 
int getMaxDepth () const
 
int getSolutionDepth () const
 
bool isSolved ()
 
 Solver ()
 
string to_string ()
 Generates a string containing useful information from the solver run. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Solver
bool isVisited (GameState &g)
 Checks whether a node has already been visited by the solver. More...
 
LinkedList< GameState * > visit (GameState *current)
 
LinkedList< GameState * > visit (GameState *current, bool keepVisited)
 Visit a game state, adding it to the list of visited states and returning its valid child states. More...
 
LinkedList< GameState * > endSearch (GameState *currentGame, const clock_t start)
 End the search, generating the steps from the initial state to the goal. More...
 
- Protected Attributes inherited from Solver
OrderedList< GameState * > visited = OrderedList<GameState *>(compare)
 
int visitedNodes
 
int maxDepth
 
int solutionDepth
 
float secondsToSolve
 
bool solved
 

Detailed Description

8-puzzle exploration based on a breadth-first search strategy

Author
Douglas De Rizzo Meneghetti (dougl.nosp@m.asri.nosp@m.zzom@.nosp@m.gmai.nosp@m.l.com)
Date
2017-6-238-puzzle exploration based on a breadth-first search strategy

Definition at line 14 of file BreadthFirstSolver.hpp.

Member Function Documentation

◆ solve()

LinkedList<GameState *> BreadthFirstSolver::solve ( Game g,
GameState gs 
)
inlinevirtual

Explores the game tree in search of the goal state.

Exploration is done by applying one of the four valid actions of the 8-puzzle to intermediate, non-goal states until the goal state is reached.

Parameters
gA description of the game.
gsThe initial state of the game.
Returns
list containing all states explored, from the initial state to the goal state.

Implements Solver.

Definition at line 17 of file BreadthFirstSolver.hpp.

17  {
18  // use a queue to organize states, states that were reached first will be the first ones to be evaluated
19  DynamicQueue<GameState *> expanded;
20 
21  const clock_t start = clock();
22  expanded.enqueue(&g0);
23 
24  int currentDepth = 0;
25 
26  cout << "Depth\tVisited\t\tExpanded\n";
27  while (! expanded.isEmpty()) {
28  GameState *currentGame = expanded.dequeue();
29 
30  // check for goal
31  if (*game.getGoal() == *currentGame) {
32  solved = true;
33  return endSearch(currentGame, start);
34  }
35 
36  // if new depth, output a line to stdout
37  if (currentGame->getDepth() != currentDepth) {
38  currentDepth = currentGame->getDepth();
39  cout << currentGame->getDepth() << "\t\t" << visited.getSize() << "\t\t\t" << expanded.getSize() << endl;
40  }
41 
42  //expand children
43  LinkedList<GameState *> children = visit(currentGame);
44  while (! children.isEmpty()) expanded.enqueue(children.remove(0));
45  }
46  throw invalid_argument("This game is unsolvable!");
47  }
OrderedList< GameState * > visited
Definition: Solver.hpp:27
LinkedList< GameState * > endSearch(GameState *currentGame, const clock_t start)
End the search, generating the steps from the initial state to the goal.
Definition: Solver.hpp:91
LinkedList< GameState * > visit(GameState *current)
Definition: Solver.hpp:43
Describes a single state in the 8-puzzle.
Definition: GameState.hpp:18
bool solved
Definition: Solver.hpp:34
int getDepth() const
Definition: GameState.hpp:339
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: