Search algorithms for the 8-puzzle solution
Public Member Functions | Private Attributes
DepthFirstSolver Class Reference

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

#include <DepthFirstSolver.hpp>

Inheritance diagram for DepthFirstSolver:
Collaboration diagram for DepthFirstSolver:

Public Member Functions

 DepthFirstSolver ()
 
 DepthFirstSolver (int maxDepth)
 
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...
 

Private Attributes

int maxDepth
 

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 depth-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 depth-first search strategy

Definition at line 16 of file DepthFirstSolver.hpp.

Constructor & Destructor Documentation

◆ DepthFirstSolver() [1/2]

DepthFirstSolver::DepthFirstSolver ( )
inline

Definition at line 23 of file DepthFirstSolver.hpp.

23  : maxDepth(- 1) {
24  // no limits in depth
25  }
Here is the caller graph for this function:

◆ DepthFirstSolver() [2/2]

DepthFirstSolver::DepthFirstSolver ( int  maxDepth)
inlineexplicit
Parameters
maxDepththe maximum depth allowed for exploration

Definition at line 28 of file DepthFirstSolver.hpp.

28  : maxDepth(maxDepth > - 1 ? maxDepth : - 1) {
29  }

Member Function Documentation

◆ solve()

LinkedList<GameState *> DepthFirstSolver::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 31 of file DepthFirstSolver.hpp.

31  {
32  // use a stack to organize states, states that were reached last will be the
33  // first ones to be evaluated
34  DynamicStack<GameState *> expanded;
35 
36  const clock_t start = clock();
37  expanded.push(&g0);
38  int currentMaxDepth = 0;
39 
40  while (! expanded.isEmpty()) {
41  GameState *currentGame = expanded.pop();
42 
43  if (currentGame->getDepth() > currentMaxDepth) {
44  currentMaxDepth = currentGame->getDepth();
45  }
46 
47  // useful only for debugging
48  // cout << currentGame->getDepth() << '\t' << visited.getSize() << '\t' << expanded.getSize() << endl;
49 
50  // check for goal
51  if (*game.getGoal() == *currentGame) {
52  solved = true;
53  return endSearch(currentGame, start);
54  }
55 
56  // expand children, but check the maximum allowed depth before inserting
57  // them in the exploration stack
58  LinkedList<GameState *> children = visit(currentGame);
59 
60  if ((maxDepth == - 1) || (currentGame->getDepth() < maxDepth)) {
61  while (! children.isEmpty()) {
62  expanded.push(children.remove(0));
63  }
64  }
65  }
66  throw invalid_argument("This game is unsolvable!");
67  }
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:

Field Documentation

◆ maxDepth

int DepthFirstSolver::maxDepth
private

Definition at line 19 of file DepthFirstSolver.hpp.


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