mp_puzzle
Perplexing Puzzles
PuzzleState Class Reference

Public Types

enum class  Direction { UP , DOWN , LEFT , RIGHT }
 

Public Member Functions

 PuzzleState ()
 Default constructor for the puzzle state. More...
 
 PuzzleState (const std::array< uint8_t, 16 > state)
 Custom constructor for the puzzle state. More...
 
std::array< uint8_t, 16 > asArray () const
 Convert the puzzle state to an array. More...
 
bool operator== (const PuzzleState &rhs) const
 Overloaded operator== for the puzzle state. More...
 
bool operator!= (const PuzzleState &rhs) const
 Overloaded operator!= for the puzzle state. More...
 
bool operator< (const PuzzleState &rhs) const
 Overloaded operator< for the puzzle state. More...
 
PuzzleState getNeighbor (Direction direction) const
 Get the neighbor specified by the direction. More...
 
std::vector< PuzzleStategetNeighbors () const
 Gets all possible PuzzleStates that result from a single move. More...
 
int manhattanDistance (const PuzzleState desiredState=PuzzleState()) const
 Calculates the "manhattan distance" between the current state and the goal state. More...
 

Constructor & Destructor Documentation

◆ 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< uint8_t, 16 >  state)

Custom constructor for the puzzle state.

Invalid inputs should initialize the puzzle representing all zeros.

Parameters
stateThe 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

Member Function Documentation

◆ asArray()

std::array< uint8_t, 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
directionThe 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()

int PuzzleState::manhattanDistance ( const PuzzleState  desiredState = PuzzleState()) const

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
desiredStateThe 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
rhsThe 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.

Parameters
rhsThe 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
rhsThe puzzle state to compare to

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