-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChessPiece.cpp
125 lines (103 loc) · 3.25 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
//
// Created by Lukas Bos on 30/11/2017.
//
#include <iostream>
#include "ChessPiece.h"
using namespace sf;
using namespace std;
ChessPiece::ChessPiece() {
type = PAWN;
};
ChessPiece::ChessPiece(Board *board, Square *location, PieceColor color)
: board(board), location(location), color(color) {
type = PAWN;
}
vector<Move *> ChessPiece::getAvailableMoves(bool considerCheck) {
vector<Move *> moves;
for (Square *square : getAvailableSquares(considerCheck)) {
moves.push_back(new Move(board, location, square));
}
return moves;
}
/*
* This function takes an array of directional vectors (sf::vector21)
* For example: (1,1) means up-right and (-1,0) means left.
* It returns the available legal moves according to the board.
* Considercheck is usually always true, but it is needed for the program to detect check(mate)
*/
vector<Square *>
ChessPiece::calculateMovesForDirections(Square *location, Vector2i directions[], Board *board,
PieceColor color, short amountOfDirections, short maxAmountOfSteps,
bool considerCheck) {
vector<Square *> moves;
moves.reserve(21); // a piece cannot have more than 21 moves
int y = location->getCoordinates().y;
int x = location->getCoordinates().x;
for (int i = 0; i < amountOfDirections; i++) {
for (int j = 1; j <= maxAmountOfSteps; j++) {
int xLocation = x + (j*directions[i].x);
int yLocation = y + (j*directions[i].y);
bool elementExists = xLocation >= 0 && xLocation < 8 && yLocation >= 0 && yLocation < 8;
if (elementExists) {
Square *element = board->getSquare(xLocation, yLocation);
if (element->getChessPiece() && element->getChessPiece()->color==color) {
break;
} else if (element->getChessPiece()) {
moves.push_back(element);
break;
}
moves.push_back(element);
}
}
}
if (considerCheck) {
return removeMovesLeadingToSelfCheck(moves);
}
return moves;
}
// this kindof becomes "removeillegalmoves" now.
vector<Square *>
ChessPiece::removeMovesLeadingToSelfCheck(vector<Square *> destinations) {
vector<Square *> safeDestinations;
safeDestinations.reserve(50);
for (Square *destination : destinations) {
Move *moveToTry = new Move(board, location, destination, true);
board->doMove(moveToTry);
if (!board->isInCheck(color) && !board->tooMuchRepetition()) {
safeDestinations.push_back(destination);
}
board->undoMove();
delete moveToTry;
}
return safeDestinations;
}
PieceType ChessPiece::getType() const {
return type;
}
Square *ChessPiece::getLocation() const {
return location;
}
void ChessPiece::setLocation(Square *location) {
ChessPiece::location = location;
}
PieceColor ChessPiece::getColor() const {
return color;
}
int ChessPiece::getAmountOfSteps() const {
return amountOfSteps;
}
void ChessPiece::increaseAmountOfSteps(int amount) {
ChessPiece::amountOfSteps += amount;
}
void ChessPiece::decreaseAmountOfSteps(int amount) {
ChessPiece::amountOfSteps -= amount;
}
const string &ChessPiece::getName() const {
return name;
}
char ChessPiece::getLetter() const {
return letter;
}
void ChessPiece::setLetter(char letter) {
ChessPiece::letter = letter;
}