-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessPiece.cpp
291 lines (227 loc) · 8.49 KB
/
ChessPiece.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include"ChessPiece.h"
Shader* ChessPiece::shader;
Texture* ChessPiece::white;
Texture* ChessPiece::black;
ChessPiece::ChessPiece(const ObjBinFile& file, const GLboolean color, const GLfloat boardWidth, const GLfloat boardHeight)
: Piece(), color(color), boardWidth(boardWidth), boardHeight(boardHeight), chessPosition(-100, -100), virtualDeleted(GL_FALSE)
{
this->addVertices(file.getVertexes());
this->addNormals(file.getNormals());
this->addUVs(file.getUVs());
this->setShader(ChessPiece::shader);
if (color) {
this->setTexture(ChessPiece::white);
}
else {
this->setTexture(ChessPiece::black);
}
}
void ChessPiece::getLastChessPosition(Move& move) const {
move = this->chessPosition;
}
void ChessPiece::revertPosition() {
this->setChessPosition(this->grabbedPosition);
}
void ChessPiece::getGrabbed(const std::vector<ChessPiece*> otherPieces) {
this->grabbedPosition = this->chessPosition;
this->validMoves = std::vector<Move>();
//we launch a thread here to calculate where we are allowed to go
this->moveProccessingThread = std::thread(&ChessPiece::processGetGrabbed, this, otherPieces);
}
void ChessPiece::processGetGrabbed(const std::vector<ChessPiece*> otherPieces) {
this->validMoves = this->getValidMoves(otherPieces, false);
}
std::vector<Move> ChessPiece::getValidMovesFromCache() {
return this->validMoves;
}
GLboolean ChessPiece::getColor() const {
return this->color;
}
std::vector<Move> ChessPiece::getThreats(const std::vector<ChessPiece*>& otherPieces) {
if (!this->virtualDeleted) {
return this->getValidMoves(otherPieces, true);
} else {
return std::vector<Move>();
}
}
void ChessPiece::onBeforeValidMove() {
this->moveProccessingThread.join();
}
void ChessPiece::onMoveCallback() {};
void ChessPiece::filterByCheck(const std::vector<ChessPiece*>& otherPieces, std::vector<Move>& moves) {
//foreach move, we will set this pieces 'chess position' to the moves position, we will calculate the threat board
//first find the king
ChessPiece* king = NULL;
for (size_t pos = 0; pos < otherPieces.size(); pos++) {
if (otherPieces[pos]->getClass() == KING && otherPieces[pos]->getColor() == this->getColor()) {
king = otherPieces[pos];
break;
}
}
//if there is no king check is not possible
if (king == NULL) {
return;
}
//then for each move, try it and see if it puts us in check
for (size_t move = 0; move < moves.size(); move++) {
std::vector<ChessPiece*> toRevert = this->virtualMove(otherPieces, moves[move]);
ChessBoard board = getThreatBoard(!this->getColor(), otherPieces);
//we calculate the kings position here, because the piece were moving might be the king itself
Move kingPos;
king->getLastChessPosition(kingPos);
//check if that puts the king in check
if (board.at(kingPos) == THREAT) {
moves.erase(moves.begin() + move);
move -= 1;
}
//revert all the pieces we moved
for (size_t i = 0; i < toRevert.size(); i++) {
toRevert[i]->restore();
}
}
}
std::vector<ChessPiece*> ChessPiece::virtualMove(const std::vector<ChessPiece*>& otherPieces, const Move& move) {
//the default behavior for this method is to cache the pieces position,
//move the piece to the requested location, tag the piece moved onto as deleted,
//and return an array of the deleted piece and itself
//pawn and king both have complex logic to consider the effects on the board of castling
//and en passent capture
this->cachePositionAndSet(move, GL_FALSE);
std::vector<ChessPiece*> toRestore;
toRestore.push_back(this);
for (size_t i = 0; i < otherPieces.size(); i++) {
if (otherPieces[i]->chessPosition == this->chessPosition &&
otherPieces[i] != this) {
otherPieces[i]->cachePositionAndSet(Move(), GL_TRUE);
toRestore.push_back(otherPieces[i]);
}
}
return toRestore;
}
void ChessPiece::restore() {
//restore the position
this->chessPosition = this->virtualCache;
//set the piece back on the board
this->virtualDeleted = GL_FALSE;
}
void cachePositionAndSet(const Move& move, GLboolean deleted) {
this->virtualCache = this->chessPosition;
this->chessPosition = move;
this->virtualDeleted = deleted;
}
void ChessPiece::setChessPosition(const Move& move) {
//the space between each row on the chess board
GLfloat gridSpacing = this->boardWidth / 8;
//the offset between to center of the board and the center of the next row
GLfloat lineOffset = gridSpacing / 2;
GLfloat xOffset = (move.x - 4) * gridSpacing + lineOffset;
GLfloat yOffset = this->boardHeight + this->getBottomHeight();
GLfloat zOffset = (move.z - 4) * gridSpacing + lineOffset;
this->position(glm::vec3(xOffset, yOffset, zOffset));
//and save our position
this->chessPosition = move;
}
void ChessPiece::getChessPosition(Move& move) const {
GLfloat gridSpacing = this->boardWidth / 8;
GLfloat lineOffset = gridSpacing / 2;
GLfloat xOffset = this->current_position.x;
GLfloat zOffset = this->current_position.z;
//these is just solving the equation for the xOffset in this->setChessPosition for x
GLfloat floatXPos = (xOffset - lineOffset) / gridSpacing;
GLfloat floatZPos = (zOffset - lineOffset) / gridSpacing;
//now we need to round these value to the nearsest integer, they might be negative
if (floatXPos < 0.0f) {
floatXPos -= 0.5f;
} else {
floatXPos += 0.5f;
}
if (floatZPos < 0.0f) {
floatZPos -= 0.5f;
} else {
floatZPos += 0.5f;
}
GLint xPos = (GLint)(floatXPos)+4;
GLint zPos = (GLint)(floatZPos)+4;
//assign the calculated positions
move = Move(xPos, zPos);
}
GLboolean ChessPiece::validMove(const Move& move) const {
//get the legal moves
std::vector<Move> moves = this->validMoves;//this->getValidMoves(otherPieces, false);
//iterate over each and see if our coordinates match
for (size_t pos = 0; pos < moves.size(); pos++) {
if (moves[pos] == move) {
return true;
}
}
return false;
}
std::vector<ChessPiece*> ChessPiece::makeMove(const std::vector<ChessPiece*>& otherPieces, const Move& move) {
std::vector<ChessPiece*> toDelete;
//so we know we will capture a place on the location we are going to, we need to find it
//by position and add it to the array
for (size_t i = 0; i < otherPieces.size(); i++) {
Move piecePos;
otherPieces[i]->getLastChessPosition(piecePos);
if (piecePos == move && otherPieces[i] != this) {
toDelete.push_back(otherPieces[i]);
}
}
//move the piece into position
this->setChessPosition(move);
this->onMoveCallback();
return toDelete;
}
ChessBoard ChessPiece::getBoardState(const std::vector<ChessPiece*>& otherPieces) const {
//create a board
ChessBoard board;
//for each piece, add it to the board state
for (size_t i = 0; i < otherPieces.size(); i++) {
ChessPiece* otherPiece = otherPieces[i];
//grab the otherpiece's location
Move piecePos;
otherPiece->getLastChessPosition(piecePos);
if (otherPiece->getColor()) {
board.at(piecePos) = WHITE_PIECE;
} else {
board.at(piecePos) = BLACK_PIECE;
}
}
return board;
}
std::vector<Move> ChessPiece::checkPath(const ChessBoard& board, const Move& dMove) const {
GLuint otherColor = this->getColor() == WHITE ? BLACK_PIECE : WHITE_PIECE;
GLuint ourColor = this->getColor() == WHITE ? WHITE_PIECE : BLACK_PIECE;
Move toCheck = this->chessPosition + dMove;
std::vector<Move> moves;
//while the piece is on the board, and is either over a piece of the other color or no piece
while (toCheck.onBoard() && (board.at(toCheck) == otherColor || board.at(toCheck) == NONE)) {
//add the moves to the array and move to the next square
moves.push_back(toCheck);
//break if the piece was of the other color, so we don't go through them
if (board.at(toCheck) == otherColor) {
break;
}
//increment the move
toCheck += dMove;
}
return moves;
}
ChessBoard ChessPiece::getThreatBoard(const GLboolean color, const std::vector<ChessPiece*>& otherPieces) {
std::vector<Move> moves;
//get the threatend spaces
for (size_t piece = 0; piece < otherPieces.size(); piece++) {
if (otherPieces[piece]->getColor() == color) {
std::vector<Move> otherMoves = otherPieces[piece]->getThreats(otherPieces);
moves.insert(moves.end(), otherMoves.begin(), otherMoves.end());
}
}
//initializes board
ChessBoard board;
//apply the spaces
for (size_t move = 0; move < moves.size(); move++) {
board.at(moves[move]) = THREAT;
}
//return the board
return board;
}