-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessman.cpp
More file actions
64 lines (51 loc) · 1.41 KB
/
Chessman.cpp
File metadata and controls
64 lines (51 loc) · 1.41 KB
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
#include "Chessman.h"
#include <ctype.h>
#include <vector>
#include "Position.h"
#include "Chessboard.h"
#include <string>
using namespace std;
Chessman::Chessman(char symbol, bool white) : symbol(symbol), white(white)
{
valid_moves = new vector<Position>;
capture_moves= new vector<Position>;
}
Chessman::~Chessman()
{
valid_moves->clear();
capture_moves->clear();
delete valid_moves;
delete capture_moves;
}
char Chessman::get_symbol() const
{
return is_white() ? toupper(symbol) : tolower(symbol);
}
bool Chessman::can_move(int from_row, int from_col, int to_row, int to_col, const Chessboard& cb) const
{
auto moves = get_moves();
auto it_moves = std::find(
moves.begin(),
moves.end(),
Position(to_row, to_col));
if (it_moves != moves.end())
return true;
return false;
}
std::vector<Position> Chessman::get_moves() const
{
vector<Position> joined_vector;
if (valid_moves == nullptr || capture_moves == nullptr)
return joined_vector;
joined_vector.reserve(valid_moves->size() + capture_moves->size());
joined_vector.insert(joined_vector.end(), valid_moves->begin(), valid_moves->end());
joined_vector.insert(joined_vector.end(), capture_moves->begin(), capture_moves->end());
return joined_vector;
}
bool Chessman::can_move() const
{
return get_moves().size() > 0;
if (valid_moves == nullptr || capture_moves == nullptr)
return false;
return valid_moves->size() > 0 || capture_moves->size() > 0;
}