-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.cpp
More file actions
669 lines (621 loc) · 20.9 KB
/
Engine.cpp
File metadata and controls
669 lines (621 loc) · 20.9 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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
ChessBoard Interactive Backend C++ Engine for deciding moves
Written By: Kshitij Tomar
*/
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
//#include <httplib.h> //Requires configuration of this library, helps to cross-platform with HTTP/HTTPS
//#include <fstream>
using namespace std;
const int NONE = -1, WHITE = 0, BLACK = 1; //constansts needed to color validation
// Piece structure representing each chessboard piece
struct Piece
{
char symbol = ' ';
int color = NONE; //By default, their is no color to a location on the board if there is no peice on it
};
int player_turn = NONE;
// Function to initialize the chessboard
vector<vector<Piece> > initialize_board()
{
player_turn = WHITE; //game starts with white's move
vector<vector<Piece> > chessboard(8, vector<Piece>(8));
// Initialize pawns
for (int i = 0; i < 8; ++i)
{
chessboard[1][i].symbol = 'P';
chessboard[1][i].color = BLACK;
chessboard[6][i].symbol = 'P';
chessboard[6][i].color = WHITE;
}
// Initialize other pieces
chessboard[0][0].symbol = 'R';
chessboard[0][0].color = BLACK;
chessboard[0][7].symbol = 'R';
chessboard[0][7].color = BLACK;
chessboard[7][0].symbol = 'R';
chessboard[7][0].color = WHITE;
chessboard[7][7].symbol = 'R';
chessboard[7][7].color = WHITE;
chessboard[0][1].symbol = 'N';
chessboard[0][1].color = BLACK;
chessboard[0][6].symbol = 'N';
chessboard[0][6].color = BLACK;
chessboard[7][1].symbol = 'N';
chessboard[7][1].color = WHITE;
chessboard[7][6].symbol = 'N';
chessboard[7][6].color = WHITE;
chessboard[0][2].symbol = 'B';
chessboard[0][2].color = BLACK;
chessboard[0][5].symbol = 'B';
chessboard[0][5].color = BLACK;
chessboard[7][2].symbol = 'B';
chessboard[7][2].color = WHITE;
chessboard[7][5].symbol = 'B';
chessboard[7][5].color = WHITE;
chessboard[0][3].symbol = 'Q';
chessboard[0][3].color = BLACK;
chessboard[0][4].symbol = 'K';
chessboard[0][4].color = BLACK;
chessboard[7][3].symbol = 'Q';
chessboard[7][3].color = WHITE;
chessboard[7][4].symbol = 'K';
chessboard[7][4].color = WHITE;
return chessboard;
}
vector<vector<Piece> > chessboard = initialize_board(); //Global Initialization of ChessBoard to keep track of moves
/*
@param int i: An integer ranging from 0 to 8
@param int j: An integer randing from 0 to 8
@return bool: Return whether a given square has a specific color attribute
This function is called to check whether a specific square has a specific color
*/
bool check_piece_color(int i, int j, int color)
{
return chessboard[i][j].color == color;
}
/*
This function returns the entirety of ther chess board to the terminal screen when it is called - used for testing
*/
void print_board(const vector<vector<Piece> >& chessboard)
{
for (int i = 0; i < 8; ++i)
{
cout << i << " ";
for (int j = 0; j < 8; ++j)
{
if (chessboard[i][j].symbol == ' ')
{
cout << " ";
}
else
{
cout << chessboard[i][j].symbol << " ";
}
}
cout << endl;
}
cout << " ";
for (int j = 0; j < 8; j++)
{
cout << j << " ";
}
cout << endl;
}
/*
@param row: An int type variable that tells the row at which a chess peice is located
@param column: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves that are horizontally or vertically accessible by a peice on a coordinate - used for rooks
*/
vector<pair<int, int> > straight_moves(int row, int column)
{
vector<pair<int, int> > moves;
cout << "rook" << endl;
bool empty = true;
int i = row+1;
int j = column;
while (empty && i < 8)
{
if (chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
else
{
empty = false;
}
i++;
}
empty = true;
i = row;
j = column + 1;
while (empty && j < 8)
{
if (chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
else
{
empty = false;
}
j++;
}
empty = true;
i = row;
j = column - 1;
while (empty && j > -1)
{
if (chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
else
{
empty = false;
}
j--;
}
empty = true;
i = row - 1;
j = column;
while (empty && i > -1)
{
if (chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
else
{
empty = false;
}
i--;
}
return moves;
}
/*
@param row: An int type variable that tells the row at which a chess peice is located
@param column: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves that are L-shape accessible by a peice on a coordinate - used for knights
*/
vector<pair<int, int> > knight_moves(int row, int column)
{
vector<pair<int, int> > moves;
cout << "knight" << endl;
int i = row + 2;
int j = column + 1;
if (i < 8 && j < 8 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row + 1;
j = column + 2;
if (i < 8 && j < 8 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row + 1;
j = column - 2;
if (i < 8 && j > -1 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row + 2;
j = column - 1;
if (i < 8 && j > -1 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row - 2;
j = column + 1;
if (i > -1 && j < 8 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row - 1;
j = column + 2;
if (i > -1 && j < 8 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row - 1;
j = column - 2;
if (i > -1 && j > -1 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
i = row - 2;
j = column - 1;
if (i > -1 && j > -1 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
return moves;
}
/*
@param row: An int type variable that tells the row at which a chess peice is located
@param column: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves that are diagonally accessible by a peice on a coordinate - used for bishops
*/
vector<pair<int, int> > bishop_moves(int row, int column)
{
vector<pair<int, int> > moves;
cout << "bishop " << row << " " << column << endl;
bool empty = true;
int i = row + 1;
int j = column + 1;
while (empty && i < 8 && j < 8 && chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
i++;
j++;
}
empty = true;
i = row - 1;
j = column + 1;
while (empty && i > -1 && j < 8 && chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
i--;
j++;
}
empty = true;
i = row + 1;
j = column - 1;
while (empty && i < 8 && j > -1 && chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
i++;
j--;
}
empty = true;
i = row - 1;
j = column - 1;
while (i > -1 && j > -1 && chessboard[i][j].color != chessboard[row][column].color)
{
if (chessboard[i][j].symbol != ' ')
{
empty = false;
}
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
i--;
j--;
}
return moves;
}
/*
@param row: An int type variable that tells the row at which a chess peice is located
@param column: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves that are diagonally/horizontally/vertically accessible by a peice on a coordinate - used for queens
*/
vector<pair<int, int> > queen_moves(int row, int column)
{
vector<pair<int, int> > moves;
cout << "queen " << row << " " << column << endl;
moves.insert(moves.end(), bishop_moves(row, column).begin(), bishop_moves(row, column).end());
moves.insert(moves.end(), straight_moves(row, column).begin(), straight_moves(row, column).end());
return moves;
}
/*
@param row: An int type variable that tells the row at which a chess peice is located
@param column: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves that are one square away in all directions from on a coordinate - used for kings
*/
vector<pair<int, int> > king_moves(int row, int column)
{
vector<pair<int, int> > moves;
cout << "king" << endl;
int i = row - 1;
int j = column - 1;
// Check if the new position is within the chessboard boundaries
for (int i = row - 1; i <= row +1; i++)
{
for (int j = column - 1; j <= column + 1; j++)
{
// Check if the square is either empty or has an opponent's piece
if (i < 8 && i >= 0 && j < 8 && j >= 0 && chessboard[i][j].color != chessboard[row][column].color)
{
moves.push_back(make_pair(i, j));
cout << i << " " << j << endl;
}
}
}
return moves;
}
/*
@param row: An int type variable that tells the row at which a chess peice is located
@param column: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the pawn moves in a given position - allowing captures
*/
vector<pair<int, int> > pawn_moves(int row, int column)
{
vector<pair<int, int> > moves;
cout << chessboard[row][column].color << " pawn " << row << " " << column << endl;
if (chessboard[row][column].color == BLACK)
{
int i = row + 1;
if (chessboard[i][column+1].color != chessboard[row][column].color && !check_piece_color(i, column + 1, NONE))
{
moves.push_back(make_pair(i, column+1));
cout << i << " " << column+1 << endl;
}
if (chessboard[i][column-1].color != chessboard[row][column].color && !check_piece_color(i, column - 1, NONE))
{
moves.push_back(make_pair(i, column-1));
cout << i << " " << column-1 << endl;
}
if (chessboard[i][column].color == NONE)
{
moves.push_back(make_pair(i, column));
cout << i << " " << column << endl;
}
if (chessboard[i][column].color == NONE && chessboard[i+1][column].color == NONE && row == 1)
{
moves.push_back(make_pair(i+1, column));
cout << i+1 << " " << column << endl;
}
}
else
{
int i = row - 1;
if (i >= 0 && column+1 < 8 && chessboard[i][column+1].color != chessboard[row][column].color && chessboard[i][column+1].color != NONE)
{
moves.push_back(make_pair(i, column+1));
cout << i << " " << column+1 << endl;
}
if (i >= 0 && column-1 >= 0 && chessboard[i][column-1].color != chessboard[row][column].color && chessboard[i][column-1].color != NONE)
{
moves.push_back(make_pair(i, column-1));
cout << i << " " << column-1 << endl;
cout << "here" << endl;
}
if (chessboard[i][column].color == NONE)
{
moves.push_back(make_pair(i, column));
cout << i << " " << column << endl;
}
if (i >= 1 && chessboard[i][column].color == NONE && chessboard[i-1][column].color == NONE && row == 6)
{
moves.push_back(make_pair(i-1, column));
cout << i-1 << " " << column << endl;
}
}
return moves;
}
/*
@param i: An int type variable that tells the row at which a chess peice is located
@param j: An int type variable that tells the column at which a chess peice is located
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves in a given position at a given square
*/
vector<pair<int, int> > moves_from_square(int i, int j)
{
vector<pair<int, int> > piece_moves;
Piece peice = chessboard[i][j];
if (peice.color == player_turn && peice.symbol != ' ')
{
if (peice.symbol == 'N')
{
piece_moves = knight_moves(i, j);
}
else if (peice.symbol == 'R')
{
piece_moves = straight_moves(i, j);
}
else if (peice.symbol == 'B')
{
piece_moves = bishop_moves(i, j);
}
else if (peice.symbol == 'Q')
{
piece_moves = queen_moves(i, j);
}
else if (peice.symbol == 'K')
{
piece_moves = king_moves(i, j);
}
else if (peice.symbol == 'P')
{
piece_moves = pawn_moves(i, j);
}
}
return piece_moves;
}
/*
@return: Returns a vector of coordinates, represented by a pair of integers
This function returns all of the moves in a given position for every peice in the position
*/
vector<pair<int, int> > all_moves()
{
vector<pair<int, int> > moves;
Piece peice;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
vector<pair<int, int> > piece_moves = moves_from_square(i, j);
moves.insert(moves.end(), piece_moves.begin(), piece_moves.end());
}
}
return moves;
}
/*
@param move: A pair of ints that dictate the move the player is trying to player or the square that they are moving their peice to
@param startLoc: A pair of ints that dictate the move the player is starting from
This function updates the board's position after a move has been played
*/
void update_board(pair<int, int> move, pair<int, int> startLoc)
{
int row = move.first;
int column = move.second;
int start_row = startLoc.first;
int start_column = startLoc.second;
// Copy the piece at the starting location to the destination location
chessboard[row][column] = chessboard[start_row][start_column];
// Clear the starting location
chessboard[start_row][start_column].color = NONE;
chessboard[start_row][start_column].symbol = ' ';
print_board(chessboard);
player_turn = !player_turn;
}
/*
@param move: A pair of ints that dictate the move the player is trying to player or the square that they are moving their peice to
@param startLoc: A pair of ints that dictate the move the player is starting from
@return bool: true or false to indicate whether the move is valid
This function checks whether a given move as input is valid - used for testing
*/
bool is_valid(pair<int, int> move, pair<int, int> startloc)
{
vector<pair<int, int> > all = moves_from_square(startloc.first, startloc.second);
for (int i = 0; i < all.size(); i++)
{
cout << "first all" << all[i].first << endl;
cout << "second all" << all[i].second << endl;
cout << "first move" << move.first << endl;
cout << "second move" << move.second << endl;
if (all[i].first == move.first && all[i].second == move.second)
{
return true;
}
}
return false;
}
/*
The function that decides the move that the computer is choosing to play
*/
void perform_move()
{
srand(static_cast<unsigned int>(time(nullptr)));
// Determine valid moves based on the piece type
vector<pair<int, int> > valid_moves;
vector<pair<int, int> > valid_starts;
int random_start;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (player_turn == chessboard[i][j].color)
{
valid_starts.push_back(make_pair(i, j));
}
}
}
do
{
random_start = (rand() % valid_starts.size());
valid_moves = moves_from_square(valid_starts[random_start].first, valid_starts[random_start].second);
} while (valid_moves.size() == 0);
// Keep trying random moves until a valid capture move is found
// No valid capture moves, try a new random move
int random_index = rand() % valid_moves.size();
update_board(valid_moves[random_index], valid_starts[random_start]);
//Run program only once the include path has been updated, otherwise the code will not function
//Commented to allow inspection and running in terminal if necessary
/*
std::ofstream file("moveLog.txt", std::ios::app);
if (file.is_open()) {
file << move << std::endl;
file.close();
}*/
// Print the move to the terminal
cout << "Moved piece from (" << valid_starts[random_start].first << ", " << valid_starts[random_start].second << ") to (" << valid_moves[random_index].first << ", " << valid_moves[random_index].second << ")" << endl;
}
//program will run until the game ends --> Will end on the javascript end as the computer doesn't need to perform a move once that occurs
int main()
{
//Run program only once the include path has been updated, otherwise the code will not function
/*
httplib::Server server;
//server side managment
server.Post("/perform_move", [&](const httplib::Request& req, httplib::Response& res) {
const std::string move = req.body;
perform_move();
res.set_content("Move performed successfully", "text/plain");
});
server.listen("localhost", 3000);
*/
return 0;
}
//Code that was used to test the workings of this file
/*
print_board(chessboard);
for (int i = 0; i < 4; i++)
{
int start_row;
int start_column;
int target_row;
int target_column;
cout << "please enter the starting location of the peice" << endl;
cin >> start_row >> start_column;
pair<int, int> save = make_pair(start_row, start_column); //starting location of peice
cout << "please enter row or column into the program (enter as ints)" << endl;
cin >> target_row >> target_column;
while (!is_valid(make_pair(target_row, target_column), save))
{
cout << "please try again" << endl;
cout << "please enter the starting location of the peice" << endl;
cin >> start_row >> start_column;
save = make_pair(start_row, start_column);
cout << "please enter row or column into the program (enter as ints)" << endl;
cin >> target_row >> target_column;
}
update_board(make_pair(target_row, target_column), save);
cout << endl;
//Perform a random move for any living piece
cout << "\nAfter Random Move:\n";
perform_move();
}
*/