-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.cpp
More file actions
232 lines (220 loc) · 7.59 KB
/
Piece.cpp
File metadata and controls
232 lines (220 loc) · 7.59 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
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
//
// Piece.cpp
// CPPChess
//
// Created by Tyler Phalen on 4/8/22.
//
#include "chessGame.h"
chessPiece::chessPiece(){}
chessPiece::chessPiece(int Type, int color, coordinates xy){
switch (Type) {
case blank:
Title = "blank";
UCode = " ";
pieceType = blank;
pieceColor = null;
XY = xy;
break;
case king:
switch (color) {
case white:
Title = "White King";
UCode = "\u2654";
pieceType = king;
pieceColor = white;
XY = xy;
break;
case black:
Title = "Black King";
UCode = "\u265A";
pieceType = king;
pieceColor = black;
XY = xy;
break;
default: cout << "Invalid Color" << endl;
break;
}
break;
case queen:
switch (color) {
case white:
Title = "White Queen";
UCode = "\u2655";
pieceType = queen;
pieceColor = white;
XY = xy;
break;
case black:
Title = "Black Queen";
UCode = "\u265B";
pieceType = queen;
pieceColor = black;
XY = xy;
break;
default: cout << "Invalid Color" << endl;
break;
}
break;
case bishop:
switch (color) {
case white:
Title = "White Bishop";
UCode = "\u2657";
pieceType = bishop;
pieceColor = white;
XY = xy;
break;
case black:
Title = "Black Bishop";
UCode = "\u265D";
pieceType = bishop;
pieceColor = black;
XY = xy;
break;
default: cout << "Invalid Color" << endl;
break;
}
break;
case knight:
switch (color) {
case white:
Title = "White Knight";
UCode = "\u2658";
pieceType = knight;
pieceColor = white;
XY = xy;
break;
case black:
Title = "Black Knight";
UCode = "\u265E";
pieceType = knight;
pieceColor = black;
XY = xy;
break;
default: cout << "Invalid Color" << endl;
break;
}
break;
case rook:
switch (color) {
case white:
Title = "White Rook";
UCode = "\u2656";
pieceType = rook;
pieceColor = white;
XY = xy;
break;
case black:
Title = "Black Rook";
UCode = "\u265C";
pieceType = rook;
pieceColor = black;
XY = xy;
break;
default: cout << "Invalid Color" << endl;
break;
}
break;
case pawn:
switch (color) {
case white:
Title = "White Pawn";
UCode = "\u2659";
pieceType = pawn;
pieceColor = white;
XY = xy;
break;
case black:
Title = "Black Pawn";
UCode = "\u265F";
pieceType = pawn;
pieceColor = black;
XY = xy;
break;
default: cout << "Invalid Color" << endl;
break;
}
break;
default: cout << "Invalid Piece Type." << endl;
break;
}
}
bool chessPiece::move(chessPiece* piece, chessPiece* destination, int moveCount){
int x;
int y;
bool wasValid = false;
switch (piece->pieceType) {
case king:
if (abs(piece->XY.x - destination->XY.x) <= 1 && abs(piece->XY.y - destination->XY.y) <= 1){
piece->XY = destination->XY;
wasValid = true;
} else {
cout << "Invalid Move" << endl;
}
break;
case queen:
x = abs(piece->XY.x - destination->XY.x);
y = abs(piece->XY.y - destination->XY.y);
if ((x == 0 && y <= 8) || (x <= 8 && y == 0) || (x == y)){
piece->XY = destination->XY;
wasValid = true;
} else {
cout << "Invalid Move" << endl;
}
break;
case bishop:
x = abs(piece->XY.x - destination->XY.x);
y = abs(piece->XY.y - destination->XY.y);
if ((x == y) && (x + y < 16)){
piece->XY = destination->XY;
wasValid = true;
} else {
cout << "Invalid Move" << endl;
}
break;
case knight:
x = abs(piece->XY.x - destination->XY.x);
y = abs(piece->XY.y - destination->XY.y);
if ((x == 2 && y == 1) || (x == 1 && y == 2)){
piece->XY = destination->XY;
wasValid = true;
} else {
cout << "Invalid Move" << endl;
}
break;
case rook:
x = abs(piece->XY.x - destination->XY.x);
y = abs(piece->XY.y - destination->XY.y);
if ((x > 1 && y == 0) || (x == 0 && y > 1)){
piece->XY = destination->XY;
wasValid = true;
} else {
cout << "Invalid Move" << endl;
}
break;
case pawn:
if (moveCount >= 1 && abs(piece->XY.x - destination->XY.x) <= 0 && abs(piece->XY.y - destination->XY.y) == 1){
piece->XY = destination->XY;
wasValid = true;
} else if (moveCount < 1 && abs(piece->XY.x - destination->XY.x) <= 0 && abs(piece->XY.y - destination->XY.y) == 2){
piece->XY = destination->XY;
wasValid = true;
} else if (((destination->pieceType != blank) && (destination->pieceColor != piece->pieceColor)) && abs(piece->XY.x - destination->XY.x) == 1 && abs(piece->XY.y - destination->XY.y) == 1){
piece->XY = destination->XY;
wasValid = true;
} else if ((moveCount < 2) && (piece->pieceColor == black) && abs(piece->XY.x - destination->XY.x) <= 0 && abs(piece->XY.y - destination->XY.y) == 2){
piece->XY = destination->XY;
wasValid = true;
} else {
cout << "Invalid Move" << endl;
}
break;
default: cout << "Invalid Piece Type" << endl;
break;
}
return wasValid;
}
void chessPiece::displayPiece(){
//a function for displaying an individual piece to the console.
cout << UCode << " " << Title << endl;
}