-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.cc
More file actions
126 lines (113 loc) · 4.2 KB
/
Player.cc
File metadata and controls
126 lines (113 loc) · 4.2 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "Player.h"
#include <iostream>
Player::Player(bool color, bool inCheck, bool hasCastled, bool isComputer, ChessBoard *board) :
color{color}, inCheck{inCheck}, hasCastled{hasCastled}, isComputer{isComputer}, board{board} {}
Player::~Player() {}
void Player::setInCheck(bool inCheck) {
this->inCheck = inCheck;
}
void Player::setHasCastled(bool hasCastled) {
this->hasCastled = hasCastled;
}
ChessBoard *Player::getBoard() {
return board;
}
bool Player::getColor() {
return color;
}
bool Player::getInCheck() {
return inCheck;
}
bool Player::getHasCastled() {
return hasCastled;
}
Move Player::castleMoveCreator(Move move) {
// 0 is no castle, 1 is KS, 2 is QS
ChessBoard *board = getBoard();
int type = 0;
if (getColor() == 1) { //e1 g1 OR e1 c1
if (move.getStartPos() == make_pair(0, 4)) {
if(move.getEndPos() == make_pair(0, 6)) type = 1; // todo make into enum
else if (move.getEndPos() == make_pair(0, 2)) type = 2;
}
}
else { // black castling; e8 g8 OR e8 c8
if (move.getStartPos() == make_pair(7, 4)) {
if(move.getEndPos() == make_pair(7, 6)) type = 1;
else if (move.getEndPos() == make_pair(7, 2)) type = 2;
}
}
if (type == 1) { // ks castle
Move m{board->getPiece(move.getStartPos()), move.getStartPos(), move.getEndPos(),
board->getPiece(move.getEndPos()) != nullptr
? board->getPiece(move.getEndPos())
: nullptr, true, false, false, ' ', false};
return m;
}
else if (type == 2) { // qs castle
Move m{board->getPiece(move.getStartPos()), move.getStartPos(), move.getEndPos(),
board->getPiece(move.getEndPos()) != nullptr
? board->getPiece(move.getEndPos())
: nullptr, false, true, false, ' ', false};
return m;
}
else { // no castle
return move;
}
}
bool Player::isCheckmate() {
if(getInCheck()){
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
Piece *p = board->getPiece({i,j});
if(board->isOccupied({i, j}) && p->isWhite() == color){
//check all pieces of same color as player for valid moves, if exists, return false. Else, return true.
vector<Move> ml = p->generateMoves(); //these moves do not pass in captured pieces
for(int i = 0; i < (int)ml.size(); ++i){
if(board->isOccupied(ml[i].getEndPos())){
ml[i] = {p, ml[i].getStartPos(), ml[i].getEndPos(), board->getPiece(ml[i].getEndPos())};
}
try{
if(board->checkMoveLegal(ml[i])){ //ERROR HERE
return false;
}
} catch(std::invalid_argument &err){
//do nothing
}
}
}
}
}
return true;
}
//return false if not in check
return false;
}
bool Player::isStalemate() {
if(!getInCheck()){
for(int i = 0; i < 8; i++){
for(int j = 0; j < 8; j++){
Piece *p = board->getPiece({i,j});
if(board->isOccupied({i, j}) && p->isWhite() == color){
//check all pieces of same color as player for valid moves, if exists, return false. Else, return true.
vector<Move> ml = p->generateMoves();
for(Move m : ml){
if(board->isOccupied(m.getEndPos())){
m = {p, m.getStartPos(), m.getEndPos(), board->getPiece(m.getEndPos())};
}
try{
if(board->checkMoveLegal(m)){
return false;
}
} catch(std::invalid_argument &err){
//do nothing
}
}
}
}
}
return true;
}
//return false if in check
return false;
}