Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
game.h
Go to the documentation of this file.
1#ifndef GAME_H
2#define GAME_H
3
4
5#include "ccfwd.h"
6#include "message.h"
7#include "piece.h"
8#include "pieces.h"
9#include "action_history.h"
11#include "fen_string.h"
12
13#include <iosfwd>
14#include <vector>
15#include <optional>
16
27class game
28{
29public:
30 explicit game(const std::vector<piece>& pieces = get_standard_starting_pieces());
31
33 auto& get_pieces() noexcept { return m_pieces; }
34
36 const auto& get_pieces() const noexcept { return m_pieces; }
37
40 const auto& get_in_game_time() const noexcept { return m_in_game_time; }
41
42 const auto& get_winner() const noexcept { return m_winner; }
43
49 void tick(const delta_t& dt);
50
51private:
52
54 std::vector<piece> m_pieces;
55
57 in_game_time m_in_game_time;
58
60 std::optional<chess_color> m_winner;
61
68 void check_all_occupied_squares_are_unique() const;
69
70
75 void check_game_and_pieces_agree_on_the_time() const;
76
78 void check_if_there_is_a_winner();
79
83 void tick_impl(const delta_t& dt);
84
86};
87
89bool can_castle_kingside(const piece& p, const game& g) noexcept;
90
92bool can_castle_queenside(const piece& p, const game& g) noexcept;
93
97bool can_do_attack(
98 const game& g,
99 const piece& selected_piece,
100 const square& cursor_square,
101 const chess_color player_color
102);
103
106 const game& g,
107 const piece& selected_piece,
108 const chess_color player_color
109);
110
113 const game& g,
114 const piece& selected_piece,
115 const chess_color player_color
116);
117
122 const game& g,
123 const piece& selected_piece,
124 const square& cursor_square,
125 const chess_color player_color
126);
127
129bool can_do_move(
130 const game& g,
131 const piece& selected_piece,
132 const square& cursor_square,
133 const chess_color player_color
134);
135
137bool can_do_promote(
138 const piece& selected_piece,
139 const chess_color player_color
140);
141
144void clear_piece_messages(game& g) noexcept;
145
149
154std::vector<piece_action> collect_all_piece_actions(const game& g);
155
158std::vector<piece_action> collect_all_piece_actions(
159 const game& g,
160 const chess_color player_color
161);
162
165std::vector<piece_action> collect_all_piece_actions(
166 const game& g,
167 const piece& p
168);
169
172std::vector<piece_action> collect_all_bishop_actions(
173 const game& g,
174 const piece& p
175);
176
179std::vector<piece_action> collect_all_king_actions(
180 const game& g,
181 const piece& p
182);
183
186std::vector<piece_action> collect_all_knight_actions(
187 const game& g,
188 const piece& p
189);
190
193std::vector<piece_action> collect_all_pawn_actions(
194 const game& g,
195 const piece& p
196);
197
200std::vector<piece_action> collect_all_pawn_attack_actions(
201 const game& g,
202 const piece& p
203);
204
207std::vector<piece_action> collect_all_pawn_en_passant_actions(
208 const game& g,
209 const piece& p
210);
211
214std::vector<piece_action> collect_all_pawn_move_actions(
215 const game& g,
216 const piece& p
217);
218
221std::vector<piece_action> collect_all_queen_actions(
222 const game& g,
223 const piece& p
224);
225
228std::vector<piece_action> collect_all_rook_actions(
229 const game& g,
230 const piece& p
231);
232
234std::vector<message> collect_messages(const game& g) noexcept;
235
239std::vector<message_type> collect_message_types(const game& g) noexcept;
240
242int count_piece_actions(const game& g);
243
246 const game& g,
247 const chess_color player
248);
249
252
256
261 const race lhs_race = race::classic,
262 const race rhs_race = race::classic
263) noexcept;
264
266std::vector<piece> find_pieces(
267 const game& g,
268 const piece_type type,
269 const chess_color color
270);
271
273const piece& get_closest_piece_to(const game& g, const game_coordinate& coordinat);
274
276piece& get_closest_piece_to(game& g, const game_coordinate& coordinat);
277
278
279
282 const game& g,
283 const game_coordinate& coordinat
284);
285
286/*
289game get_kings_only_game() noexcept;
290*/
300std::vector<square> get_unique_occupied_squares(const game& g) noexcept;
301
311
312std::vector<square> get_occupied_squares(const game& g) noexcept;
313
316const piece& get_piece_at(const game& g, const square& coordinat);
317
320const piece& get_piece_at(const game& g, const std::string& square_str);
321
324piece& get_piece_at(game& g, const square& coordinat);
325
328piece& get_piece_at(game& g, const std::string& square_str);
329
332const piece& get_piece_with_id(const game& g, const piece_id& id);
333
336piece& get_piece_with_id(game& g, const piece_id& id);
337
339std::vector<piece>& get_pieces(game& g) noexcept;
340
342const std::vector<piece>& get_pieces(const game& g) noexcept;
343
345const in_game_time& get_time(const game& g) noexcept;
346
349 const game& g,
350 const piece_id& id
351);
352
354bool is_draw(const game& g);
355
357bool is_empty(const game& g, const square& s) noexcept;
358
362 const game& g,
363 const square& from,
364 const square& to
365);
366
370 const game& g,
371 const std::string& from_square_str,
372 const std::string& to_square_str
373);
374
376bool is_idle(const game& g) noexcept;
377
379bool is_piece_at(
380 const game& g,
381 const game_coordinate& coordinat,
382 const double distance = 0.5
383);
384
386bool is_piece_at(
387 const game& g,
388 const square& coordinat
389);
390
392bool is_piece_at(
393 const game& g,
394 const std::string& square_str
395);
396
406 const game& g,
407 const square& s,
408 const chess_color attacker_color
409);
410
414void tick(game& g, const delta_t dt);
415
417void tick_until_idle(game& g);
418
419std::string to_board_str(
420 const game& g,
422) noexcept;
423
426
432std::string to_pgn(const game& g);
433
434std::ostream& operator<<(std::ostream& os, const game& g) noexcept;
435
436#endif // GAME_H
chess_color
A chess piece color.
Definition chess_color.h:10
The collected in-game times of actions, in chrononical order.
Definition action_history.h:17
Options on how to convert a chessboard to text.
Definition board_to_text_options.h:10
A change of in_game_time}.
Definition delta_t.h:12
Forsyth–Edwards Notation string.
Definition fen_string.h:20
An exact coordinate anywhere on the board.
Definition game_coordinate.h:32
The game logic.
Definition game.h:28
auto & get_pieces() noexcept
Get all the pieces.
Definition game.h:33
const auto & get_pieces() const noexcept
Get all the pieces.
Definition game.h:36
const auto & get_in_game_time() const noexcept
Get the in-game time.
Definition game.h:40
void tick(const delta_t &dt)
Go to the next frame.
Definition game.cpp:1233
friend game create_game_with_starting_position(starting_position_type t) noexcept
const auto & get_winner() const noexcept
Definition game.h:42
The in-game time, in chess moves.
Definition in_game_time.h:16
A unique ID.
Definition piece_id.h:12
A chess piece.
Definition piece.h:24
A chess square.
Definition square.h:58
fen_string to_fen_string(const game &g)
Convert the game's position to a FEN string.
Definition game.cpp:1317
std::vector< piece_action > collect_all_pawn_actions(const game &g, const piece &p)
Collect all valid moves and attacks at a board for a focal pawn.
Definition game.cpp:564
bool can_castle_queenside(const piece &p, const game &g) noexcept
Can this piece castle queenside?
Definition game.cpp:73
std::vector< piece_action > collect_all_pawn_move_actions(const game &g, const piece &p)
Collect all valid move actions at a board for a focal pawn.
Definition game.cpp:731
const piece & get_closest_piece_to(const game &g, const game_coordinate &coordinat)
Get the piece that is closest to the coordinat.
Definition game.cpp:981
bool is_empty_between(const game &g, const square &from, const square &to)
Are all squares between these two squares empty? Returns true if the squares are adjacent.
Definition game.cpp:1168
bool can_do_castle_kingside(const game &g, const piece &selected_piece, const chess_color player_color)
Can a piece_action_type::castle_kingside action be done?
Definition game.cpp:147
std::vector< piece_action > collect_all_piece_actions(const game &g)
Collect all valid moves and attackes at a board for all pieces.
Definition game.cpp:298
void tick(game &g, const delta_t dt)
Call game::tick safely.
Definition game.cpp:1289
std::vector< piece > find_pieces(const game &g, const piece_type type, const chess_color color)
Find zero, one or more chess pieces of the specified type and color.
Definition game.cpp:961
bool is_draw(const game &g)
Determine if the game is a draw.
Definition game.cpp:1157
action_history collect_action_history(const game &g)
Collect the history of a game, i.e.
Definition game.cpp:293
bool can_do_promote(const piece &selected_piece, const chess_color player_color)
Can a piece_action_type::promote_to_bishop action be done?
Definition game.cpp:238
std::vector< message > collect_messages(const game &g) noexcept
Get all the sound effects to be processed.
Definition game.cpp:1080
bool can_do_castle_queenside(const game &g, const piece &selected_piece, const chess_color player_color)
Can a piece_action_type::castle_queenside action be done?
Definition game.cpp:159
std::vector< piece_action > collect_all_king_actions(const game &g, const piece &p)
Collect all valid moves and attacks at a board for a focal king.
Definition game.cpp:460
const piece & get_piece_with_id(const game &g, const piece_id &id)
Get the piece with a certain ID.
Definition game.cpp:1060
bool is_empty(const game &g, const square &s) noexcept
Is the square empty?
Definition game.cpp:1163
bool can_do_en_passant(const game &g, const piece &selected_piece, const square &cursor_square, const chess_color player_color)
Can a piece_action_type::en_passant action be done? This is not a regular attack.
Definition game.cpp:171
std::vector< piece_action > collect_all_queen_actions(const game &g, const piece &p)
Collect all valid moves and attacks at a board for a focal queen.
Definition game.cpp:854
std::vector< piece_action > collect_all_rook_actions(const game &g, const piece &p)
Collect all valid moves and attacks at a board for a focal rook.
Definition game.cpp:900
std::string to_pgn(const game &g)
Convert the played game to pseudo-PGN notation.
Definition game.cpp:1335
bool is_piece_at(const game &g, const game_coordinate &coordinat, const double distance=0.5)
Determine if there is a piece at the coordinat.
Definition game.cpp:1202
game create_game_with_standard_starting_position() noexcept
Create a game with all default settings and the default starting position.
Definition game.cpp:1005
std::string to_board_str(const game &g, const board_to_text_options &options=board_to_text_options()) noexcept
Definition game.cpp:1306
bool has_piece_with_id(const game &g, const piece_id &id)
Determine if there is a piece at the coordinat.
Definition game.cpp:1145
std::vector< piece > & get_pieces(game &g) noexcept
Get all the pieces.
Definition game.cpp:1050
bool can_do_attack(const game &g, const piece &selected_piece, const square &cursor_square, const chess_color player_color)
Can a piece_action_type::attack action be done? This is not an en-passant.
Definition game.cpp:109
int count_piece_actions(const game &g)
Count the total number of actions to be done by pieces of both players.
Definition game.cpp:946
bool can_castle_kingside(const piece &p, const game &g) noexcept
Can this piece castle kingside?
Definition game.cpp:37
const in_game_time & get_time(const game &g) noexcept
Get the time in the game.
Definition game.cpp:1140
bool is_idle(const game &g) noexcept
Are all pieces idle?
Definition game.cpp:1197
std::vector< message_type > collect_message_types(const game &g) noexcept
See if there are messages.
Definition game.cpp:1104
void clear_piece_messages(game &g) noexcept
Clear the sound effects to be processed, i.e.
Definition game.cpp:288
game create_game_with_starting_position(starting_position_type t, const race lhs_race=race::classic, const race rhs_race=race::classic) noexcept
Create a game with all default settings and a specific starting position.
Definition game.cpp:1010
std::vector< piece_action > collect_all_bishop_actions(const game &g, const piece &p)
Collect all valid moves and attacks at a board for a focal bishop.
Definition game.cpp:414
std::vector< square > get_occupied_squares(const game &g) noexcept
Get all the squares that are occupied, allowing duplicates.
Definition game.cpp:1038
bool can_do_move(const game &g, const piece &selected_piece, const square &cursor_square, const chess_color player_color)
Can a piece_action_type::move action be done?
Definition game.cpp:207
std::vector< square > get_unique_occupied_squares(const game &g) noexcept
Get all the squares that are occupied, disallowing duplicates.
Definition game.cpp:1033
std::vector< piece_action > collect_all_pawn_en_passant_actions(const game &g, const piece &p)
Collect all valid attack actions at a board for a focal pawn.
Definition game.cpp:650
std::vector< piece_action > collect_all_pawn_attack_actions(const game &g, const piece &p)
Collect all valid attack actions at a board for a focal pawn.
Definition game.cpp:584
int get_index_of_closest_piece_to(const game &g, const game_coordinate &coordinat)
Get the index of the piece that is closest to the coordinat.
Definition game.cpp:1020
void tick_until_idle(game &g)
Call game::tick until all pieces are idle.
Definition game.cpp:1295
std::vector< piece_action > collect_all_knight_actions(const game &g, const piece &p)
Collect all valid moves and attacks at a board for a focal knight.
Definition game.cpp:524
bool is_square_attacked(const game &g, const square &s, const chess_color attacker_color)
Determine if the square is attacked by (another) piece of a certain color.
Definition game.cpp:1224
game create_game_from_fen_string(const fen_string &s) noexcept
Create a game from a FEN string.
Definition game.cpp:997
const piece & get_piece_at(const game &g, const square &coordinat)
Get the piece that at that square, will throw if there is no piece.
Definition game.cpp:1119
piece_type
The type of chess piece.
Definition piece_type.h:9
std::vector< piece > get_standard_starting_pieces(const race white_race=race::classic, const race black_race=race::classic) noexcept
Get all the pieces in the starting position.
Definition pieces.cpp:916
race
The chess race.
Definition race.h:10
@ classic
starting_position_type
The starting position type.
Definition starting_position_type.h:10