-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchesspiece.cc
138 lines (126 loc) · 4.27 KB
/
chesspiece.cc
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
127
128
129
130
131
132
133
134
135
136
137
138
#include "chesspiece.h"
#include "board.h"
ChessPiece::ChessPiece(bool side): moves{0} {
piece.second = side;
};
void ChessPiece::moved(){
moves++;
}
void ChessPiece::undo(){
moves--;
}
int ChessPiece::move(){
return moves;
}
void ChessPiece::checkDiag(Board* b, std::vector<Position>& result, Position pos){
int x = (int) pos.first;
int y = pos.second;
Position retpos;
//diag check top right
for(int row = y+1, col = x+1; row <= 8 && col <= 8; ++row, ++col){
retpos.first = (locationx)(col);
retpos.second = row;
if(b->getType(retpos).first == PieceType::Empty){
//if that cell is empty, we know that is valid
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
//if that cell is occupied by the enemy, we know its valid
//but this is the end for that diagnol line
result.emplace_back(retpos);
break;
}
else {break; } //else its just not a valid cell
}
//diag check top left
for(int row = y+1, col = x-1; row <= 8 && col >= 1; ++row, --col){
retpos.first = (locationx)(col);
retpos.second = row;
if(b->getType(retpos).first == PieceType::Empty){
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
break;
}
else {break;}
}
//diag check bottom right
for(int row = y-1, col = x+1; row >= 1 && col <= 8; --row, ++col){
retpos.first = (locationx)(col);
retpos.second = row;
if(b->getType(retpos).first == PieceType::Empty){
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
break;
}
else {break;}
}
//diag check bottom left
for(int row = y-1, col = x-1; row >= 1 && col >= 1; --row, --col){
retpos.first = (locationx)(col);
retpos.second = row;
if(b->getType(retpos).first == PieceType::Empty){
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
break;
}
else {break;}
}
}
void ChessPiece::checkRowCol(Board* b, std::vector<Position>& result, Position pos){
int x = (int) pos.first;
int y = pos.second;
Position retpos;
//col check to the right
for(int col = x+1; col <= 8; ++col){
retpos.first = (locationx) col;
retpos.second = y;
if(b->getType(retpos).first == PieceType::Empty){
//if that cell is empty, we know that is valid
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
//if that cell is occupied by the enemy, we know its valid
//but this is the end for this col
result.emplace_back(retpos);
break;
}
else {break;} //else its just not a valid cell
}
// //col check to the left
for(int col = x-1; col >= 1; --col){
retpos.first = (locationx)(col);
retpos.second = y;
if(b->getType(retpos).first == PieceType::Empty){
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
break;
}
else {break;}
}
// //row check to the top
for(int row = y+1; row <= 8; ++row){
retpos.first = pos.first;
retpos.second = row;
if(b->getType(retpos).first == PieceType::Empty){
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
break;
}
else {break;}
}
// //row check to the bottom
for(int row = y-1; row >= 1; --row){
retpos.first = pos.first;
retpos.second = row;
if(b->getType(retpos).first == PieceType::Empty){
result.emplace_back(retpos);
}else if(b->getType(retpos).second != b->getType(pos).second){
result.emplace_back(retpos);
break;
}
else {break;}
}
}