-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
344 lines (318 loc) · 10.6 KB
/
Copy pathBoard.cpp
File metadata and controls
344 lines (318 loc) · 10.6 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include "Board.hpp"
#include "chesspieces/Chesspiece.hpp"
#include "chesspieces/Chesspiece.cpp"
#include "chesspieces/Pawn/Pawn.cpp"
#include "chesspieces/Pawn/Pawn.hpp"
#include "chesspieces/Rook/Rook.hpp"
#include "chesspieces/Rook/Rook.cpp"
#include "chesspieces/Bishop/Bishop.cpp"
#include "chesspieces/Bishop/Bishop.hpp"
#include "chesspieces/King/King.hpp"
#include "chesspieces/King/King.cpp"
#include "chesspieces/Knight/Knight.hpp"
#include "chesspieces/Knight/Knight.cpp"
#include "chesspieces/Queen/Queen.hpp"
#include "chesspieces/Queen/Queen.cpp"
#include <iostream>
using namespace std;
Board:: Board(){
whiteTurn = true;
moves = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
playArea[i][j] = nullptr; // empty address
}
}
// initialize all elements of the playArea array to nullptr
// for (int i = 0; i < 8; i++) {
// for (int j = 0; j < 8; j++) {
// playArea[i][j] = nullptr;
// }
// }
// cout << "array of null ptrs made" << endl;
//Its likely all the following code will be changed except for the initializing team names-> The rest will probably have to use the consttructors
//From each of the specific piece classes-> Likely a lot of changes in the futur->
playArea[0][4] = new King('W');
playArea[7][4] = new King('B');
for (int i = 0; i < 8; i++)
{
playArea[1][i] = new Pawn('W');
// playArea[1][i] = new Rook('W');
// cout << "set first team" << endl;
}
// cout << "functions called";
for (int i = 0; i < 8; i++)
{
// playArea[7][i] = new Pawn('B');
playArea[6][i] = new Pawn('B');
}
// cout << "created black pawns";
//Like this will probably for sure be gone (but keeping the names all 5 letters i think is a good touch);
//Rook
playArea[0][0] = new Rook('W');
playArea[0][7] = new Rook('W');
playArea[7][0] = new Rook('B');
playArea[7][7] = new Rook('B');
// Bishop
playArea[0][2] = new Bishop('W');
playArea[0][5] = new Bishop('W');
playArea[7][2] = new Bishop('B');
playArea[7][5] = new Bishop('B');
// Knight
playArea[0][1] = new Knight('W');
playArea[0][6] = new Knight('W');
playArea[7][1] = new Knight('B');
playArea[7][6] = new Knight('B');
playArea[0][3] = new Queen('W');
playArea[7][3] = new Queen('B');
// playArea[7][0]-> setName("Rookk");
// playArea[0][1]-> setName("Knite");
// playArea[7][1]-> setName("Knite");
// playArea[0][2]-> setName("Bihop");
// playArea[7][2]-> setName("Bihop");
// playArea[0][3]-> setName("Queen");
// playArea[7][3]-> setName("Queen");
// playArea[0][4]-> setName("Kingg");
// playArea[7][4]-> setName("Kingg");
// playArea[0][5]-> setName("Bihop");
// playArea[7][5]-> setName("Bihop");
// playArea[0][6]-> setName("Knite");
// playArea[7][6]-> setName("Knite");
// playArea[0][7]-> setName("Rookk");
// playArea[7][7]-> setName("Rookk");
whiteAlive = true;
blackAlive = true;
}
int Board:: turn(){
if(whiteTurn){
cout <<"It is White's turn" << endl;
}
else{
cout << "It is Black's turn" << endl;
}
int ogRow = 0, ogCol = 0;
int tRow = 0, tCol = 0;
bool notPicked = true;
while(notPicked){
ogRow = 0, ogCol = 0;
cout<< "Which piece would you like to move? Enter the x axis first(1-8)" << endl;
while( (ogCol<1 || ogCol>8) && ogCol != 69){
cin >> ogCol;
}
ogCol-=1;
cout << "Enter the y axis now" << endl;
while( ogRow<1 || ogRow>8 && ogRow != 69){
cin >> ogRow;
}
ogRow-=1;
if (ogRow == 68 || ogCol == 68) // will delete Board if written 69
{
return 69;
}
else if(playArea[ogRow][ogCol] != nullptr){
notPicked = false;
}
else
{
cout << "Not a piece, pick again" << endl;
}
}
cout << "You have selected " << playArea[ogRow][ogCol]-> getName() << " on team " << playArea[ogRow][ogCol]-> getTeam() << endl;
cout<< "Where would you like to move? Enter the x axis first (1-8)" << endl;
while( tCol<1 || tCol>8){
cin >> tCol;
}
tCol -=1;
cout << "Enter the y axis now" << endl;
while( tRow<1 || tRow>8){
cin >> tRow;
}
tRow-=1;
cout<< "You have chosen " << tCol+1 << "," << tRow+1 << endl;
//Here would be code that moves stuff around, checks if king is moving into wrong spot, handles captures, etc->
//I imagine this function will be changed a LOT from what it is now->
//Idea checklist:
//Check if occupied by opponent or team piece(return false, might have to turn this into a bool function or something, or make one)
//Check if valid move based on piece
//USE THIS FOR QUICK DEBUGGING:
// swapPiece( tRow, tCol, ogRow, ogCol);
// swapTurn();
// MAIN MOVECHECKING CODE
if(playArea[ogRow][ogCol]-> MoveCheck(ogRow, ogCol, tRow, tCol, playArea)){
//Destruction code
delete playArea[tRow][tCol];
playArea[tRow][tCol] = nullptr;
swapPiece( tRow, tCol, ogRow, ogCol);
cout << "ran swap" << endl;
moves++;
if(playArea[tRow][tCol]->getName() == "Pawnn"){
checkPawnPromotion();
}
swapTurn();
}
else{
cout << "Not valid move, please try again" << endl;
}
// If empty, swap pieces
// If occupied by enemy, make enemy default constructor again (or destroy it somehow), and then swap
return 0;
}
void Board:: swapTurn(){
//Might not change much either
if(whiteTurn){
whiteTurn = false;
}
else{
whiteTurn = true;
}
}
void Board:: display(){
//This display code I dont imagine changing much
for (int row = 7; row >=0; row--)
{
cout << row + 1 <<" ";
for (int col = 0; col <8; col++)
{
if(playArea[row][col] == nullptr){
cout << "noname" << "\t\t";
}
else{
cout << playArea[row][col]-> getTeam() << playArea[row][col]-> getName() << "\t\t";
}
}
cout << endl << endl << endl << endl;
}
cout << " ";
for (int i = 0; i < 8; i++)
{
cout << " " << i+1<< " " << "\t\t";
}
cout << endl;
}
void Board :: swapPiece(int tRow,int tCol,int ogRow,int ogCol)
{
Chesspiece* piece1 = playArea[ogRow][ogCol];
Chesspiece* piece2 = playArea[tRow][tCol];
swap(piece1, piece2);
playArea[tRow][tCol] = piece2;
playArea[ogRow][ogCol] = piece1;
}
void Board:: checkPawnPromotion(){
char user;
for (int i = 0; i < 8; i++)
{ if(playArea[7][i] != nullptr){
if(playArea[7][i]->getTeam() == 'W' && playArea[7][i]->getName() == "Pawnn"){
cout << "Your pawn can promote!" << endl;
cout << "What would you like to promote to: " << endl;
cout << "Q - Queen, B - Bishop, K- Knight, R- Rook" << endl;
cin >> user;
user = toupper(user);
while(user != 'Q' && user!= 'B' && user != 'K' && user!= 'R'){
cout << "Not valid, please try again" << endl;
cin >> user;
}
delete playArea[7][i];
<<<<<<< HEAD
if(user == 'Q'){
playArea[7][i] = new Queen('W');
=======
//TO DO: Implement these as the classes get finished.
if(user == 'Q'){
playArea[7][i] = nullptr;
}
if(user == 'B'){
playArea[7][i] = new Bishop('W');
}
if(user == 'K'){
playArea[7][i] = nullptr;
}
if(user == 'R'){
playArea[7][i] = new Rook('W');
>>>>>>> parent of 94b00a4 (Merge pull request #15 from JustNekoChris/feature/SFML)
}
}
}
}
//This could be optimized very easily by passing in the team into a function after checking if it was white or black.
for (int i = 0; i < 8; i++)
{
if(playArea[0][i] != nullptr){
if(playArea[0][i]->getTeam() == 'B' && playArea[0][i]->getName() == "Pawnn"){
<<<<<<< HEAD
delete playArea[0][i];
if(user == 'Q'){
playArea[0][i] = new Queen('B');
=======
cout << "Your pawn can promote!" << endl;
cout << "What would you like to promote to: " << endl;
cout << "Q - Queen, B - Bishop, K- Knight, R- Rook" << endl;
cin >> user;
user = toupper(user);
while(user != 'Q' && user!= 'B' && user != 'K' && user!= 'R'){
cout << "Not valid, please try again" << endl;
cin >> user;
}
delete playArea[0][i];
//TO DO: Implement these as the classes get finished.
if(user == 'Q'){
playArea[0][i] = nullptr;
}
if(user == 'B'){
playArea[0][i] = new Bishop('B');
}
if(user == 'K'){
playArea[0][i] = nullptr;
}
if(user == 'R'){
playArea[0][i] = new Rook('B');
>>>>>>> parent of 94b00a4 (Merge pull request #15 from JustNekoChris/feature/SFML)
}
}
}
}
}
bool Board:: gameOver(){
bool tempWAlive = false, tempBAlive = false;
for (int row = 0; row < 8; row++)
{
for (int col = 0; col < 8; col++)
{
if(playArea[row][col] != nullptr){
if(playArea[row][col]->getName() == "Kingg"){
if(playArea[row][col]->getTeam() == 'W'){
tempWAlive = true;
}
else if(playArea[row][col]->getTeam() == 'B'){
tempBAlive = true;
}
}
}
}
}
whiteAlive = tempWAlive;
blackAlive = tempBAlive;
if(whiteAlive == false || blackAlive == false){
return true;
}
return false;
}
void Board::deleter()
{
for(int x = 0; x < 8; x++)
{
for(int y = 0; y < 8; y++)
{
if (playArea[y][x] == nullptr)
{
/* code */
}
else
{
delete playArea[y][x];
}
}
}
cout << "Deleted board :]" << endl;
}