-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
501 lines (347 loc) · 18.5 KB
/
Copy pathMain.cpp
File metadata and controls
501 lines (347 loc) · 18.5 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
///////////////////////////////////////////////////////22I-0944//////////////////////////////////////////////////////
/////////////////////////////////////////////////////////22I-1120///////////////////////////////////////////////////
/////////////////////////////////////////////////////////Twaha Nadeem//////////////////////////////////////////////
/////////////////////////////////////////////////////////Asad Mehdi///////////////////////////////////////////////
/////////////////////////////////////////////////////////PF_lab_project_D////////////////////////////////////////
/////////////////////////////////////////////////////////submitted to-:Maam Maryam Shahbaz//////////////////////
/////////////////////////////////////////////////////////submission date-:12/5/2023////////////////////////////
/////////////////////////////////////////////////////////Chess////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include<cstdlib>
#include <string>
#include <cstring>
#include<ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
/////////////////////////////////////////////Global array///////////////////////////////////////////////
char board[8][8] = {
{'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'},
{' ', '%', ' ', '%', ' ', '%', ' ', '%'},
{'%', ' ', '%', ' ', '%', ' ', '%', ' '},
{' ', '%', ' ', '%', ' ', '%', ' ', '%'},
{'%', ' ', '%', ' ', '%', ' ', '%', ' '},
{' ', '%', ' ', '%', ' ', '%', ' ', '%'},
{'%', ' ', '%', ' ', '%', ' ', '%', ' '},
{'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}
};
///////////////start/////////////////////////////////////////////////////////////////
void start();
////////////////////Display movements of chess pieces ,and show the rule of check and checkmate////////////////////////////////////////////////
void Instructions();
////////////////////Display Chessboard////////////////////////////////////////////////
void chessboard();
////////////////////movement of chess pieces////////////////////////////////////////////
bool movement(int, int, int, int);
//////////////////////////Validity check to check if the chess pieces of the same side is landing on its own pieces /////////////////////
bool validmovehitting1(char board[][8], int initialrow, int initialcolumn, int nextrow, int nextcolumn, int s);
//////////////////////////Validity check to check if the chess pieces of the same side is landing on its own pieces /////////////////////
bool validmovehitting2(char board[][8], int initialrow, int initialcolumn, int nextrow, int nextcolumn, int s);
//////////////////////////////////Validity check for movement of Queen //////////////////
bool ValidyofQueenMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn);
/////////////////////////////////Validity check for movement of Knight //////////////////
bool ValidyofKnightMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn);
/////////////////////////////////Validity check for movement of King ///////////////////////////////////////////////////
bool ValidyofKingMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn);
/////////////////////////////////Validity check for movement of Rook //////////////////
bool ValidyofRookMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn);
/////////////////////////////////Validity check for movement of Rook //////////////////
bool ValidyofBishopMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn);
// WHen king is surrounded by all opponents game over player wins
void isCheckmate1()
{
int enemyPieceCount = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
char piece = board[i][j];
if (piece == 'n' || piece == 'b' || piece == 'q' || piece == 'r')
{
enemyPieceCount++;
}
}
}
if (enemyPieceCount == 0)
{
cout<< "PLAYER 1 WINS";
exit(0);
}
}
// WHen king is surrounded by all opponents game over player wins
void isCheckmate2() {
int enemyPieceCount = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
char piece = board[i][j];
if (piece == 'N' || piece == 'B' || piece == 'Q' || piece == 'R')
{
enemyPieceCount++;
}
}
}
if (enemyPieceCount == 0)
{
cout << "PLAYER 2 WINS";
exit(0);
}
}
int main()
{
system("Color F0");
////////////////////////////////////////////////Starting Interface//////////////////////////////////////////////////////////////
cout << "***********************************************************************************************************************";
cout << setw(80) << " _____ _ _ _____ _____ _____\n";
cout << setw(80) << " / ____| | | | | | ____| / ____| / ____|\n";
cout << setw(81) << "| | | |__| | | |__ | (___ | (___ \n";
cout << setw(80) << "| | | __ | | __| \\___ \\ \\___ \\ \n";
cout << setw(80) << "| |____ | | | | | |___ ____)| ____)|\n";
cout << setw(46) << " \\_____| " << " |_| |_| |_____| |_____/ |_____/ \n";
cout << "***********************************************************************************************************************" << endl << endl << endl;
cout << "\n\n\t\t\t\t 'Without error there can be no brilliancy'\n\n\n\n\n";
cout << "\n\n\t\t\t\t\tPress Enter to Continue ...\n\n\n\n\n";
_getch();
cout << "\a\a\a\a\a\a\a";
system("cls");
system("Color F0");
start();
return 0;
}
void start()
{
char choice;
int quotes;
srand(time(0));
quotes = (rand() % 8) + 1;
////////////////////////////////////////////////Starting Interface//////////////////////////////////////////////////////////////
cout << "***********************************************************************************************************************";
cout << setw(80) << " _____ _ _ _____ _____ _____\n";
cout << setw(80) << " / ____| | | | | | ____| / ____| / ____|\n";
cout << setw(81) << "| | | |__| | | |__ | (___ | (___ \n";
cout << setw(80) << "| | | __ | | __| \\___ \\ \\___ \\ \n";
cout << setw(80) << "| |____ | | | | | |___ ____)| ____)|\n";
cout << setw(46) << " \\_____| " << " |_| |_| |_____| |_____/ |_____/ \n";
cout << "***********************************************************************************************************************" << endl << endl << endl;
bool b = true;
while (b)
{
cout << "Menu: " << endl << endl << endl;
cout << "(N)ew game" << endl;
cout << "(I)nstruction" << endl;
cout << "(Q)uit" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice == 'N' || choice == 'n')
{
chessboard();
}
else if (choice == 'I' || choice == 'i')
{
Instructions();
}
else if (choice == 'Q' || choice == 'q') {
switch (quotes) {
case 1:
cout << "Once the game is over, he says, the king and the pawn go back in the same box. In lifeand death we are equal. " << endl << " - Lisa Renee Jones" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
case 2:
cout << "When you see a good move, look for a better one. " << endl << " -Emanuel Lasker" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
case 3:
cout << "Every chess master was once a beginner. " << endl << " -Irving Chernev" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
case 4:
cout << "My problem with chess was that all my pieces wanted to end the game as soon as possible. " << endl << " -Dave Barry" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
case 5:
cout << "In life, as in chess, forethought wins. " << endl << " -Charles Buxton" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
case 6:
cout << "Critical thinking is the most important factor with chess. As it is in life, you need to think before you make decisions. " << endl << "-Hikaru Nakamura" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
case 7:
cout << "Some part of a mistake is always correct. " << endl << " -Savielly Tartakover" << endl << endl;
cout << "******************************************************Game Ended!******************************************************" << endl << endl;
break;
}
b = false;
}
}
}
void Instructions() {
system("cls");
cout << "King: The king can move one square in any direction: horizontally, vertically, or diagonally\nQueen: The queen is the most powerful pieceand can move any number of squares in any direction : horizontally, vertically, or diagonally\nBishop : The bishop can move any number of squares diagonally.It stays on the same color of the square throughout the game.\nRook : The rook can move any number of squares horizontally or vertically\nKnight : The knight moves in an L shape, consisting of two squares in one direction(horizontally or vertically) and then one square in a perpendicular direction.The knight can jump over other pieces\nCheck :\n\nWhen a player's king is under threat of capture on the opponent's next move, it is considered to be in check. The player must make a move to remove the king from check on their next turn\nThe opponent's piece that is putting the king in check cannot be captured or blocked on the next move\nCheckmate \n If a player's king is in check and there is no legal move to remove the king from check, it is considered to be in checkmate\nWhen a checkmate occurs, the game ends, and the player in checkmate loses the game.";
cout << "\nDo you wanna go Back to Main Menu. Press Enter ......... \n";
_getch();
system("cls");
}
void chessboard() {
const int s = 8;
int initialrow, initialcolumn, nextrow, nextcolumn;
bool b = 1;
bool PlayerTurn = true;
/////////// Display the board
while (b)
{
system("cls");
cout << endl << endl << endl;
for (int i = 0; i < s + 1; i++) {
for (int j = 0; j < s + 1; j++) {
if (i == 0) {
if (j == 8)
cout << " ";
else if (i == 0)
cout << "\t" << j;
}
else if (j == 0) {
cout << endl << i - 1 << "\t";
}
if (i > 0 && j > 0)
cout << board[i - 1][j - 1] << "\t";
}
if (i == 0)
cout << endl;
else
cout << endl << endl;
}
if (PlayerTurn)
{
cout << "\nPlayer 1: \n";
}
else
{
cout << "\nPlayer 2: \n";
}
////////// Taking input for movemennt ///
cout << "Enter the coordinates of the chess piece you want to move respectively(i.e 02): ";
cin >> initialrow >> initialcolumn;
cout << "Enter the coordinates of the point where you want to move the chess piece on the chessboard(i.e 04): ";
cin >> nextrow >> nextcolumn;
if (PlayerTurn) {
if (validmovehitting1(board, initialrow, initialcolumn, nextrow, nextcolumn, s))
continue;
if (!(board[initialrow][initialcolumn] == 'N' || board[initialrow][initialcolumn] == 'K' || board[initialrow][initialcolumn] == 'B' || board[initialrow][initialcolumn] == 'Q' || board[initialrow][initialcolumn] == 'R'))
{
cout << "\nInvalid Move Player 1\n";
Sleep(2000);
continue;
}
}
else {
if (validmovehitting2(board, initialrow, initialcolumn, nextrow, nextcolumn, s))
continue;
if (!(board[initialrow][initialcolumn] == 'n' || board[initialrow][initialcolumn] == 'k' || board[initialrow][initialcolumn] == 'b' || board[initialrow][initialcolumn] == 'q' || board[initialrow][initialcolumn] == 'r'))
{
cout << "\nInvalid Move Player 2\n";
Sleep(2000);
continue;
}
}
////////////////////////////////////////Validity of all the chess moves/////////////////////////////////////
if (!movement(initialrow,initialcolumn, nextrow, nextcolumn))
continue;
if ((board[initialrow][initialcolumn] == 'N' || board[initialrow][initialcolumn] == 'n') && !ValidyofKnightMove(initialrow, initialcolumn, nextrow, nextcolumn))
continue;
if ((board[initialrow][initialcolumn] == 'K' || board[initialrow][initialcolumn] == 'k') && !ValidyofKingMove(initialrow, initialcolumn, nextrow, nextcolumn))
continue;
if ((board[initialrow][initialcolumn] == 'R' || board[initialrow][initialcolumn] == 'r') && !ValidyofRookMove(initialrow, initialcolumn, nextrow, nextcolumn))
continue;
if ((board[initialrow][initialcolumn] == 'B' || board[initialrow][initialcolumn] == 'b') && !ValidyofBishopMove(initialrow, initialcolumn, nextrow, nextcolumn))
continue;
if ((board[initialrow][initialcolumn] == 'Q'|| board[initialrow][initialcolumn] == 'q') && !ValidyofQueenMove(initialrow, initialcolumn, nextrow, nextcolumn))
continue;
board[nextrow][nextcolumn] = board[initialrow][initialcolumn];
board[initialrow][initialcolumn] = ' ';
isCheckmate1();
isCheckmate2();
PlayerTurn = !PlayerTurn;
}
}
bool validmovehitting1(char board[][8], int initialrow, int initialcolumn, int nextrow, int nextcolumn, int s)
{
if (board[nextrow][nextcolumn] == 'K' || board[nextrow][nextcolumn] == 'R' || board[nextrow][nextcolumn] == 'N' || board[nextrow][nextcolumn] == 'B')
{
cout << "Invalid Move , Hitting on your Piece"; return true;
}
else return false;
}
bool validmovehitting2(char board[][8], int initialrow, int initialcolumn, int nextrow, int nextcolumn, int s)
{
if (board[nextrow][nextcolumn] == 'k' || board[nextrow][nextcolumn] == 'r' || board[nextrow][nextcolumn] == 'n' || board[nextrow][nextcolumn] == 'b')
{
cout << "Invalid Move , Hitting on your Piece"; return true;
}
else return false;
}
bool movement(int initialrow, int initialcolumn, int nextrow, int nextcolumn) {
if ((initialrow >= 0 && initialrow <= 7 && initialcolumn >= 0 && initialcolumn <= 7 && nextrow >= 0 && nextrow <= 7 && nextcolumn >= 0 && nextcolumn <= 7))
{
return true;
}
else
{
cout << "Invalid Move, out of Board";
return false;
}
}
bool ValidyofKnightMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn) {
//////////////////////////////// (Knight moves in an L-shape by restraining it going to the points where queen can move (validity) )///////////////////////////////////////////////////
if (!ValidyofQueenMove(initialrow, initialcolumn, nextrow, nextcolumn) && nextrow >= initialrow - 2 && nextrow <= initialrow + 2 && nextcolumn >= initialcolumn - 2 && nextcolumn <= initialcolumn + 2) {
return true;
}
cout << "Inavalid Knight Move";
return false;
}
bool ValidyofKingMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn) {
int rowsdifference = abs(nextrow - initialrow);
int columnsdifference = abs(nextcolumn - initialcolumn);
/////////////////////////////////Moving one Square in all direction///////////////////////////////
if ((rowsdifference == 1 && columnsdifference == 0) || (rowsdifference == 0 && columnsdifference == 1) || (rowsdifference == 1 && columnsdifference == 1)) {
return true;
}
cout << "Inavalid King Move";
return false;
}
bool ValidyofRookMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn) {
//// Moving Left or Right
if (initialrow == nextrow && initialcolumn != nextcolumn) {
return true;
}
///// Moving Straight
if (initialrow != nextrow && initialcolumn == nextcolumn) {
return true;
}
cout << "Inavalid Rook Move";
return false;
}
bool ValidyofBishopMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn) {
int rowsdifference = abs(nextrow - initialrow);
int columnsdifference = abs(nextcolumn - initialcolumn);
///////////////////////////// Using diagonally (equal row and column differences)/////////////////////////////////////
if (rowsdifference == columnsdifference) {
return true;
}
cout << "Inavalid Bishoop Move";
return false;
}
bool ValidyofQueenMove(int initialrow, int initialcolumn, int nextrow, int nextcolumn) {
////////////// using Moving Rook horizontally or vertically as validity for Queen////////////////////////////////////////
if (ValidyofRookMove(initialrow, initialcolumn, nextrow, nextcolumn)) {
return true;
}
////////////// using Moving Bishop in queen as validity/////////////////////////////////////////
if (ValidyofBishopMove(initialrow, initialcolumn, nextrow, nextcolumn)) {
return true;
}
cout << "Inavalid Queen Move";
return false;
}