-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessPiece.cpp
161 lines (128 loc) · 3.91 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
#include "ChessPiece.h"
sf::Sprite& ChessPiece::GetSprite() {
return pieceType;
}
bool ChessPiece::GetColor() {
return color;
}
void ChessPiece::Draw(sf::RenderWindow& window) {
window.draw(pieceType);
}
bool ChessPiece::IsDragging() {
return dragging;
}
void ChessPiece::SetDragging(bool drag) {
dragging = drag;
}
void ChessPiece::IncrementMoveCount() {
moveCount++;
}
vector<sf::Vector2f>& ChessPiece::GetLegalMoves() {
return legalMoves;
}
vector<sf::Vector2f>& ChessPiece::GetLegalMovesWithFF() {
return legalMovesIncludingFriendlyFire;
}
vector<sf::Vector2f>& ChessPiece::GetTotalMoves() {
return totalMoves;
}
void ChessPiece::SetLegalMoves(const vector<sf::Vector2f>& moves) {
legalMoves = moves;
}
void ChessPiece::AddMove(vector<vector<ChessPiece*>>& gridPieces, int activeRowIndex, int rowOffset, int activeColIndex, int colOffset, int activePieceOriginalPositionX, int activePieceOriginalPositionY) {
legalMoves.push_back(sf::Vector2f(activePieceOriginalPositionX + (96 * colOffset), activePieceOriginalPositionY + (96 * rowOffset)));
legalMovesIncludingFriendlyFire.push_back(legalMoves[legalMoves.size() - 1]); //This list is more or less a copy of the other one
try {
ChessPiece* target = gridPieces.at(activeRowIndex + rowOffset).at(activeColIndex + colOffset);
if (target != nullptr) { //Can't attack friendlies
if (target->GetColor() == this->GetColor()) {
legalMoves.pop_back();
}
lineOfSight = false;
return;
}
}
catch (const out_of_range& e) {
legalMoves.pop_back(); //The space wasn't on the board
legalMovesIncludingFriendlyFire.pop_back();
lineOfSight = false;
return;
}
}
void ChessPiece::AddTotalMove(vector<vector<ChessPiece*>>& gridPieces, int activeRowIndex, int rowOffset, int activeColIndex, int colOffset, int activePieceOriginalPositionX, int activePieceOriginalPositionY) {
totalMoves.push_back(sf::Vector2f(activePieceOriginalPositionX + (96 * colOffset), activePieceOriginalPositionY + (96 * rowOffset)));
try {
ChessPiece* target = gridPieces.at(activeRowIndex + rowOffset).at(activeColIndex + colOffset);
}
catch (const out_of_range& e) {
totalMoves.pop_back(); //The space wasn't on the board
lineOfSight = false; //Only stop adding moves if they're not on the board
}
}
int ChessPiece::GetMoveCount() {
return moveCount;
}
string ChessPiece::GetName() {
return name;
}
bool ChessPiece::CanCastle() {
return canCastle;
}
bool ChessPiece::CanCastleRight() {
return canCastleRight;
}
bool ChessPiece::CanCastleLeft() {
return canCastleLeft;
}
bool ChessPiece::CanEnPassant() {
return canEnPassant;
}
void ChessPiece::SetCheckState(bool check) {
inCheck = check;
}
void ChessPiece::SetAttackingKing(bool attack) {
attackingKing = attack;
}
bool ChessPiece::IsAttackingKing() {
return attackingKing;
}
bool ChessPiece::IsInCheck() {
return inCheck;
}
void ChessPiece::Move(sf::Vector2i& mousePos) {
cout << "Moving Piece" << endl;
pieceType.setPosition(mousePos.x - pieceType.getGlobalBounds().width / 2, mousePos.y - pieceType.getGlobalBounds().height / 2);
}
void ChessPiece::SetProtecting(bool protect) {
protectingKing = protect;
}
bool ChessPiece::IsProtecting() {
return protectingKing;
}
bool ChessPiece::IsAlreadyModified() {
return alreadyModified;
}
void ChessPiece::SetAlreadyModified(bool mod) {
alreadyModified = mod;
}
void ChessPiece::SetMoveCount(int moveCount) {
this->moveCount = moveCount;
}
//DEBUG
void ChessPiece::PrintName() {
cout << name << " ";
}
void ChessPiece::PrintMoveCount() {
cout << "This piece has made " << moveCount << " moves" << endl;
}
void ChessPiece::PrintCheckState() {
if (color && name == "King") {
cout << "The White king is in check: " << inCheck << endl;
}
if (!color && name == "King") {
cout << "The Black king is in check: " << inCheck << endl;
}
}
void ChessPiece::DisplayPosition() {
cout << "Sprite at (" << pieceType.getPosition().x << ", " << pieceType.getPosition().y << ") clicked!" << endl;
}