Conquer Chess
Chess and Starcraft combined
Loading...
Searching...
No Matches
piece.h
Go to the documentation of this file.
1#ifndef PIECE_H
2#define PIECE_H
3
4
5#include "action_history.h"
6#include "delta_t.h"
7#include "chess_color.h"
8#include "piece_type.h"
9#include "piece_action.h"
10#include "piece_id.h"
11#include "message_type.h"
12#include "race.h"
13#include "read_only.h"
14
15#include <iosfwd>
16#include <string>
17#include <vector>
18
23class piece
24{
25public:
26 explicit piece(
27 const chess_color color,
28 const piece_type type,
29 const square& coordinat,
30 const race r = race::classic
31 );
32 explicit piece(
33 const chess_color color,
34 const piece_type type,
35 const std::string& coordinat,
36 const race r = race::classic
37 );
38
42 void add_action(const piece_action& action);
43
45 void add_message(const message_type& message);
46
48 void clear_messages() noexcept;
49
51 const auto& get_actions() const noexcept { return m_actions; }
52
54 auto& get_actions() noexcept { return m_actions; }
55
57 const auto& get_action_history() const noexcept { return m_action_history; }
58
60 const auto& get_color() const noexcept { return m_color.get_value(); }
61
65
66 const auto& get_current_square() const noexcept { return m_current_square; }
67
69 double get_health() const noexcept { return m_health; }
70
72 const piece_id& get_id() const noexcept { return m_id; }
73
75 const auto& get_in_game_time() const noexcept { return m_in_game_time; }
76
78 int get_kill_count() const noexcept { return m_kill_count; }
79
81 double get_max_health() const noexcept { return m_max_health; }
82
86 double get_max_shield() const;
87
89 const auto& get_messages() const noexcept { return m_messages; }
90
92 const auto& get_race() const noexcept { return m_race.get_value(); }
93
97 double get_shield() const;
98
100 const auto& get_type() const noexcept { return m_type.get_value(); }
101
103 bool has_moved() const noexcept { return m_has_moved; }
104
106 void increase_kill_count() noexcept { ++m_kill_count; }
107
110 bool is_enpassantable(const in_game_time& when) const;
111
112
115 void receive_damage(const double damage);
116
118 void set_current_action_progress(const delta_t& t) noexcept;
119
121 void set_current_square(const square& s) noexcept { m_current_square = s; }
122
123
130 void tick(
131 const delta_t& dt,
132 game& g
133 );
134
135private:
136
138 std::vector<piece_action> m_actions;
139
141 action_history m_action_history;
142
145
149 delta_t m_current_action_progress;
150
152 square m_current_square;
153
155 bool m_has_moved;
156
158 double m_health;
159
164 piece_id m_id;
165
167 in_game_time m_in_game_time;
168
170 int m_kill_count;
171
173 double m_max_health;
174
176 double m_max_shield;
177
179 std::vector<message_type> m_messages;
180
182 read_only<race> m_race;
183
185 double m_shield;
186
189};
190
194 const chess_color color,
195 const piece_type& p,
196 const square& from,
197 const square& to
198);
199
203 const piece_type& p,
204 const square& from,
205 const square& to,
206 const chess_color color
207);
208
212 const chess_color color,
213 const piece_type& p,
214 const square& from,
215 const square& to
216);
217
219bool can_promote(
220 const chess_color color,
221 const piece_type& p,
222 const square& s
223) noexcept;
224
226bool can_promote(const piece& p) noexcept;
227
229void clear_actions(piece& p);
230
232int count_piece_actions(const piece& p);
233
235std::string describe_actions(const piece& p);
236
238double get_f_health(const piece& p) noexcept;
239
243double get_f_shield(const piece& p) noexcept;
244
246square get_occupied_square(const piece& p) noexcept;
247
249piece get_test_white_king() noexcept;
250
252piece get_test_white_knight(const race r = race::classic) noexcept;
253
255bool has_actions(const piece& p) noexcept;
256
260 const piece& p,
261 const in_game_time& when
262) noexcept;
263
265bool has_moved(const piece& p) noexcept;
266
267
269bool is_dead(const piece& p) noexcept;
270
273 const piece& p,
274 const in_game_time& when
275);
276
278bool is_idle(const piece& p) noexcept;
279
281bool is_on_starting_rank(const chess_color c, const square& q) noexcept;
282
284bool is_pawn(const piece& p) noexcept;
285
287bool is_selected(const piece& p);
288
290void test_piece();
291
293void tick_attack(
294 piece& p,
295 const delta_t& dt,
296 game& g
297);
298
300 piece& p,
301 const delta_t& dt,
302 game& g
303);
304
307 piece& p,
308 const delta_t& dt,
309 game& g
310);
311
314 piece& p,
315 const delta_t& dt,
316 game& g
317);
318
320void tick_move(
321 piece& p,
322 const delta_t& dt,
323 game& g
324);
325
336char to_fen_char(const piece& p) noexcept;
337
338bool operator==(const piece& lhs, const piece& rhs) noexcept;
339bool operator!=(const piece& lhs, const piece& rhs) noexcept;
340
341std::ostream& operator<<(std::ostream& os, const piece& p) noexcept;
342
343#endif // PIECE_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
A change of in_game_time}.
Definition delta_t.h:12
The game logic.
Definition game.h:28
The in-game time, in chess moves.
Definition in_game_time.h:16
A message.
Definition message.h:15
An action to be done by a piece.
Definition piece_action.h:18
A unique ID.
Definition piece_id.h:12
A chess piece.
Definition piece.h:24
const piece_id & get_id() const noexcept
The unique piece ID.
Definition piece.h:72
void increase_kill_count() noexcept
Increase the kill count by one.
Definition piece.h:106
double get_shield() const
Get the shield value of the unit.
Definition piece.cpp:329
void tick(const delta_t &dt, game &g)
Do one frame of movement, resulting in a piece movement of 1 * delta_t.
Definition piece.cpp:1061
const auto & get_in_game_time() const noexcept
Get the in-game time according to this piece.
Definition piece.h:75
void set_current_action_progress(const delta_t &t) noexcept
Set the current progres an action has passed.
Definition piece.cpp:435
double get_health() const noexcept
Get the health of the unit.
Definition piece.h:69
bool has_moved() const noexcept
Has this piece (attempted to) moved?
Definition piece.h:103
bool is_enpassantable(const in_game_time &when) const
Can the unit be captured by en-passant? Will fail if the piece is not a pawn.
Definition piece.cpp:386
void add_action(const piece_action &action)
Add an action for the piece to do This function will split up the action in smaller atomic actions.
Definition piece.cpp:68
auto & get_actions() noexcept
Get all the piece actions.
Definition piece.h:54
const auto & get_action_history() const noexcept
Get the actions history.
Definition piece.h:57
int get_kill_count() const noexcept
Get the number of pieces this piece has killd.
Definition piece.h:78
delta_t get_current_action_progress() const
The progress of the current action, which is a value from zero to (and including) one.
Definition piece.cpp:300
void set_current_square(const square &s) noexcept
Set the current/occupied square.
Definition piece.h:121
const auto & get_actions() const noexcept
Get all the piece actions.
Definition piece.h:51
double get_max_health() const noexcept
Get the maximum health of the unit.
Definition piece.h:81
double get_max_shield() const
Get the maximum shield value of the unit.
Definition piece.cpp:318
const auto & get_race() const noexcept
Get the race of a piece.
Definition piece.h:92
void clear_messages() noexcept
Clear all the sound effects, i.e. remove all.
Definition piece.cpp:272
const auto & get_type() const noexcept
Get the type of piece, e.g. king, queen, rook, bishop, knight, pawn.
Definition piece.h:100
const auto & get_current_square() const noexcept
Definition piece.h:66
void add_message(const message_type &message)
Add a message, i.e. a thing to say for this piece.
Definition piece.cpp:145
const auto & get_color() const noexcept
Get the color of the piece, i.e. white or black.
Definition piece.h:60
void receive_damage(const double damage)
Receive damage.
Definition piece.cpp:423
const auto & get_messages() const noexcept
The things this piece wants to say.
Definition piece.h:89
A copyable read-only value.
Definition read_only.h:14
const T & get_value() const
Definition read_only.h:21
A chess square.
Definition square.h:58
message_type
The type of a message.
Definition message_type.h:10
bool is_idle(const piece &p) noexcept
Is the unit idle?
Definition piece.cpp:402
bool can_attack_on_empty_board(const chess_color color, const piece_type &p, const square &from, const square &to)
Can a piece attack from 'from' to 'to'? This function assumes the board is empty.
Definition piece.cpp:150
void tick_castle_queenside(piece &p, const delta_t &dt, game &g)
Process a tick, when the current action is a castling to queenside.
Definition piece.cpp:1274
bool can_move_on_empty_board(const chess_color color, const piece_type &p, const square &from, const square &to)
Can a piece move from 'from' to 'to'? This function assumes the board is empty.
Definition piece.cpp:195
void tick_move(piece &p, const delta_t &dt, game &g)
Process a tick, when the current action is a move.
Definition piece.cpp:1355
piece get_test_white_king() noexcept
Create a piece to be used in testing: a white king on e1.
Definition piece.cpp:335
bool is_dead(const piece &p) noexcept
Is the unit dead?
Definition piece.cpp:381
square get_occupied_square(const piece &p) noexcept
Get the square that this piece occupies now.
Definition piece.cpp:324
void clear_actions(piece &p)
Clear all the actions.
Definition piece.cpp:266
bool is_on_starting_rank(const chess_color c, const square &q) noexcept
Is this piece (a pawn) on the starting rank?
Definition piece.cpp:407
bool is_selected(const piece &p)
Determine from the action history if the piece is selected.
Definition piece.cpp:418
bool can_promote(const chess_color color, const piece_type &p, const square &s) noexcept
Can this piece promote?
Definition piece.cpp:245
int count_piece_actions(const piece &p)
Count the number of actions a piece has.
Definition piece.cpp:278
void tick_attack_en_passant(piece &p, const delta_t &dt, game &g)
Definition piece.cpp:1159
void tick_attack(piece &p, const delta_t &dt, game &g)
Process a tick, when the current action is an attack.
Definition piece.cpp:1127
bool has_moved(const piece &p) noexcept
Has this piece (attempted to) move?
Definition piece.cpp:376
bool has_actions(const piece &p) noexcept
Does the piece have actions to do?
Definition piece.cpp:363
bool is_pawn(const piece &p) noexcept
Is the piece a pawn?
Definition piece.cpp:413
std::string describe_actions(const piece &p)
Describe the actions a piece have, e.g. 'idle', or 'moving to (3, 4)'.
Definition piece.cpp:283
double get_f_shield(const piece &p) noexcept
Get the fraction of the shield, where 1.0 denotes full shields.
Definition piece.cpp:312
bool is_enpassantable(const piece &p, const in_game_time &when)
Can the unit be captured by en-passant?
Definition piece.cpp:394
bool has_just_double_moved(const piece &p, const in_game_time &when) noexcept
Has this piece (i.e.
Definition piece.cpp:368
void tick_castle_kingside(piece &p, const delta_t &dt, game &g)
Process a tick, when the current action is a castling to kingside.
Definition piece.cpp:1192
bool can_capture(const piece_type &p, const square &from, const square &to, const chess_color color)
Can a piece capture from 'from' to 'to'? This function assumes the board is empty.
void test_piece()
Test this class and its free functions.
Definition piece.cpp:440
piece get_test_white_knight(const race r=race::classic) noexcept
Create a piece to be used in testing: a white knight on c3.
Definition piece.cpp:344
double get_f_health(const piece &p) noexcept
Get the fraction of the health, where 1.0 denotes full health.
Definition piece.cpp:307
char to_fen_char(const piece &p) noexcept
Convert the piece to a FEN character.
Definition piece.cpp:1435
piece_type
The type of chess piece.
Definition piece_type.h:9
race
The chess race.
Definition race.h:10
@ classic