-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
622 lines (590 loc) · 19.3 KB
/
Board.cpp
File metadata and controls
622 lines (590 loc) · 19.3 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include <iostream>
#include <fstream>
#include <vector>
#include "Board.h"
#include <unistd.h>
#include <string>
using namespace std;
void startGame();
void resumeGame();
void currentTurn();
void Pawn();
void Bishop();
void Knight();
void Rook();
void Queen();
void King();
//This constructor opens a file and checks if the savefile has anything in it
// if it does than it prompts the user if they want to continue and calls the resume game function, if not then the two players enter their names
// Then it calls the startgame function
Board::Board()
{
tileBoard.resize(8, vector<Pieces>(8));
ifstream myFile;
myFile.open("SaveFile.cpp");
string l;
while(getline(myFile, l))
{
char A;
string B;
string C;
if (l == "")
{
cout << "Player 1, Choose Your Name: ";
getline(cin, B);
player1 = string(B);
cin.get();
cout << "Player 2, Choose Your Name: ";
getline(cin, C);
player2 = string(C);
cin.get();
myFile.close();
startGame();
}
else
{
myFile.close();
cout << "Would you like to continue your last game? Y or N?" << endl;
cin >> A;
cin.get();
if (A == 'Y'|| A == 'y')
{
resumeGame();
}
else if (A == 'N'|| A == 'n')
{
cout << "Player 1, Choose Your Name: ";
getline(cin, B);
player1 = string(B);
cin.get();
cout << "Player 2, Choose Your Name: ";
getline(cin, C);
player2 = string(C);
cin.get();
startGame();
}
else
{
while (A != 'Y'& A != 'N'&& A != 'y' && A != 'n')
{
cout << "That wasn't a valid response. Y or N?" << endl;
cin >> A;
if (A == 'Y'||A == 'y')
{
resumeGame();
}
else if (A == 'N'||A == 'n')
{
cout << "Player 1, Choose Your Name: ";
getline(cin, B);
player1 = string(B);
cin.get();
cout << "Player 2, Choose Your Name: ";
getline(cin, C);
player2 = string(C);
cin.get();
startGame();
}
}
}
}
}
Checkmate = false;
}
Board::~Board()
{
if (Checkmate == false)
{
}
}
//This funtion is for the currentturn function and makes sure the location given is correct by putting it in an array
//Then has to pass the if statement and would give the appropriate array coordinate depending on the input for the chess coordinate
//If it doesn't pass then they are prompted to put in a location again
int Board::PLocation(string a)
{
bool valid = false;
while(valid == false)
{
string arr[2];
for (int i = 0; i < 2; i++)
{
arr[i] = a[i];
}
if (arr[0] >= "A" && arr[0] <= "H" || arr[0] >= "a" && arr[0] <= "h" && arr[1] >= "1" && arr[1] <= "8")
{
valid = true;
if (arr[0] == "A"||arr[0] == "a")
{
return 0;
}
else if (arr[0] == "B"||arr[0] == "b")
{
return 1;
}
else if (arr[0] == "C"||arr[0] == "c")
{
return 2;
}
else if (arr[0] == "D"||arr[0] == "d")
{
return 3;
}
else if (arr[0] == "E"||arr[0] == "e")
{
return 4;
}
else if (arr[0] == "F"||arr[0] == "f")
{
return 5;
}
else if (arr[0] == "G"||arr[0] == "g")
{
return 6;
}
else if (arr[0] == "H"||arr[0] == "h")
{
return 7;
}
}
else
{
cout << "That's not a valid location, check the order of the location. Try again." << endl;
cout << "What piece would you like to move: ";
getline(cin, a);
}
}
}
int Board::PLocation2(string a)
{
bool valid = false;
while(valid == false)
{
string arr[2];
for (int i = 0; i < 2; i++)
{
arr[i] = a[i];
}
if (arr[0] >= "A" && arr[0] <= "H" || arr[0] >= "a" && arr[0] <= "h" && arr[1] >= "1" && arr[1] <= "8")
{
valid = true;
return stoi(arr[1]) - 1;
}
else
{
cout << "That's not a valid location, check the order of the location. Try again." << endl;
cout << "What piece would you like to move: ";
getline(cin, a);
}
}
}
//This asks the players to put in a location to choose a piece, it has to be exactly two, otherwise it would give a prompt to do it again
//Then if it is 2 it would call the plocation function above, then it would call the appropriate chess piece function depending on the piece type at the array location
void Board::currentTurn1()
{
player_one = true;
string l;
cout << "What piece would you like to move, " + string(player1) + ": ";
getline(cin, l);
int a, b;
while (l.length() != 2)
{
l = "";
cout << "That's too long/short, not a valid location. Try again."<< endl;
cout << "What piece would you like to move, " + string(player1) + ": ";
getline(cin, l);
}
if (l.length() == 2)
{
b = PLocation(l);
a = PLocation2(l);
if (tileBoard.at(a).at(b).getType() == "Pawn")
{
ro = a;
co = b;
tileBoard.at(a).at(b) = Pieces();
Pawn();
}
else if (tileBoard.at(a).at(b).getType() == "Bishop")
{
ro = a;
co = b;
tileBoard.at(a).at(b) = Pieces();
Bishop();
}
else if (tileBoard.at(a).at(b).getType() == "Knight")
{
ro = a;
co = b;
tileBoard.at(a).at(b) = Pieces();
Knight();
}
else if (tileBoard.at(a).at(b).getType() == "Rook")
{
ro = a;
co = b;
tileBoard.at(a).at(b) = Pieces();
Rook();
}
else if (tileBoard.at(a).at(b).getType() == "Queen")
{
ro = a;
co = b;
tileBoard.at(a).at(b) = Pieces();
Queen();
}
else if (tileBoard.at(a).at(b).getType() == "King")
{
ro = a;
co = b;
tileBoard.at(a).at(b) = Pieces();
King();
}
else if (tileBoard.at(a).at(b).getType() == "")
{
cout << "There's nothing there." << endl;
currentTurn1();
}
}
l = "";
}
void Board::currentTurn2()
{
string l;
player_one = false;
cout << "What piece would you like to move, " + string(player2) + ": ";
getline(cin, l);
int r, c;
while (l.length() != 2)
{
l = "";
cout << "That's too long/short, not a valid location. Try again."<< endl;
cout << "What piece would you like to move, " + string(player2) + ": ";
getline(cin, l);
}
if (l.length() == 2)
{
c = PLocation(l);
r = PLocation2(l);
if (tileBoard.at(r).at(c).getType() == "Pawn")
{
ro = r;
co = c;
tileBoard.at(r).at(c) = Pieces();
Pawn();
}
else if (tileBoard.at(r).at(c).getType() == "Bishop")
{
ro = r;
co = c;
tileBoard.at(r).at(c) = Pieces();
Bishop();
}
else if (tileBoard.at(r).at(c).getType() == "Knight")
{
ro = r;
co = c;
tileBoard.at(r).at(c) = Pieces();
Knight();
}
else if (tileBoard.at(r).at(c).getType() == "Rook")
{
ro = r;
co = c;
tileBoard.at(r).at(c) = Pieces();
Rook();
}
else if (tileBoard.at(r).at(c).getType() == "Queen")
{
ro = r;
co = c;
tileBoard.at(r).at(c) = Pieces();
Queen();
}
else if (tileBoard.at(r).at(c).getType() == "King")
{
ro = r;
co = c;
tileBoard.at(r).at(c) = Pieces();
King();
}
else if (tileBoard.at(r).at(c).getType() == "")
{
cout << "There's nothing there." << endl;
currentTurn2();
}
}
l = "";
}
//This puts in all the pieces into the tileboard array
void Board::startGame()
{
cout << "Let the game begin!" << endl << endl;
for (int j = 0; j < 8; j++)
{
tileBoard.at(1).at(j) = Pieces ("Pawn", "Black", 1, j);
}
tileBoard.at(0).at(1) = Pieces("Bishop", "Black", 0, 1);
tileBoard.at(0).at(6) = Pieces("Bishop", "Black", 0, 6);
tileBoard.at(0).at(2) = Pieces("Knight", "Black", 0, 2);
tileBoard.at(0).at(5) = Pieces("Knight", "Black", 0, 5);
tileBoard.at(0).at(0) = Pieces("Rook", "Black", 0, 0);
tileBoard.at(0).at(7) = Pieces("Rook", "Black", 0, 7);
tileBoard.at(0).at(3) = Pieces("Queen", "Black", 0, 3);
tileBoard.at(0).at(4) = Pieces("King", "Black", 0, 4);
for (int j = 0; j < 8; j++)
{
tileBoard.at(6).at(j) = Pieces("Pawn", "White", 6, j);
}
tileBoard.at(7).at(1) = Pieces("Bishop", "White", 7, 1);
tileBoard.at(7).at(6) = Pieces("Bishop", "White", 7, 6);
tileBoard.at(7).at(2) = Pieces("Knight", "White", 7, 2);
tileBoard.at(7).at(5) = Pieces("Knight", "White", 7, 5);
tileBoard.at(7).at(0) = Pieces("Rook", "White", 7, 0);
tileBoard.at(7).at(7) = Pieces("Rook", "White", 7, 7);
tileBoard.at(7).at(3) = Pieces("Queen", "White", 7, 3);
tileBoard.at(7).at(4) = Pieces("King", "White", 7, 4);
}
void Board::ParseSave(string line, string Array[])
{
int p = 0;
int c = 0;
line = line + " ";
for (int i = 0; i < line.length(); i++)
{
if (line[i] == ' ')
{
Array[c] = line.substr(p, i-p);
p = i+1;
c++;
}
cout << Array[i] + ", " << endl;
}
}
//This is meant to get data from the save file to resume the game
void Board::resumeGame()
{
ifstream myFile;
myFile.open("SaveFile.cpp");
string l;
while (!myFile.eof())
{
getline(myFile, l);
string arr[4];
ParseSave(l, arr);
tileBoard.at(stoi(arr[2])).at(stoi(arr[3])) = Pieces(arr[0], arr[1], stoi(arr[2]), stoi(arr[3]));
}
cout << "Let the game begin ... Again!" << endl << endl;
}
//This is meant to take the array and put it into the savefile
void Board::saveGame()
{
if (Checkmate == false)
{
ofstream myFile;
myFile.open("SaveFile.cpp", ofstream::out | ofstream::trunc);
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
if (tileBoard.at(i).at(j).getType() == "Pawn"&& tileBoard.at(i).at(j).getColor() == "Black")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Bishop" && tileBoard.at(i).at(j).getColor() == "Black")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Knight" && tileBoard.at(i).at(j).getColor() == "Black")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Rook" && tileBoard.at(i).at(j).getColor() == "Black")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Queen" && tileBoard.at(i).at(j).getColor() == "Black")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "King" && tileBoard.at(i).at(j).getColor() == "Black")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Pawn" && tileBoard.at(i).at(j).getColor() == "White")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Bishop" && tileBoard.at(i).at(j).getColor() == "White")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Knight" && tileBoard.at(i).at(j).getColor() == "White")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Rook" && tileBoard.at(i).at(j).getColor() == "White")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "Queen" && tileBoard.at(i).at(j).getColor() == "White")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
else if (tileBoard.at(i).at(j).getType() == "King" && tileBoard.at(i).at(j).getColor() == "White")
{
myFile << tileBoard.at(i).at(j).getType() + " " + tileBoard.at(i).at(j).getColor() + " " + to_string(i) + " " + to_string(j) << endl;
}
}
}
myFile.close();
}
else
{
ofstream myFile;
myFile.open("SaveFile.cpp", ofstream::out | ofstream::trunc);
myFile.close();
}
}
//These are the functions for the chess pieces
void Board::Pawn()
{
cout << "Where would you like your Pawn to go: ";
string l;
getline(cin, l);
int a, b;
if (l.length() == 2 && player_one == true)
{
a = PLocation2(l);
b = PLocation(l);
if (tileBoard.at(a).at(b).getColor() == "White")
{
cout << "That's one your pieces, try again" << endl;
Pawn();
}
else
{
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Pawn", "White", a, b);
}
}
else if (l.length() == 2 && player_one == false)
{
a = PLocation2(l);
b = PLocation(l);
if (tileBoard.at(a).at(b).getColor() == "Black")
{
cout << "That's one your pieces, try again" << endl;
Pawn();
}
else
{
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Pawn", "Black", a, b);
}
}
}
void Board::Bishop()
{
cout << "Where would you like your Bishop to go: ";
string l;
getline(cin, l);
int a, b;
if (l.length() == 2 && player_one == true)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Bishop", "White", a, b);
}
else if (l.length() == 2 && player_one == false)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Bishop", "Black", a, b);
}
}
void Board::Knight()
{
cout << "Where would you like your Knight to go: ";
string l;
getline(cin, l);
int a, b;
if (l.length() == 2 && player_one == true)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Knight", "White", a, b);
}
else if (l.length() == 2 && player_one == false)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Knight", "Black", a, b);
}
}
void Board::Rook()
{
cout << "Where would you like your Rook to go: ";
string l;
getline(cin, l);
int a, b;
if (l.length() == 2 && player_one == true)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Rook", "White", a, b);
}
else if (l.length() == 2 && player_one == false)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Rook", "Black", a, b);
}
}
void Board::Queen()
{
cout << "Where would you like your Queen to go: ";
string l;
getline(cin, l);
int a, b;
if (l.length() == 2 && player_one == true)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Queen", "White", a, b);
}
else if (l.length() == 2 && player_one == false)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("Queen", "Black", a, b);
}
}
void Board::King()
{
cout << "Where would you like your King to go: ";
string l;
getline(cin, l);
int a, b;
if (l.length() == 2 && player_one == true)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("King", "White", a, b);
}
else if (l.length() == 2 && player_one == false)
{
a = PLocation2(l);
b = PLocation(l);
tileBoard.at(a).at(b) = Pieces();
tileBoard.at(a).at(b) = Pieces("King", "Black", a, b);
}
}