Base class for all 8-puzzle solvers.
More...
#include <Solver.hpp>
Base class for all 8-puzzle solvers.
- Author
- Douglas De Rizzo Meneghetti (dougl.nosp@m.asri.nosp@m.zzom@.nosp@m.gmai.nosp@m.l.com)
- Date
- 2017-6-22Base class for all 8-puzzle solvers.
Definition at line 17 of file Solver.hpp.
◆ Solver()
Definition at line 133 of file Solver.hpp.
OrderedList< GameState * > visited
static int compare(GameState *a, GameState *b)
◆ compare()
◆ endSearch()
LinkedList<GameState *> Solver::endSearch |
( |
GameState * |
currentGame, |
|
|
const clock_t |
start |
|
) |
| |
|
inlineprotected |
End the search, generating the steps from the initial state to the goal.
- Parameters
-
currentGame | the state containing the goal |
start | the time the solve began, in order to calculate its time |
- Returns
- list containing the steps from the initial state to the goal
Definition at line 91 of file Solver.hpp.
94 LinkedList<GameState *> resultSteps;
103 while (tmp != NULL) {
104 resultSteps.insert(tmp, resultSteps.getSize());
OrderedList< GameState * > visited
GameState * getParent() const
Describes a single state in the 8-puzzle.
◆ getMaxDepth()
int Solver::getMaxDepth |
( |
| ) |
const |
|
inline |
◆ getSecondsToSolve()
double Solver::getSecondsToSolve |
( |
| ) |
const |
|
inline |
◆ getSolutionDepth()
int Solver::getSolutionDepth |
( |
| ) |
const |
|
inline |
◆ getVisitedNodes()
int Solver::getVisitedNodes |
( |
| ) |
const |
|
inline |
◆ isSolved()
bool Solver::isSolved |
( |
| ) |
|
|
inline |
◆ isVisited()
Checks whether a node has already been visited by the solver.
- Parameters
-
- Returns
- true if g has been visited, otherwise false
Definition at line 39 of file Solver.hpp.
OrderedList< GameState * > visited
◆ solve()
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
-
g | A description of the game. |
gs | The initial state of the game. |
- Returns
- list containing all states explored, from the initial state to the goal state.
Implemented in HillClimbingSolver, GreedySolver, AStarSolver, DepthFirstSolver, MonteCarloSolver, and BreadthFirstSolver.
◆ to_string()
string Solver::to_string |
( |
| ) |
|
|
inline |
Generates a string containing useful information from the solver run.
- Returns
- string representation of the state-space exploration
Definition at line 149 of file Solver.hpp.
152 "\n\t\t Solution depth: ").append(
155 std::to_string(
getMaxDepth())).append(
"\nNumber of visited nodes: ").append(
int getVisitedNodes() const
double getSecondsToSolve() const
int getSolutionDepth() const
◆ visit() [1/2]
Definition at line 43 of file Solver.hpp.
44 return visit(current,
true);
LinkedList< GameState * > visit(GameState *current)
◆ visit() [2/2]
Visit a game state, adding it to the list of visited states and returning its valid child states.
- Parameters
-
current | the state to be visited |
- Returns
- a list of the valid child states. A valid child state is the result state of applying a valid action to the current state. Only states that have not been visited before by the solver are returned.
Definition at line 50 of file Solver.hpp.
51 LinkedList<GameState *> children;
62 for (
int i = 0; i < 4; i ++) {
64 if (current->
isValid(actions[i])) {
70 if (!
isVisited(*newState)) children.insert(newState);
77 if (keepVisited && (children.getSize() == 4) && (current->
getDepth() > 0)) {
78 throw invalid_argument(
"Duplicate states being visited.");
GameAction
Actions allowed in the 8-puzzle and its variants.
OrderedList< GameState * > visited
bool isVisited(GameState &g)
Checks whether a node has already been visited by the solver.
bool isValid(GameAction action)
Checks whether an action is valid for this state.
Describes a single state in the 8-puzzle.
◆ maxDepth
◆ secondsToSolve
float Solver::secondsToSolve |
|
protected |
◆ solutionDepth
int Solver::solutionDepth |
|
protected |
◆ solved
◆ visited
◆ visitedNodes
The documentation for this class was generated from the following file: