TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
machine_learning::aystar_search::EightPuzzle< N > Class Template Reference

A class defining EightPuzzle/15-Puzzle game. More...

Collaboration diagram for machine_learning::aystar_search::EightPuzzle< N >:
[legend]

Public Member Functions

uint32_t get (size_t i, size_t j) const
 get the value from i units from right and j units from left side of the board
 
std::array< std::array< uint32_t, N >, N > get_state ()
 Returns the current state of the board.
 
size_t get_size () const
 returns the size of the EightPuzzle (number of row / column)
 
 EightPuzzle ()
 Default constructor for EightPuzzle.
 
 EightPuzzle (const std::array< std::array< uint32_t, N >, N > &init)
 Parameterized Constructor for EightPuzzle.
 
 EightPuzzle (const EightPuzzle< N > &A)
 Copy constructor.
 
 EightPuzzle (const EightPuzzle< N > &&A) noexcept
 Move constructor.
 
 ~EightPuzzle ()=default
 Destructor of EightPuzzle.
 
EightPuzzleoperator= (const EightPuzzle &A)
 Copy assignment operator.
 
EightPuzzleoperator= (EightPuzzle &&A) noexcept
 Move assignment operator.
 
std::vector< EightPuzzle< N > > generate_possible_moves ()
 Find all possible states after processing all possible moves, given the current state of the puzzle.
 
bool operator== (const EightPuzzle< N > &check) const
 check whether two boards are equal
 
bool operator< (const EightPuzzle< N > &check) const
 check whether one board is lexicographically smaller
 
bool operator<= (const EightPuzzle< N > &check) const
 check whether one board is lexicographically smaller or equal
 

Private Member Functions

std::pair< uint32_t, uint32_t > find_zero ()
 A helper array to evaluate the next state from current state;.
 
bool in_range (const uint32_t value) const
 check whether the index value is bounded within the puzzle area
 

Private Attributes

std::array< std::array< uint32_t, N >, N > board
 
std::vector< std::pair< int8_t, int8_t > > moves
 N x N array to store the current state of the Puzzle.
 

Friends

std::ostream & operator<< (std::ostream &op, const EightPuzzle< N > &SomeState)
 friend operator to display EightPuzzle<>
 

Detailed Description

template<size_t N = 3>
class machine_learning::aystar_search::EightPuzzle< N >

A class defining EightPuzzle/15-Puzzle game.

A well known 3 x 3 puzzle of the form 1 2 3 4 5 6 7 8 0 where 0 represents an empty space in the puzzle Given any random state, the goal is to achieve the above configuration (or any other configuration if possible)

Template Parameters
Nsize of the square Puzzle, default is set to 3 (since it is EightPuzzle)

Definition at line 60 of file a_star_search.cpp.

Constructor & Destructor Documentation

◆ EightPuzzle() [1/4]

template<size_t N = 3>
machine_learning::aystar_search::EightPuzzle< N >::EightPuzzle ( )
inline

Default constructor for EightPuzzle.

Definition at line 121 of file a_star_search.cpp.

121 {
122 for (size_t i = 0; i < N; ++i) {
123 for (size_t j = 0; j < N; ++j) {
124 board[i][j] = ((i * 3 + j + 1) % (N * N));
125 }
126 }
127 }
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....

◆ EightPuzzle() [2/4]

template<size_t N = 3>
machine_learning::aystar_search::EightPuzzle< N >::EightPuzzle ( const std::array< std::array< uint32_t, N >, N > & init)
inlineexplicit

Parameterized Constructor for EightPuzzle.

Parameters
inita 2-dimensional array denoting a puzzle configuration

Definition at line 132 of file a_star_search.cpp.

133 : board(init) {}

◆ EightPuzzle() [3/4]

template<size_t N = 3>
machine_learning::aystar_search::EightPuzzle< N >::EightPuzzle ( const EightPuzzle< N > & A)
inline

Copy constructor.

Parameters
Aa reference of an EightPuzzle

Definition at line 139 of file a_star_search.cpp.

