-
Notifications
You must be signed in to change notification settings - Fork 1
/
chess_piece.cpp
98 lines (87 loc) · 3.04 KB
/
chess_piece.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "chess_piece.hpp"
namespace chess
{
color chess_piece::get_color() const
{
return m_color;
}
color chess_piece::get_opposite_color() const
{
return m_color == 'b' ? 'w' : 'b';
}
const position_type& chess_piece::get_position() const
{
return m_position;
}
void chess_piece::move(const position_type& new_pos)
{
m_position = new_pos;
}
// Default implementation does nothing
void chess_piece::notify_move()
{
}
chess_piece::chess_piece(color c, const position_type& pos)
: m_color(c), m_position(pos)
{
}
bool chess_piece::check_col_move(const position_type& new_pos,
const has_piece_callback& cb) const
{
const position_type& current_pos = get_position();
bool valid = new_pos.first == current_pos.first && new_pos.second != current_pos.second;
if (valid)
{
size_t min_pos = std::min(current_pos.second, new_pos.second);
size_t max_pos = std::max(current_pos.second, new_pos.second);
for (size_t i = min_pos + 1; valid && (i < max_pos - 1); ++i)
{
valid = !cb(position_type(new_pos.first, i), 'a');
}
}
return valid;
}
bool chess_piece::check_row_move(const position_type& new_pos,
const has_piece_callback& cb) const
{
const position_type& current_pos = get_position();
bool valid = new_pos.second == current_pos.second && new_pos.first != current_pos.first;
if (valid)
{
char min_pos = std::min(current_pos.first, new_pos.first);
char max_pos = std::max(current_pos.first, new_pos.first);
for (char i = static_cast<char>(min_pos + 1); valid && (i < max_pos - 1); ++i)
{
valid = !cb(position_type(i, new_pos.second), 'a');
}
}
return valid;
}
bool chess_piece::check_diag_move(const position_type& new_pos,
const has_piece_callback& cb) const
{
const position_type& current_pos = get_position();
char col_min = std::min(current_pos.first, new_pos.first);
char col_max = std::max(current_pos.first, new_pos.first);
size_t row_min = std::min(current_pos.second, new_pos.second);
size_t row_max = std::max(current_pos.second, new_pos.second);
bool valid = (size_t(col_max - col_min) == row_max - row_min)
&& (new_pos.first != current_pos.first);
if (valid)
{
size_t dist = row_max - row_min;
for (size_t i = 1; valid && (i < dist - 1); ++i)
{
position_type pos(col_min + static_cast<char>(i),
row_min + i);
valid = !cb(pos, 'a');
}
}
return valid;
}
std::ostream& operator<<(std::ostream& out, const chess_piece& pce)
{
pce.print(out);
return out;
}
}