Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
game_controller.h
Go to the documentation of this file.
1#ifndef GAME_CONTROLLER_H
2#define GAME_CONTROLLER_H
3
4#include "ccfwd.h"
5#include "piece_action_type.h"
6#include "game_coordinate.h"
7#include "game.h"
8#include "lobby_options.h"
9#include "side.h"
10#include "user_inputs.h"
11
12#include <map>
13#include <iosfwd>
14
31{
32public:
33 explicit game_controller(
34 const game& g = game(),
35 const lobby_options& lo = lobby_options()
36 );
37
39 void add_user_input(const user_input& a);
40
43
45 const game_coordinate& get_cursor_pos(const side player_side) const noexcept;
46
48 const game& get_game() const noexcept { return m_game; }
49
51 game& get_game() noexcept { return m_game; }
52
54 const auto& get_lobby_options() const noexcept { return m_lobby_options; }
55
57 const std::optional<piece_id>& get_selected_piece_id(const side s) const noexcept;
58
60 const auto& get_user_inputs() const noexcept { return m_user_inputs; }
61
63 const physical_controller& get_physical_controller(const side player_side) const noexcept;
64
66 void set_cursor_pos(const game_coordinate& pos, const side player_side) noexcept;
67
69 void set_selected_piece_id(const side s, const std::optional<piece_id>& selected_piece_id) noexcept;
70
72 void tick(const delta_t& dt = delta_t(1.0));
73
74private:
75
77 std::map<side, game_coordinate> m_cursor_pos;
78
80 game m_game;
81
83 lobby_options m_lobby_options;
84
86 std::map<side, std::optional<piece_id>> m_selected_piece_id;
87
89 user_inputs m_user_inputs;
90
92 void apply_action_type_to_game(game& g, const piece_action_type t, const side s);
93
95 void apply_action_type_attack_to_game(game& g, const side s);
96 void apply_action_type_attack_en_passant_to_game(game& g, const side s);
97 void apply_action_type_castle_kingside_to_game(game& g, const side s);
98 void apply_action_type_castle_queenside_to_game(game& g, const side s);
99 void apply_action_type_move_to_game(game& g, const side s);
100 void apply_action_type_promote_to_game(game& g, const piece_type promote_to_type, const side s);
101 void apply_action_type_select_to_game(game& g, const side s);
102 void apply_action_type_unselect_to_game(game& g, const side s);
103
104 friend class game_view;
105};
106
108void add_user_input(game_controller& c, const user_input& input);
109
111void add_user_inputs(game_controller& c, const user_inputs& input);
112
113
115bool can_attack(
116 const game_controller& c,
117 const side player_side
118) noexcept;
119
122 const game_controller& c,
123 const side player_side
124) noexcept;
125
128 const game_controller& c,
129 const side player_side
130) noexcept;
131
134 const game_controller& c,
135 const side player_side
136) noexcept;
137
138bool can_move(
139 const game_controller& c,
140 const side player_side
141) noexcept;
142
145 const game_controller& c,
146 const chess_color cursor_color
147);
148
151 const game_controller& c,
152 const side player_side
153);
154
155bool can_promote(
156 const game_controller& c,
157 const side player_side
158) noexcept;
159
160
161bool can_select(
162 const game_controller& c,
163 const side player_side
164) noexcept;
165
166bool can_unselect(
167 const game_controller& c,
168 const side player_side
169) noexcept;
170
172std::vector<message> collect_messages(const game_controller& g) noexcept;
173
175std::vector<piece_id> collect_selected_piece_ids(const game_controller& c);
176
180 const game_controller& c,
181 const chess_move& move,
183);
184
187
190 const game_controller& g,
191 const chess_color player
192);
193
198 const game_controller& c,
199 const chess_color player_color
200);
201
204int count_user_inputs(const game_controller& c) noexcept;
205
207 const game_controller& c,
208 const side s
209);
210
213 const game_controller& c,
214 const side player_side
215) noexcept;
216
219 const game_controller& c,
220 const chess_color cursor_color
221);
222
225 const game_controller& c,
226 const side player_side
227);
228
230 const game_controller& c
231) noexcept;
232
237std::vector<piece_action_type> get_piece_actions(
238 const game_controller& c,
239 const side player_side
240) noexcept;
241
242piece& get_piece_at(game_controller& c, const std::string& square_str);
244
247std::vector<square> get_possible_moves(
248 const game_controller& c,
249 const side player
250);
251
256std::vector<piece> get_selected_pieces(
257 const game_controller& c,
258 const chess_color player
259);
260
265std::vector<piece> get_selected_pieces(
266 const game_controller& c,
267 const side player
268);
269
270
272const user_inputs& get_user_inputs(const game_controller& c) noexcept;
273
280 const game_controller& c,
281 const square& from,
282 const square& to,
283 const side player_side,
285);
286
289 const game_controller& c,
290 const square& to,
291 const side player_side,
293);
294
295const std::optional<chess_color>& get_winner(const game_controller& c) noexcept;
296
302 const game_controller& c,
303 const chess_color player_color
304);
305
308 const game_controller& c,
309 const side player_side
310);
311
313bool has_winner(const game_controller& c) noexcept;
314
316 const game_controller& c,
317 const side player_side
318) noexcept;
319
321bool is_draw(const game_controller& c);
322
324bool is_piece_at(
325 const game_controller& g,
326 const square& coordinat
327);
328
330bool is_piece_at(
331 const game_controller& g,
332 const std::string& square_str
333);
334
337bool is_selected(
338 const piece& p,
339 const game_controller& c
340);
341
351 const game_controller& c,
352 const square& s,
353 const chess_color attacker_color
354);
355
358void move_cursor_to(
360 const std::string& square_str,
361 const side player_side
362);
363
367void move_cursor_to(
369 const square& s,
370 const side player_side
371);
372
374game play_random_game(const int n_turns = 2, const int seed = 42);
375
377void set_cursor_pos(
379 const game_coordinate& pos,
380 const side player_side
381) noexcept;
382
385
386std::string to_board_str(
387 const game_controller& c,
389) noexcept;
390
391std::ostream& operator<<(std::ostream& os, const game_controller& g) noexcept;
392
393#endif // GAME_CONTROLLER_H
chess_color
A chess piece color.
Definition chess_color.h:10
Options on how to convert a chessboard to text.
Definition board_to_text_options.h:10
A chess move.
Definition chess_move.h:22
A change of in_game_time}.
Definition delta_t.h:12
The class that acts as a controller for game}.
Definition game_controller.h:31
void tick(const delta_t &dt=delta_t(1.0))
Make the game go forward.
Definition game_controller.cpp:2618
const auto & get_lobby_options() const noexcept
Get the game.
Definition game_controller.h:54
const game_coordinate & get_cursor_pos(const side player_side) const noexcept
Get the a player's cursor position.
Definition game_controller.cpp:747
void add_user_input(const user_input &a)
Add a user input. These will be processed in 'game::tick'.
Definition game_controller.cpp:30
const physical_controller & get_physical_controller(const side player_side) const noexcept
Get a player's physical controller.
game & get_game() noexcept
Get the game.
Definition game_controller.h:51
void set_selected_piece_id(const side s, const std::optional< piece_id > &selected_piece_id) noexcept
Set the selected square, if any.
Definition game_controller.cpp:1101
const game & get_game() const noexcept
Get the game.
Definition game_controller.h:48
const std::optional< piece_id > & get_selected_piece_id(const side s) const noexcept
Get the selected squares, if any.
Definition game_controller.cpp:884
void set_cursor_pos(const game_coordinate &pos, const side player_side) noexcept
Set a player's cursor's position.
Definition game_controller.cpp:1094
const auto & get_user_inputs() const noexcept
Get the game users' inputs.
Definition game_controller.h:60
void apply_user_inputs_to_game()
Process all actions and apply these on the game.
Definition game_controller.cpp:48
An exact coordinate anywhere on the board.
Definition game_coordinate.h:32
The Game dialog.
Definition game_view.h:27
The game logic.
Definition game.h:28
The in-game time, in chess moves.
Definition in_game_time.h:16
The options that are set in the lobby.
Definition lobby_options.h:16
A physical controller.
Definition physical_controller.h:19
A chess piece.
Definition piece.h:24
A chess square.
Definition square.h:58
A user input.
Definition user_input.h:18
A collection of user inputs.
Definition user_inputs.h:26
bool can_player_select_piece_at_cursor_pos(const game_controller &c, const chess_color cursor_color)
Can the player select a piece at the current cursor position?
Definition game_controller.cpp:612
bool has_selected_pieces(const game_controller &c, const chess_color player_color)
See if there is at least 1 piece selected.
Definition game_controller.cpp:966
void add_user_inputs(game_controller &c, const user_inputs &input)
Add a user_inputs. These will be processed in 'game::tick'.
Definition game_controller.cpp:40
int count_piece_actions(const game_controller &g)
Count the total number of actions to be done by pieces of both players.
bool is_selected(const piece &p, const game_controller &c)
Determine if the piece is selected, based on its action history.
Definition game_controller.cpp:1029
bool is_cursor_on_selected_piece(const game_controller &c, const side player_side) noexcept
Definition game_controller.cpp:987
int count_user_inputs(const game_controller &c) noexcept
Count the total number of user_input}s to be done by the game_controller}.
Definition game_controller.cpp:742
bool can_attack_en_passant(const game_controller &c, const side player_side) noexcept
Can the player do an en-passant attack?
Definition game_controller.cpp:521
bool can_move(const game_controller &c, const side player_side) noexcept
Definition game_controller.cpp:594
std::vector< message > collect_messages(const game_controller &g) noexcept
Get all the sound effects to be processed.
std::vector< piece_action_type > get_piece_actions(const game_controller &c, const side player_side) noexcept
Get the types of actions, if any, a player could by pressing an action key.
Definition game_controller.cpp:785
square get_cursor_square(const game_controller &c, const side player_side)
Get the a player's cursor position, as a square}.
Definition game_controller.cpp:768
const game_coordinate & get_cursor_pos(const game_controller &c, const side player_side) noexcept
Get the a player's cursor position.
Definition game_controller.cpp:752
user_inputs convert_move_to_user_inputs(const game_controller &c, const chess_move &move, const physical_controller_type t)
Convert a chess move, e.g.
Definition game_controller.cpp:690
bool is_draw(const game_controller &c)
Determine if the game is a draw.
Definition game_controller.cpp:1001
game play_random_game(const int n_turns=2, const int seed=42)
Play a random game, used for profiling.
Definition game_controller.cpp:1066
const user_inputs & get_user_inputs(const game_controller &c) noexcept
Get the game users' inputs.
Definition game_controller.cpp:889
bool can_castle_queenside(const game_controller &c, const side player_side) noexcept
Can the player castle queenside?
Definition game_controller.cpp:574
user_inputs get_user_inputs_to_move_cursor_from_to(const game_controller &c, const square &from, const square &to, const side player_side, const physical_controller_type t)
Create the user inputs to move the cursor to a target square knowing it will be at the 'from' square.
Definition game_controller.cpp:894
const in_game_time & get_in_game_time(const game_controller &c) noexcept
Definition game_controller.cpp:778
bool can_select(const game_controller &c, const side player_side) noexcept
Definition game_controller.cpp:661
int count_selected_units(const game_controller &c, const chess_color player_color)
Count the number of selected units.
Definition game_controller.cpp:737
const std::optional< chess_color > & get_winner(const game_controller &c) noexcept
Definition game_controller.cpp:961
void add_user_input(game_controller &c, const user_input &input)
Add a user input. These will be processed in 'game::tick'.
Definition game_controller.cpp:35
bool has_winner(const game_controller &c) noexcept
Does the game have a winner?
Definition game_controller.cpp:982
std::vector< piece_id > collect_selected_piece_ids(const game_controller &c)
Collect all the piece IDs of the selected pieces, if any.
Definition game_controller.cpp:677
bool can_attack(const game_controller &c, const side player_side) noexcept
Can the player attack?
Definition game_controller.cpp:494
void test_game_controller()
Test this class and its free functions.
Definition game_controller.cpp:1109
bool can_promote(const game_controller &c, const side player_side) noexcept
Definition game_controller.cpp:647
std::vector< square > get_possible_moves(const game_controller &c, const side player)
Get the possible moves for a player's selected pieces Will be empty if no pieces are selected.
Definition game_controller.cpp:850
bool can_unselect(const game_controller &c, const side player_side) noexcept
Definition game_controller.cpp:669
std::string to_board_str(const game_controller &c, const board_to_text_options &options=board_to_text_options()) noexcept
Definition game_controller.cpp:2623
user_inputs get_user_inputs_to_move_cursor_to(const game_controller &c, const square &to, const side player_side, const physical_controller_type t)
Create the user inputs to move the cursor to a target square.
Definition game_controller.cpp:926
bool can_castle_kingside(const game_controller &c, const side player_side) noexcept
Can the player castle kingside?
Definition game_controller.cpp:548
void set_cursor_pos(game_controller &c, const game_coordinate &pos, const side player_side) noexcept
Set the cursor's position to the target position.
Definition game_controller.cpp:1085
void move_cursor_to(game_controller &c, const std::string &square_str, const side player_side)
Put the cursor (i.e.
Definition game_controller.cpp:1048
piece & get_piece_at(game_controller &c, const std::string &square_str)
Definition game_controller.cpp:839
std::vector< piece > get_selected_pieces(const game_controller &c, const chess_color player)
Get all the selected pieces.
Definition game_controller.cpp:863
bool is_square_attacked(const game_controller &c, const square &s, const chess_color attacker_color)
Determine if the square is attacked by (another) piece of a certain color.
Definition game_controller.cpp:1039
bool is_piece_at(const game_controller &g, const square &coordinat)
Determine if there is a piece at the coordinat.
Definition game_controller.cpp:1006
chess_color get_color(const game_controller &c, const side s)
physical_controller_type
The type of controller.
Definition physical_controller_type.h:15
piece_action_type
The type of actions a piece can do.
Definition piece_action_type.h:9
piece_type
The type of chess piece.
Definition piece_type.h:9
side
The side the player/piece is on.
Definition side.h:11