139: board(A.board) {}

◆ EightPuzzle() [4/4]

template<size_t N = 3>
machine_learning::aystar_search::EightPuzzle< N >::EightPuzzle ( const EightPuzzle< N > && A)
inlinenoexcept

Move constructor.

Parameters
Aa reference of an EightPuzzle

Definition at line 145 of file a_star_search.cpp.

146 : board(std::move(A.board)) {}

Member Function Documentation

◆ find_zero()

template<size_t N = 3>
std::pair< uint32_t, uint32_t > machine_learning::aystar_search::EightPuzzle< N >::find_zero ( )
inlineprivate

A helper array to evaluate the next state from current state;.

Finds an empty space in puzzle (in this case; a zero)

Returns
a pair indicating integer distances from top and right respectively, else returns -1, -1

Definition at line 75 of file a_star_search.cpp.

75 {
76 for (size_t i = 0; i < N; ++i) {
77 for (size_t j = 0; j < N; ++j) {
78 if (!board[i][j]) {
79 return {i, j};
80 }
81 }
82 }
83 return {-1, -1};
84 }

◆ generate_possible_moves()

template<size_t N = 3>
std::vector< EightPuzzle< N > > machine_learning::aystar_search::EightPuzzle< N >::generate_possible_moves ( )
inline

Find all possible states after processing all possible moves, given the current state of the puzzle.

Returns
list of vector containing all possible next moves
Note
the implementation is compulsory to create A* search

Definition at line 176 of file a_star_search.cpp.

176 {
177 auto zero_pos = find_zero();
178 // vector which will contain all possible state from current state
179 std::vector<EightPuzzle<N>> NewStates;
180 for (auto &move : moves) {
181 if (in_range(zero_pos.first + move.first) &&
182 in_range(zero_pos.second + move.second)) {
183 // swap with the possible moves
184 std::array<std::array<uint32_t, N>, N> new_config = board;
185 std::swap(new_config[zero_pos.first][zero_pos.second],
186 new_config[zero_pos.first + move.first]
187 [zero_pos.second + move.second]);
188 EightPuzzle<N> new_state(new_config);
189 // Store new state and calculate heuristic value, and depth
190 NewStates.emplace_back(new_state);
191 }
192 }
193 return NewStates;
194 }
EightPuzzle()
Default constructor for EightPuzzle.
bool in_range(const uint32_t value) const
check whether the index value is bounded within the puzzle area
std::pair< uint32_t, uint32_t > find_zero()
A helper array to evaluate the next state from current state;.
std::vector< std::pair< int8_t, int8_t > > moves
N x N array to store the current state of the Puzzle.

◆ get()

template<size_t N = 3>
uint32_t machine_learning::aystar_search::EightPuzzle< N >::get ( size_t i,
size_t j ) const
inline

get the value from i units from right and j units from left side of the board

Parameters
iinteger denoting ith row
jinteger denoting column
Returns
non-negative integer denoting the value at ith row and jth column
-1 if invalid i or j position

Definition at line 102 of file a_star_search.cpp.

102 {
103 if (in_range(i) && in_range(j)) {
104 return board[i][j];
105 }
106 return -1;
107 }

◆ get_size()

template<size_t N = 3>
size_t machine_learning::aystar_search::EightPuzzle< N >::get_size ( ) const
inline

returns the size of the EightPuzzle (number of row / column)

Returns
N, the size of the puzzle.

Definition at line 117 of file a_star_search.cpp.

117{ return N; }

◆ get_state()

template<size_t N = 3>
std::array< std::array< uint32_t, N >, N > machine_learning::aystar_search::EightPuzzle< N >::get_state ( )
inline

Returns the current state of the board.

Definition at line 111 of file a_star_search.cpp.

111{ return board; }

◆ in_range()

template<size_t N = 3>
bool machine_learning::aystar_search::EightPuzzle< N >::in_range ( const uint32_t value) const
inlineprivate

check whether the index value is bounded within the puzzle area

Parameters
valueindex for the current board
Returns
true if index is within the board, else false

