-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.cpp
More file actions
477 lines (445 loc) · 16.2 KB
/
Copy pathChess.cpp
File metadata and controls
477 lines (445 loc) · 16.2 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
#include<bits/stdc++.h>
#include<vector>
using namespace std;
class Chess_board{
// private:
public:
//checks if the piece in the cuurent position is the same as the piece entered by the user
bool check_valid_pie(int r1, int col1, string piece){
if(board[r1][col1]==piece){
return true;
}
return false;
}
void read_move(string move, int chance){
string piece="";
piece+=move[0];
piece+=move[1];
int row1 = 8-(move[4]-48);
int column1 = move[3] - 'a';
if(!check_valid_pie(row1, column1, piece)){
cout<<"Invalid piece entered "<<endl;
}
int row2 = 8-(move[7]-48);
int column2 = move[6]-'a';
int code = row2 * 10 + column2; //check again if it should be row2 and col2 !!!!!!!!!!!!!!@!
//cout<<row2<<" "<<column2<<endl;
chk_valid_mov(row2, column2, piece, row1, column1);
//cout<<"run"<<endl;
}
vector<vector<string>> board = {
{"bR","bk","bB","bQ","bK","bB","bk","bR"},
{"bP","bP","bP","bP","bP","bP","bP","bP"},
{"0","0","0","0","0","0","0","0"},
{"0","0","0","0","0","0","0","0"},
{"0","0","0","0","0","0","0","0"},
{"0","0","0","0","0","0","0","0"},
{"wP","wP","wP","wP","wP","wP","wP","wP"},
{"wR","wk","wB","wQ","wK","wB","wk","wR"},
};
int can_kill=0;
vector<string> dead;
int convert_col_name(char col){
int ans = 0;
if(col == 'a') ans = 0;
else if(col == 'b') ans = 1;
else if(col == 'c') ans = 2;
else if(col == 'd') ans = 3;
else if(col == 'e') ans = 4;
else if(col == 'f') ans = 5;
else if(col == 'g') ans = 6;
else if(col == 'h') ans = 7;
return ans;
}
map<string, pair<string, pair<int, int>>> location = {
//{pie, {unique_code ,{row, column}}}
{"bR", {"480", {48, 0}}},
{"bk", {"481", {48, 1}}},
{"bB", {"482", {48, 2}}},
{"bQ", {"483", {48, 3}}},
{"bK", {"484", {48, 4}}},
{"bB", {"485", {48, 5}}},
{"bk", {"486", {48, 6}}},
{"bR", {"487", {48, 7}}},
{"bP", {"470", {47, 0}}},
{"bP", {"471", {47, 1}}},
{"bP", {"472", {47, 2}}},
{"bP", {"473", {47, 3}}},
{"bP", {"474", {47, 4}}},
{"bP", {"475", {47, 5}}},
{"bP", {"476", {47, 6}}},
{"bP", {"477", {47, 7}}},
{"wR", {"550", {55, 0}}},
{"wk", {"551", {55, 1}}},
{"wB", {"552", {55, 2}}},
{"wQ", {"553", {55, 3}}},
{"wK", {"554", {55, 4}}},
{"wB", {"555", {55, 5}}},
{"wk", {"556", {55, 6}}},
{"wR", {"557", {55, 7}}},
{"wP", {"540", {54, 0}}},
{"wP", {"541", {54, 1}}},
{"wP", {"542", {54, 2}}},
{"wP", {"543", {54, 3}}},
{"wP", {"544", {54, 4}}},
{"wP", {"545", {54, 5}}},
{"wP", {"546", {54, 6}}},
{"wP", {"547", {54, 7}}}
};
//printing the board
void printboard(vector<vector<string>> board){
for(int i=-1; i<8; i++){
for(int j=0; j<8; j++){
if(i==-1){
char c=j+'a';
cout<<" "<<c<<"|";
}
else{
if(board[i][j]=="0"){
cout<<" "<<board[i][j]<<"|";
}
else{
cout<<board[i][j]<<"|";
}
}
}
if(i!=-1){
cout<<" "<<8-i;
}
cout<<endl;
}
}
bool check_valid_move(int del_col, int del_row, char piece){
/*
here i have to concider the fact
1. that i am not checking if the direction of the col and row is in the same direction.
2. not checked if there is any pieces in the way.
3. for the black, a step foreward is moving backwards as seen from white's perspective(which is the natural orientation of the board).
*/
if(piece == 'p'){
if(del_col == 1 && del_row == 1) return true;
else if(del_col == 1 && del_row == 0) return true;
}
else if(piece == 'k'){
if(del_col == 1 && del_row == 2) return true;
else if(del_col == 2 && del_row == 1) return true;
}
else if(piece == 'b'){
if(del_col == del_row) return true;
}
else if(piece == 'r'){
}
else if(piece == 'q'){
if(del_col == del_row) return true;
else if(del_col == 0 && del_row != 0) return true;
else if(del_col != 0 && del_row ==0) return true;
}
else if(piece == 'k'){
if(del_col == 1 && del_row == 0) return true;
else if(del_col == 0 && del_row == 1) return true;
else if(del_col == 1 && del_row == 1) return true;
}
return false;
}
void pawn_promo(int r2, int col2){
string promoted;
cout<<"Pawn Promoted, Enter promotion piece "<<endl;
cin>>promoted;
board[r2][col2]=promoted;
}
void check(int r2, int col2, string pie, int r1, int col1){
int chk = 0;
/*
should create a seperate map that stores the location(row and column) of both kings and the it will be easy to find location of kings in calculations
*/
// Check for white king
// Pawn Check
if(pie == "bP" && (board[r2 + 1][col2 + 1] == "wK" || board[r2 + 1][col2 - 1] == "wK")){
cout<<"White has a Check !"<<endl;
chk++;
}
// if(pie == "bP" && )
// Rook Check
else if(pie == "bR"){
for (int i = 0; i < 8; i++){
if(board[r2][i] != "0" && board[r2][i] != "wK" && i != col2){
chk = 0;
cout<<"No check"<<endl;
break;
}
else chk++;
}
for (int i = 0; i < 8; i++){
if(board[i][col2] != "0" && board[i][col2] != "wK" && i != r2){
chk = 0;
cout<<"No check"<<endl;
break;
}
else chk++;
}
if(chk != 0){
cout<<"White has a Check !"<<endl;
}
}
}
//check if the move of the piece given by the user is valid
void chk_valid_mov(int r2, int col2, string pie, int r1, int col1){
can_kill=0;
if(board[r2][col2]!="0"){
if(pie[0]=='w' && board[r2][col2][0]=='w'){
cout<<"Invalid move"<<endl;
can_kill= -1;
}
else if(pie[0]=='b' && board[r2][col2][0]=='b'){
cout<<"Invalid move"<<endl;
can_kill= -1;
}
else{
can_kill=1;
}
}
//PAWN RULEBOOK
if(pie=="bP" || pie=="wP"){
if(pie == "wP"){
if(r1 - r2 == 1 && col2 - col1 == 0 && can_kill == 0){
cout<<"Valid move"<<endl;
move_pie(r1, col1, r2, col2, pie);
if(r2==0){
pawn_promo(r2, col2);
}
}
else if(r1 - r2 == 1 && abs(col2 - col1) == 1 && can_kill == 1){
cout<<"Valid move"<<endl;
move_pie(r1, col1, r2, col2, pie);
}
else if(r1==6 && r1-r2==2 && can_kill==0){
cout<<"Valid move"<<endl;
move_pie(r1, col1, r2, col2, pie);
}
else{
cout<<"Invalid move"<<endl;
}
}
else if(pie == "bP"){
if(r2 - r1 == 1 && col2 - col1 == 0 && can_kill == 0){
cout<<"Valid move"<<endl;
move_pie(r1, col1, r2, col2, pie);
}
else if(r2 - r1 == 1 && abs(col2 - col1) == 1 && can_kill == 1){
cout<<"Valid move"<<endl;
move_pie(r1, col1, r2, col2, pie);
}
else if(r1==1 && r2-r1==2 && can_kill==0){
cout<<"Valid move"<<endl;
move_pie(r1, col1, r2, col2, pie);
}
else{
cout<<"Invalid move"<<endl;
}
}
//In future add EN PASSANTE RULE FOR PAWN
}
// ROOK RULEBOOK
else if(pie == "wR" || pie == "bR"){
int lol = 0; //to check if any piece in path of the roook
// verticle movement
if(col1==col2){
int n = abs(r1 - r2);
// cout<<"lolllll"<<endl;
cout<<n<<endl;
for(int i = 1; i < n-1; i++){
if(pie=="wR"){
if(board[r1-i][col2] != "0"){
cout<<"Invalid move"<<endl;
lol++;
break;
}
}
else if(pie=="bR"){
if(board[r1+i][col2] != "0"){
cout<<"Invalid move"<<endl;
lol++;
break;
}
}
}
}
// horizontal movement
else if(r1==r2){
int m = abs(col1 - col2);
cout<<m<<endl;
for(int i = 1; i < m+1; i++){
if(board[r2][col1+i] != "0" && can_kill == 0){
cout<<"Invalid move"<<endl;
lol++;
break;
}
}
}
// cout<<"lol "<<lol<<endl;
// cout<<"r1 "<<r1<<", r2 "<<r2<<", col1 "<<col1<<", col2 "<<col2<<endl;
if(lol == 0){
move_pie(r1, col1, r2, col2, pie);
}
}
// KNIGHT RULEBOOK
else if(pie == "wk" || pie == "bk"){
if((abs(r2-r1)==2 && abs(col2-col1)==1) || (abs(r2-r1)==1 && abs(col2-col1)==2)){
move_pie(r1,col1,r2,col2,pie);
}
}
// BHISHOP RULEBOOK
else if(pie == "wB" || pie == "bB"){
int lemao = 0; //to check if any piece in path of bhishop
for(int i = 1; i < abs(r2-r1)-1; i++){
if(pie == "wB"){
if(board[r1-i][col1-i] != "0"){
cout<<"Invalid~move"<<endl;
lemao++;
break;
}
}
else if(pie == "bB"){
if(board[r1+i][col1+i] != "0"){
cout<<"Invalid~~move"<<endl;
lemao++;
break;
}
}
}
if(lemao == 0){
move_pie(r1,col1,r2,col2,pie);
}
}
// QUEEN RULEBOOK
else if(pie == "wQ" || pie == "bQ"){
int lmao = 0;
if(col1==col2){
int n = abs(r1 - r2);
// cout<<n<<endl;
for(int i = 1; i < n; i++){
if(pie=="wQ"){
if(board[r2][col2][0] == board[r1][col1][0] && board[r1-i][col2] != "0"){
cout<<"Invalid+move"<<endl;
lmao++;
break;
}
}
else if(pie=="bQ"){
if(board[r2][col2][0] == board[r1][col1][0] && board[r1+i][col2] != "0"){
cout<<"Invalid=move"<<endl;
lmao++;
break;
}
}
}
if(lmao == 0){
// cout<<"here_1"<<lmao;
move_pie(r1,col1,r2,col2,pie);
}
}
// horizontal movement
else if(r1==r2){
int lmao = 0;
int m = abs(col1 - col2);
for(int i = 0; i < m-1; i++){
if(board[r2][col2][0] == board[r1][col1][0] && board[r2][col1+i] != "0"){
cout<<"Invalid move"<<endl;
lmao++;
break;
}
}
if(lmao == 0){
move_pie(r1, col1, r2, col2, pie);
}
}
// Diagnol Movement
else if(abs(r2-r1) == abs(col2-col1)){
for(int i = 1; i < abs(r2-r1)-1; i++){
if(pie == "wQ"){
if(board[r2][col2][0] == 'w' && board[r1+i][col1+i] != "0"){
cout<<"Invalid*move"<<i<<endl;
lmao++;
break;
}
}
else if(pie == "bQ"){
if(board[r2][col2][0] == 'b' && board[r1-i][col1-i] != "0"){
cout<<"Invalid/move"<<endl;
lmao++;
break;
}
}
}
// cout<<lmao<<endl;
if(lmao == 0){
// cout<<"here_2";
move_pie(r1, col1, r2, col2, pie);
}
}
}
// KING RULEBOOK
else if(pie == "wK" || pie == "bK"){
int lol = 0;
// Horizontal movement
if(r1 == r2){
if(can_kill != 1){
lol++;
}
}
// Verticle movement
else if(col1 == col2){
if(can_kill != 1){
lol++;
}
}
// Diagonal movement
else if(abs(r2-r1) == abs(col2-col1)){
if(can_kill == -1){
lol++;
}
}
if(lol == 0){
move_pie(r1,col1,r2,col2,pie);
}
}
}
void move_pie(int r1, int col1, int r2, int col2, string pie){
if(can_kill==1){
dead.push_back(board[r2][col2]);
}
if(can_kill==0 || can_kill==1){
board[r2][col2]=board[r1][col1];
board[r1][col1]="0";
printboard(board);
}
if(location[pie].second.first == r1 && location[pie].second.second == col1){
// if(location[pie].first ==)
location[pie].second.first = r2;
location[pie].second.second = col2;
cout<<"location.first"<<location[pie].second.first<<endl;
cout<<"location.second"<<location[pie].second.second<<endl;
// }
}
}
};
int main(){
Chess_board c1;
int x = 0;
int chance = 0; //if chance = 0 -> white's turn & chance = 1 -> black's turn
c1.printboard(c1.board);
while(x<16){
string move;
cout<<"which key u want to move?"<<endl;
cin>>move;
c1.read_move(move, chance);
//Input move in the format: "Wk,b1,c3"(for knight to e4).
x++;
chance++;
}
cout<<"Dead ones are: "<<endl;
for(int j=0; j<c1.dead.size(); j++){
cout<<c1.dead[j]<<endl;
}
return 0;
}