Describes a single state in the 8-puzzle.
More...
#include <GameState.hpp>
|
int * | toIntArray (string s) |
| Turns a string representation of an 8-puzzle into an array. More...
|
|
Describes a single state in the 8-puzzle.
Definition at line 18 of file GameState.hpp.
◆ GameState() [1/4]
Default constructor, everything starts as 0 or NULL.
Definition at line 60 of file GameState.hpp.
◆ GameState() [2/4]
GameState::GameState |
( |
string |
s | ) |
|
|
inlineexplicit |
Creates a GameState from a string, where tile numbers are separated by spaces in a single line.
- Parameters
-
s | string representation on an 8-puzzle state |
Definition at line 68 of file GameState.hpp.
76 dimension =
sizeof(conversion) /
sizeof(conversion[0]) + 1;
80 for (
int j = i + 1; j < dimension *
dimension; j ++) {
81 if (conversion[i] == conversion[j]) {
82 cout << conversion[i] << conversion[j] << i << j;
83 throw invalid_argument(s);
int * toIntArray(string s)
Turns a string representation of an 8-puzzle into an array.
◆ GameState() [3/4]
GameState::GameState |
( |
const GameState & |
obj | ) |
|
|
inline |
Copy constructor of the class.
- Parameters
-
obj | object that will be copied |
Definition at line 106 of file GameState.hpp.
◆ GameState() [4/4]
Creates a new state based on a given previous state and applying an action to it.
- Parameters
-
previous | the previous state |
action | the action to be applied to the previous state |
Definition at line 126 of file GameState.hpp.
143 int *pos_0 =
find(0);
144 int x = pos_0[0], y = pos_0[1];
149 if (action !=
RIGHT and action !=
LEFT and action !=
UP and action !=
DOWN)
150 throw invalid_argument(
"No previous action to build the new GameState.");
153 throw invalid_argument(
"Invalid action for current game state.");
int * find(int value)
Finds the position of a tile in the board.
bool isValid(GameAction action)
Checks whether an action is valid for this state.
◆ ~GameState()
GameState::~GameState |
( |
| ) |
|
|
inline |
◆ countInversions()
int GameState::countInversions |
( |
| ) |
|
|
inline |
Counts the number of inversions in the puzzle.
An inversion occurs whenever a number comes before a smaller number in the puzzle.
- Returns
- number of inversions in this states of the puzzle
Definition at line 233 of file GameState.hpp.
237 int *raster =
new int[rasterDimension];
246 for (
int i = 0; i < rasterDimension - 1; i ++)
247 for (
int j = i + 1; j < rasterDimension; j ++)
248 if (raster[i] != 0 and raster[j] != 0 and raster[i] > raster[j])
◆ find()
int* GameState::find |
( |
int |
value | ) |
|
|
inline |
Finds the position of a tile in the board.
- Parameters
-
value | the value of the tile to look for |
- Returns
- int array where [0] contains x position and [1] y position of the number
Definition at line 318 of file GameState.hpp.
325 throw invalid_argument(
"Inexistent number.");
◆ getDepth()
int GameState::getDepth |
( |
| ) |
const |
|
inline |
◆ getDimension()
int GameState::getDimension |
( |
| ) |
const |
|
inline |
◆ getParent()
◆ isSolvable()
bool GameState::isSolvable |
( |
| ) |
|
|
inline |
Check if the state is solvable.
- Returns
- a state is solvable if the number of inversions in it is even.
Definition at line 258 of file GameState.hpp.
int countInversions()
Counts the number of inversions in the puzzle.
◆ isValid()
Checks whether an action is valid for this state.
Actions are invalid if they'd move the 0 tile outside of the board.
- Parameters
-
action | the action to be validated |
- Returns
- true if the action can be applied in this state, otherwise false
Definition at line 200 of file GameState.hpp.
201 if (action !=
RIGHT and action !=
LEFT and action !=
UP and action !=
DOWN)
204 int *pos_0 =
find(0);
205 int x = pos_0[0], y = pos_0[1];
210 action ==
LEFT and x == 0 ||
211 action ==
UP and y == 0 ||
int * find(int value)
Finds the position of a tile in the board.
◆ operator!=()
bool GameState::operator!= |
( |
const GameState & |
other | ) |
const |
|
inline |
Inequality operator for GameState.
This is the negation of the equality operator.
- Parameters
-
- Returns
- false if equal, otherwise true
Definition at line 280 of file GameState.hpp.
281 return ! (*
this == other);
◆ operator<()
bool GameState::operator< |
( |
const GameState & |
other | ) |
const |
|
inline |
Compares two GameState objects.
Tile numbers are compared in order. If this[i,j] < other[i,j], this < other. This operator is useful in case objects need to be sorted inside a data structure, for example.
- Parameters
-
- Returns
- true if this < other, otherwise false
Definition at line 266 of file GameState.hpp.
267 if (
dimension != other.
dimension)
throw invalid_argument(
"Puzzles of different dimensions aren't comparable");
◆ operator==()
bool GameState::operator== |
( |
const GameState & |
other | ) |
const |
|
inline |
Checks whether two GameState objects are equal.
They are equal if they have the same board dimensions and the same values in the same positions of their board.
- Parameters
-
- Returns
- true if equal, otherwise false
Definition at line 219 of file GameState.hpp.
◆ setRepresentation()
void GameState::setRepresentation |
( |
int ** |
representation | ) |
|
|
inline |
◆ to_line_string()
string GameState::to_line_string |
( |
| ) |
const |
|
inline |
Returns a single line string representation of the 8-puzzle board state.
- Returns
- single line string representation of the state
Definition at line 304 of file GameState.hpp.
312 return tmp.erase(tmp.find_last_not_of(
" \n\r\t") + 1);
◆ to_string()
string GameState::to_string |
( |
| ) |
const |
|
inline |
Returns a multi-line string representation of the 8-puzzle board state.
- Returns
- multi-line string representation of the state
Definition at line 290 of file GameState.hpp.
◆ toIntArray()
int* GameState::toIntArray |
( |
string |
s | ) |
|
|
inlineprivate |
Turns a string representation of an 8-puzzle into an array.
- Parameters
-
s | string representation of an 8-puzzle |
- Returns
- Pointer to an int array containing the state of the puzzle
Definition at line 28 of file GameState.hpp.
32 for (
int i = 0; i < s.length(); i ++) {
33 if (s[i] ==
' ') spaces ++;
37 int *ret =
new int[spaces];
40 string intString =
"";
41 for (
int i = 0; i < s.length(); i ++) {
49 if ((s[i] ==
' ') || (i == s.length() - 1)) {
50 ret[currentInt ++] = stoi(intString);
◆ depth
◆ dimension
◆ parent
◆ representation
int** GameState::representation |
|
private |
The documentation for this class was generated from the following file: