|
enum class | Direction { UP
, DOWN
, LEFT
, RIGHT
} |
|
◆ PuzzleState() [1/2]
PuzzleState::PuzzleState |
( |
| ) |
|
Default constructor for the puzzle state.
This should initialize the puzzle to the solved state.
◆ PuzzleState() [2/2]
PuzzleState::PuzzleState |
( |
const std::array< char, 16 > |
state | ) |
|
Custom constructor for the puzzle state.
Invalid inputs should initialize the puzzle representing all zeros. A char is used to represent the value of each tile, where 0 represents the empty space, it is also typically how a byte is represented in C/C++. Almost always, a char will represent a octet (8 bits), which allow for 256 possible values, in this case, since each tile can only have a value from 0-15 (4-bits), using a char is sufficient.
- Parameters
-
state | The starting state of the puzzle. The first entry in the array is the top left tile, then the remaining entries are in row-major order. The following is a depiction of a puzzle where the value of each tile represents its index in the array: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
◆ asArray()
std::array< char, 16 > PuzzleState::asArray |
( |
| ) |
const |
Convert the puzzle state to an array.
- Returns
- an array representing the state of the puzzle in the same format as described in the constructor.
◆ getNeighbor()
PuzzleState PuzzleState::getNeighbor |
( |
Direction |
direction | ) |
const |
Get the neighbor specified by the direction.
If the direction refers to an invalid neighbor, return PuzzleState representing all zeros.
- Parameters
-
direction | The direction to move a tile (e.x. UP means the empty space should move down). |
◆ getNeighbors()
std::vector< PuzzleState > PuzzleState::getNeighbors |
( |
| ) |
const |
Gets all possible PuzzleStates that result from a single move.
- Returns
- All possible next PuzzleStates in any order
◆ manhattanDistance()
Calculates the "manhattan distance" between the current state and the goal state.
This is the sum of the manhattan distances of each tile's current location to its goal location.
- Parameters
-
desiredState | The state to calculate the distance to |
- Returns
- The manhattan distance between the current and goal states
◆ operator!=()
bool PuzzleState::operator!= |
( |
const PuzzleState & |
rhs | ) |
const |
Overloaded operator!= for the puzzle state.
- Parameters
-
rhs | The puzzle state to compare to |
◆ operator<()
bool PuzzleState::operator< |
( |
const PuzzleState & |
rhs | ) |
const |
Overloaded operator< for the puzzle state.
The PuzzleState with the first tile value less than the corresponding tile in the other PuzzleState is considered less, you can assume both puzzle states are valid.
- Parameters
-
rhs | The puzzle state to compare to |
◆ operator==()
bool PuzzleState::operator== |
( |
const PuzzleState & |
rhs | ) |
const |
Overloaded operator== for the puzzle state.
Puzzles are equal when the value of each tile is the same.
- Parameters
-
rhs | The puzzle state to compare to |
The documentation for this class was generated from the following file: