-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOthello.cpp
More file actions
572 lines (433 loc) · 12.3 KB
/
Othello.cpp
File metadata and controls
572 lines (433 loc) · 12.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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include "global_functions.h"
#include "Othello.h"
Othello::Othello()
{
//player is white
player_piece = '2';
//AI is black
AI_piece = '1';
tree.player_piece = player_piece;
tree.AI_piece = AI_piece;
resetGame(true);
}
//runs the game
void Othello::run()
{
while(true)
{
// cout<<endl;
// cout<<"Menu: "<<endl;
// cout<<"1) Play against AI"<<endl;
// cout<<"2) AI against AI"<<endl;
// cout<<"3) Print Game Tree"<<endl;
// cout<<"4) Purge Game Tree"<<endl;
// cout<<"Choice: ";
// int choice;
// cin>>choice;
int choice = 1;
cout<<endl<<endl;
//Human vs AI
if(choice==1)
{
tree.piece = AI_piece;
//the player will be at a disadvantage
tree.worse_heuristic_piece = player_piece;
// int depth = 0;
// cout<<"Depth (4 is recommended): ";
// cin>>depth;
int depth = 4;
tree.max_depth = depth;
tree.max_h_depth = depth;
cout<<"New game Human vs AI"<<endl;
resetGame();
//goes forever until someone has an unsuccessful move (board is full or no legal moves)
bool player_move_success = true;
bool AI_move_success = true;
while(player_move_success == true || AI_move_success == true)
{
//player moves
if(isPlayersTurn())
player_move_success = playersMove(0);
//AI moves
else
{
vector<int> move = AIMove(1);
if(move.size()==2)
AI_move_success = true;
else
AI_move_success = false;
// AI_move_success = AIMove(1);
}
//clears tree except for tree.ptr, which becomes new root
tree.eraseParentNodes(tree.ptr);
changeTurn();
}
//determines who won
int player_won = determineWinner();
if(player_won==1)
{
cout<<endl;
cout<<"Player WON"<<endl;
cout<<endl<<endl;
//gives bad reinforcement to tree
tree.reinforceGood(tree.ptr);
}
else if(player_won==-1)
{
cout<<endl;
cout<<"AI WON"<<endl;
cout<<endl<<endl;
//gives good reinforcement to tree
tree.reinforceBad(tree.ptr);
}
else
{
cout<<endl;
cout<<"TIE"<<endl;
cout<<endl<<endl;
}
int num_player_pieces = board_obj.countPieces(tree.ptr->board, player_piece);
int num_AI_pieces = board_obj.countPieces(tree.ptr->board, AI_piece);
cout<<num_player_pieces<<" to "<<num_AI_pieces<<endl;
// cout<<"Moves: "<<endl;
// for(int x = 0; x < moves.size(); x++)
// {
// cout<<moves[x]<<", ";
// if(x%2 != 0)
// cout<<endl;
// }
}
//AI vs AI
else if(choice==2)
{
tree.piece = AI_piece;
//the player will be at a disadvantage
tree.worse_heuristic_piece = player_piece;
int num_games = 0;
cout<<"Num games: ";
cin>>num_games;
int depth = 0;
cout<<"Depth (4 is recommended): ";
cin>>depth;
tree.max_depth = depth;
tree.max_h_depth = depth;
char verbose_choice;
cout<<"Want to print game states as they play? (y/n): ";
cin>>verbose_choice;
bool verbose = false;
if(verbose_choice=='y')
verbose = true;
char reset_tree_choice;
cout<<"Reset tree every time? (y/n): ";
cin>>reset_tree_choice;
bool reset_tree = false;
if(reset_tree_choice=='y')
reset_tree = true;
// bool reset_tree = true;
// //opens move file
// ofstream myfile;
// myfile.open("moves.txt");
cout<<"New game AI vs AI"<<endl;
int total_player_pieces = 0;
int total_AI_pieces = 0;
int total_player_wins = 0;
int total_AI_wins = 0;
for(int x = 0; x < num_games; x++)
{
// cout<<"Resetting game"<<endl;
resetGame(reset_tree);
//goes forever until someone has an unsuccessful move (board is full or no legal moves)
bool player_move_success = true;
bool AI_move_success = true;
while(player_move_success == true || AI_move_success == true)
{
//player moves
if(isPlayersTurn())
player_move_success = playersMove(1);
//AI moves
else
{
vector<int> move = AIMove(1, verbose);
if(move.size()==2)
AI_move_success = true;
else
AI_move_success = false;
// AI_move_success = true
}
//clears tree except for tree.ptr, which becomes new root
tree.eraseParentNodes(tree.ptr);
changeTurn();
}
//prints out moves vector
cout<<"Moves: "<<endl;
// myfile << "Moves: \n";
for(int x = 0; x < moves.size(); x++)
{
cout<<moves[x]<<", ";
// myfile<<moves[x]<<", ";
if(x%2 != 0)
{
cout<<endl;
// myfile<<"\n";
}
}
// myfile<<"\n";
cout<<endl;
cout<<"Game "<<x<<" ";
// myfile<<"Game "<<x<<"\n";
//determines who won
int player_won = determineWinner();
if(player_won==1)
{
total_player_wins++;
cout<<"AI #1 Won ";
//gives bad reinforcement to tree
tree.reinforceGood(tree.ptr);
}
else if(player_won==-1)
{
total_AI_wins++;
cout<<"AI #2 Won ";
//gives good reinforcement to tree
tree.reinforceBad(tree.ptr);
}
else
{
cout<<"TIE ";
//gives good reinforcement to tree
tree.reinforceBad(tree.ptr);
}
int num_player_pieces = board_obj.countPieces(tree.ptr->board, player_piece);
int num_AI_pieces = board_obj.countPieces(tree.ptr->board, AI_piece);
total_player_pieces += num_player_pieces;
total_AI_pieces += num_AI_pieces;
cout<<num_player_pieces<<" to "<<num_AI_pieces<<endl;
// myfile<<num_player_pieces<<" to "<<num_AI_pieces<<"\n\n\n";
// cout<<endl;
// cout<<"End board: "<<endl;
// tree.printNode(tree.ptr);
}
// tree.printNode(tree.ptr);
cout<<endl<<endl;
cout<<"AI #1 wins: "<<total_player_wins<<endl;
cout<<"AI #2 wins: "<<total_AI_wins<<endl;
cout<<"Average AI #1 pieces: "<<((double)total_player_pieces/(double)num_games)<<endl;
cout<<"Average AI #2 pieces: "<<((double)total_AI_pieces/(double)num_games)<<endl;
cout<<endl;
// myfile.close();
}
//Prints the neural nets
else if(choice == 3)
{
tree.printNet(tree.root);
// tree.determinePossibleMoves(&*tree.ptr, AI_piece);
cout<<endl<<endl<<endl;
}
else if(choice == 4)
{
resetGame(true);
}
}
}
//Player's turn to move. returns true if successful
bool Othello::playersMove(int player_type)
{
//also traverse the tree in player so that if AI has to skip its move
//Minimax
tree.iterateTreeDepth(tree.ptr, player_piece, 1, tree.max_depth);
//calculates heuristics
tree.getMaxHeuristic(tree.ptr, tree.ptr, MIN, MAX, tree.max_h_depth+2);
//Negamax
// tree.negamax(tree.ptr, tree.ptr, tree.max_h_depth+2, MIN, MAX, 1);
//print if player is playing, and not AI vs AI
if(player_type == 0)
{
cout<<"Player's move. "<<endl;
tree.printNode(tree.ptr);
}
string choice = "";
int col = -1;
int row = -1;
bool valid_move = false;
while (valid_move == false)
{
//returns array of possible move coordinates, with each index being an array of size 2: (col, row)
vector<vector<int>> possible_moves = board_obj.getPossibleMoveCoordinates(tree.ptr->board, player_piece);
//no possible moves
if(possible_moves.size()==0)
{
//print if player is playing, otherwise it's an AI
if(player_type==0)
cout<<"No legal moves"<<endl;
moves.push_back("-");
return false;
}
//print if player is playing, otherwise it's an AI
if(player_type == 0)
{
cout<<"Legal moves: "<<endl;
for(int x = 0; x < possible_moves.size(); x++)
{
//prints string notation, ex: c3
string notation = convert_to_notation(possible_moves[x][0], possible_moves[x][1]);
cout<<notation<<endl;
// //prints coordinates
// cout<<"("<<possible_moves[x][0]<<", "<<possible_moves[x][1]<<")"<<endl;
}
cout<<endl;
}
//// Comment this if you want the player to move randomly ////
if(player_type==0)
{
cout<<"Where to move? (ex: c3): ";
cin>>choice;
int* coordinates = new int[2];
convert_to_coordinates(choice, coordinates);
// int i = 0;
// int j = 0;
// cout<<"Where to move? (i,j) "<<endl;
// cout<<"i: ";
// cin>>i;
// cout<<"j: ";
// cin>>j;
// int coordinates[2] = {i, j};
//adds to global moves list
choice = convert_to_notation(coordinates[0], coordinates[1]);
moves.push_back(choice);
col = coordinates[0];
row = coordinates[1];
//determines if given move is a possible move
for(int x =0; x < possible_moves.size(); x++)
{
if(possible_moves[x][0]==col && possible_moves[x][1]==row)
{
valid_move=true;
break;
}
}
}
//if AI is playing for player
if(player_type==1)
{
//Get MAX heuristic and move there. But pass in that it's a player requesting it so that it gets the worse heuristic.
//returns child index corresponding with the minimum heuristic
// int child_index = tree.getIndexMinHeuristic(tree.ptr);
int child_index = tree.getIndexMaxHeuristic(tree.ptr);
//add player's move to neural net
tree.move(child_index);
//determines player's move
vector<vector<int>> coordinates = board_obj.getDifferenceCoordinates(tree.ptr->board, tree.ptr->prev->board);
string move = convert_to_notation(coordinates[0][0], coordinates[0][1]);
moves.push_back(move);
//stops the method so that the code after the loop doesn't run, thereby double moving
return true;
}
}
//add player's move to neural net
tree.playerMove(col, row);
//print if player is playing
if(player_type==0)
cout<<endl<<endl<<endl;
return true;
}
//AI's turn to move. returns a vector of the coordinates where AI moved
vector<int> Othello::AIMove(int AI_version, bool verbose)
{
tree.iterateTreeDepth(tree.ptr, AI_piece, 1, tree.max_depth);
tree.getMinHeuristic(tree.ptr, tree.ptr, MIN, MAX, tree.max_h_depth+2);
// tree.negamax(tree.ptr, tree.ptr, tree.max_h_depth+2, MIN, MAX, -1);
//coordinates to return
vector<int> to_return;
//if no boards
if(tree.hasLegalMoves(tree.ptr) == false)
{
moves.push_back("-");
// return false;
return to_return;
}
//returns board corresponding with the minimum heuristic
int child_index = tree.getIndexMinHeuristic(tree.ptr);
//add AI's move to neural net
tree.move(child_index);
//get where AI moved:
vector<vector<int>> coordinates = board_obj.getDifferenceCoordinates(tree.ptr->board, tree.ptr->prev->board);
string move = convert_to_notation(coordinates[0][0], coordinates[0][1]);
moves.push_back(move);
if(verbose == true)
{
cout<<"AI's move: "<<endl;
cout<<"("<<coordinates[0][0]<<", "<<coordinates[0][1]<<")"<<endl;
// cout<<move<<endl;
cout<<endl<<endl;
}
// return true;
to_return.push_back(coordinates[0][0]);
to_return.push_back(coordinates[0][1]);
return to_return;
}
//returns true if player won the game
int Othello::determineWinner()
{
int num_player_pieces = board_obj.countPieces(tree.ptr->board, player_piece);
int num_AI_pieces = board_obj.countPieces(tree.ptr->board, AI_piece);
if(num_player_pieces > num_AI_pieces)
return 1;
else if(num_player_pieces < num_AI_pieces)
return -1;
else
return 0;
}
//returns true if it's player's turn to move.
bool Othello::isPlayersTurn()
{
return turn;
}
//if it was player's turn, it's now AI's turn
void Othello::changeTurn()
{
//flips the boolean
turn = !turn;
}
//resets the game
void Othello::resetGame(bool reset_tree)
{
//AI goes first if turn = false
turn = false;
//resets current game state to root node
tree.ptr = tree.root;
moves.clear();
if(reset_tree)
{
tree.resetTree();
//gets first possible moves for black
tree.determinePossibleMoves(&*tree.ptr, AI_piece);
}
}
//converts a string position, like "d4" into index coordinates, like [4,3]
void Othello::convert_to_coordinates(string position, int * coordinates)
{
int letter = int(position[0]);
int number = int(position[1]); //converts "4" to ascii 52
int row = 0;
int col = number - 48 - 1; //-48 to get actual number from ascii, then -1 to get proper index
//if letter is lowercase
if (letter >= 97)
row = letter-97;
//if letter is uppercase
else
row = letter-65;
coordinates[0] = row;
coordinates[1] = col;
}
//converts a string position, like "d4" into index coordinates, like [4,3]
string Othello::convert_to_notation(int col, int row)
{
char letter = 'a'+col;
char c = '1'+row;
string to_return = string() + letter+c;
return to_return;
}