Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
327 lines (299 loc) · 9.67 KB
/
Copy pathmain.cpp
File metadata and controls
327 lines (299 loc) · 9.67 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
#include "chessboard.hpp"
#include "chessAI.hpp"
#include <iostream>
#include <string>
#include <limits>
#include <utility>
using namespace std;
bool validResponse(string answer);
pair<int,int> convertFromBoardSpaceToCoords(string move);
string convertFromCoordsToBoardSpace(pair<int,int> coords);
int convertToNumber(char c);
string convertToLetter(int n);
int main() {
Chessboard chessboard;
int turnCounter = 0;
string startSpace;
string endSpace;
string player;
pair<int,int> startCoords;
pair<int,int> endCoords;
bool valid = true;
string answer;
do {
cout << "2p game or vs AI?" << endl;
cin >> answer;
valid = validResponse(answer);
if(!valid) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
} while(!valid);
cout << endl;
// 2 player game
if(answer == "2p") {
while(!chessboard.getIsWhiteInCheckmate() && !chessboard.getIsBlackInCheckmate() && !chessboard.getIsStalemate()) {
chessboard.printBoard();
cout << endl;
turnCounter++;
if(turnCounter % 2 == 1) {
cout << "White's turn" << endl;
player = "white";
if(chessboard.getIsWhiteInCheck()) {
cout << "White is in Check" << endl;
}
}
else {
cout << "Black's turn" << endl;
player = "black";
if(chessboard.getIsBlackInCheck()) {
cout << "Black is in Check" << endl;
}
}
do {
do {
cout << "Enter move: ";
cin >> startSpace;
valid = chessboard.validSpace(startSpace, 0);
if(!valid) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
}
while(!valid);
do {
cin >> endSpace;
valid = chessboard.validSpace(endSpace, 1);
if(!valid) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
}
while(!valid);
startCoords = convertFromBoardSpaceToCoords(startSpace);
endCoords = convertFromBoardSpaceToCoords(endSpace);
valid = chessboard.move(player, startCoords, endCoords, false);
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
while(!valid);
cout<<endl;
}
chessboard.printBoard();
cout << endl;
// Check to see if someone won or if it is a tie
if(chessboard.getIsWhiteInCheckmate()) {
cout << "Checkmate" << endl;
cout << "Black wins!" << endl;
}
else if(chessboard.getIsBlackInCheckmate()) {
cout << "Checkmate" << endl;
cout << "White wins!" << endl;
}
else if(chessboard.getIsStalemate()) {
cout << "Stalemate" << endl;
}
}
// Versus AI
else {
do {
cout << "Choose to play as white or black?" << endl;
cin >> answer;
for(int i = 0; i < answer.length(); i++) {
answer[i] = tolower(answer[i]);
}
if(answer != "white" && answer != "black") {
cout << "Invalid response: enter white or black" << endl;
valid = false;
}
else {
valid = true;
}
if(!valid) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
} while(!valid);
cout << endl;
player = answer;
string aiColor;
bool aiTurn;
std::pair<std::pair<int,int>, std::pair <int,int>> move;
if(player == "white") {
aiColor = "black";
aiTurn = false;
}
else {
aiColor = "white";
aiTurn = true;
}
ChessAI AI(aiColor);
while(!chessboard.getIsWhiteInCheckmate() && !chessboard.getIsBlackInCheckmate() && !chessboard.getIsStalemate()) {
chessboard.printBoard();
cout << endl;
turnCounter++;
if(turnCounter % 2 == 1) {
cout << "White's turn" << endl;
if(chessboard.getIsWhiteInCheck()) {
cout << "White is in Check" << endl;
}
}
else {
cout << "Black's turn" << endl;
if(chessboard.getIsBlackInCheck()) {
cout << "Black is in Check" << endl;
}
}
std::string m;
// Player's turn
if(!aiTurn) {
do {
do {
cout << "Enter move: ";
cin >> startSpace;
valid = chessboard.validSpace(startSpace, 0);
if(!valid) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
}
while(!valid);
do {
cin >> endSpace;
valid = chessboard.validSpace(endSpace, 1);
if(!valid) {
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
}
while(!valid);
startCoords = convertFromBoardSpaceToCoords(startSpace);
endCoords = convertFromBoardSpaceToCoords(endSpace);
valid = chessboard.move(player, startCoords, endCoords, false);
cin.clear();
cin.ignore(numeric_limits<std::streamsize>::max(), '\n');
}
while(!valid);
cout<<endl;
}
// AI's turn
else if(aiTurn) {
cout << "The AI is thinking..." << endl;
move = AI.getNextMove(chessboard);
startCoords = move.first;
endCoords = move.second;
startSpace = convertFromCoordsToBoardSpace(startCoords);
endSpace = convertFromCoordsToBoardSpace(endCoords);
cout << "The AI's move is: " << startSpace << " " << endSpace << endl;
chessboard.move(aiColor, startCoords, endCoords, true);
cout<<endl;
}
aiTurn = !aiTurn;
}
chessboard.printBoard();
cout << endl;
// Check to see if someone won or if it is a tie
if(chessboard.getIsWhiteInCheckmate()) {
cout << "Checkmate" << endl;
cout << "Black wins!" << endl;
}
else if(chessboard.getIsBlackInCheckmate()) {
cout << "Checkmate" << endl;
cout << "White wins!" << endl;
}
else if(chessboard.getIsStalemate()) {
cout << "Stalemate" << endl;
}
}
return 0;
}
// Checks to see if the user inputted either "2p" or "ai"
bool validResponse(string answer) {
for(int i = 0; i < answer.length(); i++) {
if(isalpha(answer[i]))
answer[i] = tolower(answer[i]);
}
if(answer != "2p" && answer != "ai") {
cout << "Invalid response: enter 2p or AI" << endl;
return false;
}
return true;
}
// Converts the user inputted space, such as "a3", to it's corresponding coordinates on the board
std::pair<int,int> convertFromBoardSpaceToCoords(string move) {
int x = convertToNumber(move[0]);
int y = move[1] - '1';
return make_pair(x,y);
}
//Converts the coordinates of the AI's move into the format of "e2"
string convertFromCoordsToBoardSpace(pair<int,int> coords) {
string result = "";
result = result + convertToLetter(coords.first) + to_string(coords.second+1);
return result;
}
// Converts a letter between a and h to its corresponding index on the chessboard
int convertToNumber(char c) {
switch(c) {
case 'a':
return 0;
break;
case 'b':
return 1;
break;
case 'c':
return 2;
break;
case 'd':
return 3;
break;
case 'e':
return 4;
break;
case 'f':
return 5;
break;
case 'g':
return 6;
break;
case 'h':
return 7;
break;
default:
std::cout << "Unexpected letter, exiting program" << std::endl;
exit(1);
}
return -99;
}
// converts a number between 0 and 7 to its corresponding letter on the chessboard
string convertToLetter(int n) {
switch(n) {
case 0:
return "a";
break;
case 1:
return "b";
break;
case 2:
return "c";
break;
case 3:
return "d";
break;
case 4:
return "e";
break;
case 5:
return "f";
break;
case 6:
return "g";
break;
case 7:
return "h";
break;
default:
std::cout << "Unexpected number, exiting program" << std::endl;
exit(1);
}
return "error";
}