Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
square.h
Go to the documentation of this file.
1#ifndef SQUARE_H
2#define SQUARE_H
3
4#include "ccfwd.h"
5#include "chess_color.h"
6#include "castling_type.h"
7#include "piece_action_type.h"
8
9#include <iosfwd>
10#include <random>
11#include <string>
12#include <vector>
13
57class square
58{
59public:
60 explicit square(const std::string& pos);
61 explicit square(const game_coordinate& g);
68 explicit square(const int x, const int y);
69
74 int get_x() const noexcept { return m_x; }
75
80 int get_y() const noexcept { return m_y; }
81
82private:
83
84 int m_x;
85 int m_y;
86};
87
91bool are_adjacent(const square& a, const square& b) noexcept;
92
96bool are_adjacent_for_knight(const square& a, const square& b) noexcept;
97
99bool are_all_unique(std::vector<square> squares);
100
107bool are_at_knights_jump_distance(const square& a, const square& b) noexcept;
108
113 const square& attacker_square,
114 const square& target_square,
115 const chess_color player_color
116);
117
119bool are_on_adjacent_diagonal(const square& a, const square& b) noexcept;
120
122bool are_on_same_diagonal(const square& a, const square& b) noexcept;
123
125bool are_on_same_file(const square& a, const square& b) noexcept;
126
133bool are_on_same_half_diagonal(const square& a, const square& b) noexcept;
134
136bool are_on_same_rank(const square& a, const square& b) noexcept;
137
139std::vector<std::pair<int, int>> collect_all_bishop_delta_pairs() noexcept;
140
143std::vector<std::vector<square>> collect_all_bishop_target_squares(const square& s) noexcept;
144
146std::vector<square> collect_all_king_target_squares(const square& s) noexcept;
147
149std::vector<std::pair<int, int>> collect_all_knight_delta_pairs() noexcept;
150
152std::vector<square> collect_all_knight_target_squares(const square& s) noexcept;
153
155std::vector<std::pair<int, int>> collect_all_queen_delta_pairs() noexcept;
156
159std::vector<std::vector<square>> collect_all_queen_target_squares(const square& s) noexcept;
160
162std::vector<std::pair<int, int>> collect_all_rook_delta_pairs() noexcept;
163
166std::vector<std::vector<square>> collect_all_rook_target_squares(const square& s) noexcept;
167
169std::vector<square> concatenate(
170 const std::vector<square>& a,
171 const std::vector<square>& b
172);
173
176 std::default_random_engine& rng_engine
177);
178
180std::vector<char> get_all_files() noexcept;
181
183std::vector<int> get_all_ranks() noexcept;
184
186std::vector<int> get_all_ranks_in_reversed_order() noexcept;
187
193 const square& pawn_square,
194 const chess_color color
195);
196
202 const square& attacker_square,
203 const square& target_square
204);
205
207char get_file(const square& s) noexcept;
208
214square get_initial_king_square(const chess_color player_color) noexcept;
215
216
225 const chess_color player_color,
226 const castling_type t
227) noexcept;
228
233 const square& from,
234 const square& to
235);
236
246square get_king_target_square(const chess_color player_color, const piece_action_type action);
247
249int get_rank(const square& s) noexcept;
250
260square get_rook_target_square(const chess_color player_color, const piece_action_type action);
261
263bool is_occupied(
264 const square& s,
265 const std::vector<square>& occupied_squares
266) noexcept;
267
269bool is_valid_square_xy(const int x, const int y) noexcept;
270
272bool is_invalid_square_xy(const int x, const int y) noexcept;
273
275void test_square();
276
278chess_color to_color(const square& s) noexcept;
279
280// a1 == (0.5, 0.5)
281// b1 == (0.5, 1.5)
282game_coordinate to_coordinat(const square& s) noexcept;
283
284// a1 == (0.5, 0.5)
285// b1 == (0.5, 1.5)
286game_coordinate to_coordinat(const std::string& notation) noexcept;
287
288
292game_rect to_game_rect(const square& s) noexcept;
293
295std::vector<square> to_squares(std::vector<std::pair<int, int>> xys);
296
297std::string to_str(const square& s) noexcept;
298
299std::string to_str(const std::vector<square>& s) noexcept;
300
301bool operator==(const square& lhs, const square& rhs) noexcept;
302bool operator!=(const square& lhs, const square& rhs) noexcept;
303bool operator<(const square& lhs, const square& rhs) noexcept;
304
305std::ostream& operator<<(std::ostream& os, const square& s) noexcept;
306std::ostream& operator<<(std::ostream& os, const std::vector<square>& s) noexcept;
307
308#endif // SQUARE_H
castling_type
The type of castling.
Definition castling_type.h:9
chess_color
A chess piece color.
Definition chess_color.h:10
An exact coordinate anywhere on the board.
Definition game_coordinate.h:32
A rectangle of game coordinates.
Definition game_rect.h:12
A chess square.
Definition square.h:58
int get_y() const noexcept
Get the y coordinat, starting from 0 for a1/a2/a3/etc.
Definition square.h:80
int get_x() const noexcept
Get the x coordinat, starting from 0 for a1/b1/c1/etc.
Definition square.h:74
piece_action_type
The type of actions a piece can do.
Definition piece_action_type.h:9
bool are_all_unique(std::vector< square > squares)
Are all the squares unique?
Definition square.cpp:50
bool is_valid_square_xy(const int x, const int y) noexcept
Can the x and y be used to create a valid square?
Definition square.cpp:542
square create_random_square(std::default_random_engine &rng_engine)
Create a random square.
Definition square.cpp:299
std::vector< square > collect_all_knight_target_squares(const square &s) noexcept
Collect all the possible target squares for a knight.
Definition square.cpp:192
std::vector< std::vector< square > > collect_all_queen_target_squares(const square &s) noexcept
Collect all the possible target squares for a queen in all directions.
Definition square.cpp:225
square get_behind(const square &pawn_square, const chess_color color)
Get the square behind a pawn.
Definition square.cpp:331
std::vector< std::pair< int, int > > collect_all_bishop_delta_pairs() noexcept
Collect all the delta squares for a bishop.
Definition square.cpp:115
bool are_en_passant_capture_squares(const square &attacker_square, const square &target_square, const chess_color player_color)
Are these possible en-passant capture squares?
Definition square.cpp:68
bool are_on_same_rank(const square &a, const square &b) noexcept
Are the squares on the same rank, e.g. a1 and a8.
Definition square.cpp:110
game_coordinate to_coordinat(const square &s) noexcept
Definition square.cpp:841
bool are_on_same_file(const square &a, const square &b) noexcept
Are the squares on the same file, e.g. e2 and e4.
Definition square.cpp:97
void test_square()
Test this class and its free functions.
Definition square.cpp:556
std::vector< square > to_squares(std::vector< std::pair< int, int > > xys)
Convert the pairs of x-y-coordinats to valid squares.
Definition square.cpp:867
bool are_on_adjacent_diagonal(const square &a, const square &b) noexcept
Are the squares adjacent on the same diagonal, e.g. d1 and e2.
Definition square.cpp:83
std::vector< int > get_all_ranks_in_reversed_order() noexcept
Get all the ranks in reversed order, i.e. 8 to and including 1.
Definition square.cpp:324
std::vector< std::vector< square > > collect_all_rook_target_squares(const square &s) noexcept
Collect all the possible target squares for a rook in all directions.
Definition square.cpp:261
std::vector< square > get_intermediate_squares(const square &from, const square &to)
Get the intermediate squares, in an inclusive way: the first square will be 'from',...
Definition square.cpp:382
bool are_at_knights_jump_distance(const square &a, const square &b) noexcept
Are the squares seperated by the jump of a knight?
Definition square.cpp:60
std::vector< square > concatenate(const std::vector< square > &a, const std::vector< square > &b)
Concatenate the vectors.
Definition square.cpp:287
std::vector< std::pair< int, int > > collect_all_knight_delta_pairs() noexcept
Collect all the delta squares for a knight.
Definition square.cpp:177
std::vector< std::pair< int, int > > collect_all_queen_delta_pairs() noexcept
Collect all the delta squares for a queen.
Definition square.cpp:211
std::vector< std::vector< square > > collect_all_bishop_target_squares(const square &s) noexcept
Collect all the possible target squares for a bishop in all directions.
Definition square.cpp:125
std::vector< square > collect_all_king_target_squares(const square &s) noexcept
Collect all the possible target squares for a king.
Definition square.cpp:151
square get_rook_target_square(const chess_color player_color, const piece_action_type action)
Get the rook target square when castling.
Definition square.cpp:495
std::vector< std::pair< int, int > > collect_all_rook_delta_pairs() noexcept
Collect all the delta squares for a rook.
Definition square.cpp:251
game_rect to_game_rect(const square &s) noexcept
Convert a square to a rectangle For example, a1 == ((0,0)-(1,1)) (notation is top-left,...
Definition square.cpp:854
bool are_on_same_half_diagonal(const square &a, const square &b) noexcept
Are the squares on the same half-diagonal, also 'the jump of a knight' e.g.
Definition square.cpp:102
bool is_occupied(const square &s, const std::vector< square > &occupied_squares) noexcept
Is the square 's' occupied?
Definition square.cpp:530
bool is_invalid_square_xy(const int x, const int y) noexcept
Can the x and y not be used to create a valid square?
Definition square.cpp:547
chess_color to_color(const square &s) noexcept
What is the color of this square?
Definition square.cpp:835
square get_initial_king_square(const chess_color player_color) noexcept
Get the intial king square.
Definition square.cpp:340
square get_king_target_square(const chess_color player_color, const piece_action_type action)
Get the king target square when castling.
Definition square.cpp:462
char get_file(const square &s) noexcept
Get the file of a square, e.g. 'd' from 'd4'.
Definition square.cpp:377
bool are_adjacent(const square &a, const square &b) noexcept
Are the squares adjacent (i.e.
Definition square.cpp:37
int get_rank(const square &s) noexcept
Get the rank of a square, e.g. '3' from 'e3'.
Definition square.cpp:490
std::vector< char > get_all_files() noexcept
Get all the files, i.e. 'a' to and including 'h'.
Definition square.cpp:314
std::string to_str(const square &s) noexcept
Definition square.cpp:894
std::vector< int > get_all_ranks() noexcept
Get all the ranks, i.e. 1 to and including 8.
Definition square.cpp:319
square get_en_passant_capture_square(const square &attacker_square, const square &target_square)
Get the square that may be captured by en-passant.
Definition square.cpp:364
bool are_on_same_diagonal(const square &a, const square &b) noexcept
Are the squares on the same diagonal, e.g. d1 and a4.
Definition square.cpp:90
bool are_adjacent_for_knight(const square &a, const square &b) noexcept
Are the squares adjacent for a knight.
Definition square.cpp:43
square get_initial_rook_square(const chess_color player_color, const castling_type t) noexcept
Get the default rook square, for a castling direction.
Definition square.cpp:347