Definition at line 90 of file a_star_search.cpp.

90{ return value < N; }

◆ operator<()

template<size_t N = 3>
bool machine_learning::aystar_search::EightPuzzle< N >::operator< ( const EightPuzzle< N > & check) const
inline

check whether one board is lexicographically smaller

Returns
true if this->state is lexicographically smaller than check.state, else false

Definition at line 218 of file a_star_search.cpp.

218 {
219 for (size_t i = 0; i < N; ++i) {
220 for (size_t j = 0; j < N; ++j) {
221 if (board[i][j] != check.board[i][j]) {
222 return board[i][j] < check.board[i][j];
223 }
224 }
225 }
226 return false;
227 }

◆ operator<=()

template<size_t N = 3>
bool machine_learning::aystar_search::EightPuzzle< N >::operator<= ( const EightPuzzle< N > & check) const
inline

check whether one board is lexicographically smaller or equal

Returns
true if this->state is lexicographically smaller than check.state or same, else false

Definition at line 233 of file a_star_search.cpp.

233 {
234 for (size_t i = 0; i < N; ++i) {
235 for (size_t j = 0; j < N; ++j) {
236 if (board[i][j] != check.board[i][j]) {
237 return board[i][j] < check.board[i][j];
238 }
239 }
240 }
241 return true;
242 }

◆ operator=() [1/2]

template<size_t N = 3>
EightPuzzle & machine_learning::aystar_search::EightPuzzle< N >::operator= ( const EightPuzzle< N > & A)
inline

Copy assignment operator.

Parameters
Aa reference of an EightPuzzle

Definition at line 156 of file a_star_search.cpp.

156 {
157 board = A.board;
158 return *this;
159 }

◆ operator=() [2/2]

template<size_t N = 3>
EightPuzzle & machine_learning::aystar_search::EightPuzzle< N >::operator= ( EightPuzzle< N > && A)
inlinenoexcept

Move assignment operator.

Parameters
Aa reference of an EightPuzzle

Definition at line 165 of file a_star_search.cpp.

165 {
166 board = std::move(A.board);
167 return *this;
168 }

◆ operator==()

template<size_t N = 3>
bool machine_learning::aystar_search::EightPuzzle< N >::operator== ( const EightPuzzle< N > & check) const
inline

check whether two boards are equal

Returns
true if check.state is equal to this->state, else false

Definition at line 200 of file a_star_search.cpp.

200 {
201 if (check.get_size() != N) {
202 return false;
203 }
204 for (size_t i = 0; i < N; ++i) {
205 for (size_t j = 0; j < N; ++j) {
206 if (board[i][j] != check.board[i][j]) {
207 return false;
208 }
209 }
210 }
211 return true;
212 }

Friends And Related Symbol Documentation

◆ operator<<

template<size_t N = 3>
std::ostream & operator<< ( std::ostream & op,
const EightPuzzle< N > & SomeState )
friend

friend operator to display EightPuzzle<>

Parameters
opostream object
SomeStatea certain state.
Returns
ostream operator op

Definition at line 250 of file a_star_search.cpp.

251 {
252 for (size_t i = 0; i < N; ++i) {
253 for (size_t j = 0; j < N; ++j) {
254 op << SomeState.board[i][j] << " ";
255 }
256 op << "\n";
257 }
258 return op;
259 }

Member Data Documentation

◆ board

template<size_t N = 3>
std::array<std::array<uint32_t, N>, N> machine_learning::aystar_search::EightPuzzle< N >::board
private

Definition at line 62 of file a_star_search.cpp.

◆ moves

template<size_t N = 3>
std::vector<std::pair<int8_t, int8_t> > machine_learning::aystar_search::EightPuzzle< N >::moves
private
Initial value:
= {
{0, 1},
{1, 0},
{0, -1},
{-1,
0}}

N x N array to store the current state of the Puzzle.

Definition at line 64 of file a_star_search.cpp.

64 {
65 {0, 1},
66 {1, 0},
67 {0, -1},
68 {-1,
69 0}};

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