8 #ifndef SEARCH_GAMESTATE_HPP 9 #define SEARCH_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);
73 int *conversion = toIntArray(s);
76 dimension =
sizeof(conversion) /
sizeof(conversion[0]) + 1;
81 if (conversion[i] == conversion[j]) {
82 cout << conversion[i] << conversion[j] << i << j;
83 throw invalid_argument(s);
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.");
185 for (
int i = 0; i < dim; i ++)
201 if (action !=
RIGHT and action !=
LEFT and action !=
UP and action !=
DOWN)
205 int x = pos_0[0], y = pos_0[1];
210 action ==
LEFT and x == 0 ||
211 action ==
UP and y == 0 ||
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])
267 if (dimension != other.dimension)
throw invalid_argument(
"Puzzles of different dimensions aren't comparable");
281 return ! (*
this == other);
295 tmp.append(std::to_string(representation[x][y])).append(
" ");
309 tmp.append(std::to_string(representation[x][y])).append(
" ");
312 return tmp.erase(tmp.find_last_not_of(
" \n\r\t") + 1);
325 throw invalid_argument(
"Inexistent number.");
332 dimension =
sizeof(representation[0]) /
sizeof(representation[0][0]) + 1;
GameAction
Actions allowed in the 8-puzzle and its variants.
bool operator<(const GameState &other) const
Compares two GameState objects.
int * toIntArray(string s)
Turns a string representation of an 8-puzzle into an array.
int countInversions()
Counts the number of inversions in the puzzle.
bool operator==(const GameState &other) const
Checks whether two GameState objects are equal.
GameState(string s)
Creates a GameState from a string, where tile numbers are separated by spaces in a single line...
GameState(GameState *previous, GameAction action)
Creates a new state based on a given previous state and applying an action to it. ...
string to_line_string() const
Returns a single line string representation of the 8-puzzle board state.
int * find(int value)
Finds the position of a tile in the board.
bool isSolvable()
Check if the state is solvable.
bool isValid(GameAction action)
Checks whether an action is valid for this state.
GameState()
Default constructor, everything starts as 0 or NULL.
void setRepresentation(int **representation)
GameState * getParent() const
Describes a single state in the 8-puzzle.
~GameState()
Destructor of GameState.
GameState(const GameState &obj)
Copy constructor of the class.
string to_string() const
Returns a multi-line string representation of the 8-puzzle board state.
bool operator!=(const GameState &other) const
Inequality operator for GameState.