Algorithms_in_C++ 1.0.0
Set of 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::ostreamoperator<< (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)

Constructor & Destructor Documentation

◆ EightPuzzle() [1/4]

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

Default constructor for EightPuzzle.

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....
Definition sparse_table.cpp:47

◆ 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
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
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
146 : board(std::move(A.board)) {}
T move(T... args)

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
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
176 {
177 auto zero_pos = find_zero();
178 // vector which will contain all possible state from current state
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 }
bool in_range(const uint32_t value) const
check whether the index value is bounded within the puzzle area
Definition a_star_search.cpp:90
std::pair< uint32_t, uint32_t > find_zero()
A helper array to evaluate the next state from current state;.
Definition a_star_search.cpp:75
std::vector< std::pair< int8_t, int8_t > > moves
N x N array to store the current state of the Puzzle.
Definition a_star_search.cpp:64
T emplace_back(T... args)
T swap(T... args)
Here is the call graph for this function:

◆ 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
102 {
103 if (in_range(i) && in_range(j)) {
104 return board[i][j];
105 }
106 return -1;
107 }
Here is the call graph for this function:

◆ 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.
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.

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
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
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 }
bool check(const std::string &s, const std::unordered_set< std::string > &strSet, int pos, std::vector< int > *dp)
Function that checks if the string passed in param can be segmented from position 'pos',...
Definition word_break.cpp:80

◆ 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
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
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
165 {
166 board = std::move(A.board);
167 return *this;
168 }
Here is the call graph for this function:

◆ 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
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
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

◆ 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.

64 {
65 {0, 1},
66 {1, 0},
67 {0, -1},
68 {-1,
69 0}}; /// A helper array to evaluate the next state from current state;

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