-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_mcts2.cpp
827 lines (683 loc) · 33.2 KB
/
go_mcts2.cpp
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*******************************************
* *
* Programmer: Nikola Andric *
* Email: nikolazeljkoandric@gmail.com *
* Last Eddited: 3. Dec. 2021 *
* *
*******************************************/
#include<iostream>
#include<vector>
#include<array>
#include<algorithm> // for copy() and copy_n()
#include<time.h>
#include <cmath>
#include <bits/stdc++.h>
#include <typeinfo>
#include <ctime>
#include <time.h>
#include <memory>
using namespace std;
//creating my own namespace
namespace NAndric{
/*
* PRECONDITION: Given a 2D array.
* POSTCONDITION: Printing the 2D array so it looks like a real board for game of go.
* DESCRIPTION: Printing the board with indices on left side and on the top. The values are actually index + 1.
*/
void display(int board[15][15]){
//Printing the indices on the top of the grid
cout<<" ";
for(int i = 0; i <= 14; i++){
if(i <= 9){
cout <<"\033[1;36m"<< i <<"\033[0m" << " ";
}else{
cout<< "\033[1;36m"<< i <<"\033[0m" << " ";
}
}
cout<<endl<<endl;
///Printing the grid
//cout<<endl<<"---------------------------------------------------------------------------------"<<endl;
cout<<endl<<" | | | | | | | | | | | | | | | "<<endl;
for(int i=0; i <= 14; i++){
if(i < 10){
cout<<"\033[1;36m"<< i <<"\033[0m"<<" --";
}else{
cout<<"\033[1;36m"<< i <<"\033[0m"<<" --";
}
for(int j = 0; j <= 14; j++){
// about changing colors: https://www.tutorialspoint.com/how-to-output-colored-text-to-a-linux-terminal
//if it is a free spot then let it be yellow
//if pebble 2 then red
//if pebble 1 then green
if(board[i][j]==0)
cout<<"\033[1;33m"<< board[i][j] <<"\033[0m";
else if(board[i][j] == 2)
cout<<"\033[7;37m"<< board[i][j] <<"\033[0m";
else if (board[i][j] == 1)
cout<<"\033[7;32m"<< board[i][j] <<"\033[0m";
cout << "----";
}
cout<<endl<<" | | | | | | | | | | | | | | | "<<endl;
}
}
/**
* PRECONDITION: Given a 2D array and the position of the last played pebble.
* POSTCONDITION: Returning if there is a winner in the game.
* DESCRIPTION: Checking columns, diagonals, and rows to see if there is 5 pebbles in the row of the same collor starting from
* the position of the last pebble placed on the board
*/
int checkBoardStatus(int board[15][15], int last_position[2]){
int x = last_position[0];
int y = last_position[1];
if(last_position[0] < 0 || last_position[0]>14 || last_position[1]<0 ||last_position[1]>14)
return 0;
//Checking direction LEFT
int temp_y = y; //from this position we are looking for 5 consecutive same values
int count = 0; //count how many consecutive pebbles there are in this direction
bool winner = false; //keep track if there is a winning state
//cout<<"Checking direction LEFT"<<endl;
while(temp_y>=0 && board[x][temp_y] == board[x][y] ){
//cout<<"checking position "<< x<<" "<< temp_y <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[x][temp_y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
winner = true;
break;
}
//move one spot left
temp_y--;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction RIGHT
temp_y = y; //from this position we are looking for 5 consecutive same values
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction RIGHT"<<endl;
while(temp_y<=14 && board[x][temp_y] == board[x][y] ){
//cout<<"checking position "<< x<<" "<< temp_y <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[x][temp_y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
winner = true;
break;
}
//move one spot left
temp_y++;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction UP
int temp_x = x; //from this position we are looking for 5 consecutive same values
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction UP"<<endl;
while(temp_x>=0 && board[temp_x][y] == board[x][y] ){
//cout<<"checking position "<< temp_x<<" "<< y << " where x is "<< board[temp_x][y] <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[temp_x][y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
//cout<<"coonut is 5"<<endl;
winner = true;
break;
}
//move one spot left
temp_x--;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction DOWN
temp_x = x; //from this position we are looking for 5 consecutive same values
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction DOWN"<<endl;
while(temp_x<=14 && board[temp_x][y] == board[x][y] ){
// cout<<"checking position "<< temp_x<<" "<< y << " where x is "<< board[temp_x][y] <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[temp_x][y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
//cout<<"coonut is 5"<<endl;
winner = true;
break;
}
//move one spot down
temp_x++;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction Diagonal UP - LEFT
temp_x = x; //from this position we are looking for 5 consecutive same values
temp_y = y;
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction DIAGONAL UP - LEFT"<<endl;
while(temp_x>=0 && temp_y>=0 && board[temp_x][temp_y] == board[x][y] ){
//cout<<"checking position "<< temp_x<<" "<< temp_y <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[temp_x][temp_y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
//cout<<"coonut is 5"<<endl;
winner = true;
break;
}
//move one spot left
temp_x--;
temp_y--;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction Diagonal UP - RIGHT
temp_x = x; //from this position we are looking for 5 consecutive same values
temp_y = y;
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction DIAGONAL UP - RIGHT"<<endl;
while(temp_x>=0 && temp_y<=14 && board[temp_x][temp_y] == board[x][y] ){
//cout<<"checking position "<< temp_x<<" "<< temp_y <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[temp_x][temp_y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
//cout<<"coonut is 5"<<endl;
winner = true;
break;
}
//move one spot left
temp_x--;
temp_y++;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction Diagonal DOWN - LEFT
temp_x = x; //from this position we are looking for 5 consecutive same values
temp_y = y;
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction DIAGONAL DOWN - LEFT"<<endl;
while(temp_x<=14 && temp_y>=0 && board[temp_x][temp_y] == board[x][y] ){
//cout<<"checking position "<< temp_x<<" "<< temp_y <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[temp_x][temp_y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
//cout<<"coonut is 5"<<endl;
winner = true;
break;
}
//move one spot left
temp_x++;
temp_y--;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
//Checking direction Diagonal DOWN - RIGHT
temp_x = x; //from this position we are looking for 5 consecutive same values
temp_y = y;
count = 0; //count how many consecutive pebbles there are in this direction
winner = false; //keep track if there is a winning state
//cout<<"Checking direction DIAGONAL DOWN - RIGHT"<<endl;
while(temp_x<=14 && temp_y<=14 && board[temp_x][temp_y] == board[x][y] ){
//cout<<"checking position "<< temp_x<<" "<< temp_y <<endl;
//if the next spot on the board is same as the original one, increment the count
if (board[temp_x][temp_y] == board[x][y]){
count++;
}
//check if the winning state is achieved
if(count == 5){
//cout<<"coonut is 5"<<endl;
winner = true;
break;
}
//move one spot left
temp_x++;
temp_y++;
}
//return the value of pebble if the winning state is achieved
if(winner == true){
//cout<<"returning "<<board[x][y]<<endl;
return board[x][y];
}
return 0;
}
/**
* PRECONDITION: Given a 2D array, the color we are playing for, and position we need to change on the board.
* POSTCONDITION: Changed values on the board
* DESCRIPTION: Changing the values on hte board. If it is a black player, it will be represented by 1.
* The white player will be represented by 2. Empty spots remain 0's.
*/
void play(int board[15][15], int color, int position[2]){
//Mark the given position on the board with 1 or 2.
board[position[0]][position[1]] = (color == 1) ? 1 : 2;
}
/**
* DESCTIPTION: Class that represents a node in the Monte Carlo Search Tree.
* - Total simulation reward is an attribute of a node v and in a simplest
* form is a sum of simulation results that passed through considered node.
* - Total number of visits is another attribute of a node v representing a counter of how many times
* a node has been on the backpropagation path (and so how many times it contributed to the total
* simulation reward)
*
* In other words, if you look at random node’s statistics these two values will reflect how promising
* the node is (total simulation reward) and how intensively explored it has been (number of visits).
* Nodes with high reward are good candidates to follow (exploitation) but those with low amount of visits may
* be interesting too (because they are not explored well)
*/
class board_state_node{
private:
// int game_state[15][15]; //State of the board
// int** game_state = new int*[15];
// int pebble_color = 0;
double total_simulation_reward = 0;
int total_num_visits = 0;
shared_ptr<board_state_node> parent= NULL;
vector<shared_ptr<board_state_node>> children; //list of children (list of shared vectors)
public:
int game_state[15][15]={{0,0,0},{0,0,0},{0,0,0}}; //State of the board
int last_position_played[2]={-1,-1}; //x and y coordinates of the last position played
//getters
vector<shared_ptr<board_state_node>>& get_children(){return children;}
int get_visits(){return total_num_visits;}
double get_reward(){return total_simulation_reward;}
shared_ptr<board_state_node> &get_parent(){return parent;}
// int get_pebble_color(){return pebble_color;}
// int** get_state(){return game_state;}
//setters
void set_visits(int num_visits){total_num_visits = num_visits;}
void set_reward(double value){total_simulation_reward = value;}
void push_back_child(shared_ptr<board_state_node> child){
children.push_back(child);
}
void set_last_position(int position[2]){
last_position_played[0] = position[0];
last_position_played[1]=position[1];
}
//default constructor
board_state_node(){
// for(int i = 0; i < 15; i++)
// game_state[i] = new int[15];
//fill out the grid with zeros
for(int i = 0; i < 15; i++){
for(int k = 0; k < 15; k++){
game_state[i][k] = 0;
}
}
}
//constructor
board_state_node(int game_board[15][15], shared_ptr<board_state_node> predecessor){
// copy the passed board state to the blank board state.
copy(&game_board[0][0],&game_board[0][0]+15*15,&game_state[0][0]);
parent = predecessor;
// pebble_color = color;
}
//destructor;
virtual ~board_state_node(){
// for(vector<board_state_node*>::iterator it = children.begin(); it != children.end();it++){
// delete *it;
// }
// //delete the game state pointers
// for(int i = 0 ; i < 15; i++)
// delete[] game_state[i];
// delete[] game_state;
}
//function that checks if a node has parent or not
bool has_parent(){
return parent != NULL;
}
//check if node has children (leaf or not)
bool has_children(){
return !children.empty();
}
void print_node(){
display(game_state);
}
};
class MonteCarloTree{
private:
shared_ptr<board_state_node> root;
shared_ptr<board_state_node> current;
int pebble_color;
bool user_turn = true;
public:
MonteCarloTree(int state[15][15], int color){
root = make_shared<board_state_node>(board_state_node(state, NULL));
current = root;
pebble_color = color;
}
//getters
shared_ptr<board_state_node> get_root(){return root;}
shared_ptr<board_state_node> get_current(){return current;}
void print_current_state(){
display(current->game_state);
}
// //Create children of the node for every possible move
void create_children(shared_ptr<board_state_node> node){
for(int i = 0; i < 15; i++){
for(int k =0; k<15 ; k++){
//if the position on the board is empty then add a child
if(node->game_state[i][k] == 0){
int new_state[15][15];
//perform a deep copy to a new state
copy(&node->game_state[0][0],&node->game_state[0][0]+15*15,&new_state[0][0]);
//place new number to that state based on what player you are
new_state[i][k] = pebble_color;
//create node with that state
shared_ptr<board_state_node> parent = node; //current node is going to be the parent
// board_state_node *point_to_child = new board_state_node(new_state, parent);
shared_ptr<board_state_node> point_to_child = make_shared<board_state_node>(board_state_node(new_state, parent)) ;
//remember the position played
int last_pos[2]={i,k};
point_to_child->set_last_position(last_pos);
//add the node to the children list
node->push_back_child(point_to_child);
}
}
}
}
//function according to which the next move is chosen
//randomly picking a child
shared_ptr<board_state_node> rollout_policy(shared_ptr<board_state_node> node){
return node->get_children().at(rand()%node->get_children().size());
}
// picking what child we are going to explore based on child's Upper Confidence Bound (UCB)
//node that is being passed is the initial state
shared_ptr<board_state_node> traverse(shared_ptr<board_state_node> node){
//if there are children, calculate UCB for each child and choose the child with maximum UCB
//we keep doing this until we hit the leaf node that has no children
while(node->get_children().size() > 0){
double best_UCB = 0;
double UCB = 0;
shared_ptr<board_state_node> best_node = node;
//calculate UCB for every child and remember the best one
for(int i = 0; i < node->get_children().size(); i++){
//if child has been visited before, calculate UCB using formula
//otherwise UCB is 1.
shared_ptr<board_state_node> child = node->get_children().at(i);
if(child->get_visits() != 0)
UCB = child->get_reward()/child->get_visits() + sqrt(2) * sqrt(log(child->get_visits())/child->get_visits());
else
UCB = 1;
//update if a better UCB is found
if(UCB >= best_UCB){//****************************************************************************************************************************zbog ovog = znaka uzima poslednjeg u donjem desnom cosku
best_UCB = UCB;
best_node = child;
}
}
//we are going deeper by setting the node to be the child with the best UCB
node = best_node;
}
//at this point we got the to a leaf node and we need to expand
if(!node->has_children()){
//if the node has never been sampled/visited, then simply rollout from there.
//otherwise, for each available action, add a new state to the tree
if(node->get_visits() == 0)
return node;
else{
//check if the game is ended
if(checkBoardStatus(node->game_state, node->last_position_played) != 0){
return node;
}
//create children for this node
create_children(node);
//randomly choose a child for the rollout
node = rollout_policy(node);
// turn = !turn;
return node;
}
}
}
//function that is going to show the result of the simulation
int rollout(shared_ptr<board_state_node> node){
// board_state_node temp_node = board_state_node(node->game_state,NULL, node->pebble_color);
//keep temporary shared pointer
shared_ptr<board_state_node> point_to_temp_node = make_shared<board_state_node>(board_state_node(node->game_state,NULL));
//Loop until the game has a winner
while(checkBoardStatus(point_to_temp_node->game_state, point_to_temp_node->last_position_played) < 1){
//if node does not have children, then create children for that node and dig deeper into the tree
if(point_to_temp_node->get_children().size() == 0)
create_children(point_to_temp_node);
//pick a random child from the created children to go deeper.
point_to_temp_node = rollout_policy(point_to_temp_node);
}
//returning the winning pebble or 0 if there is no winner
return checkBoardStatus(point_to_temp_node->game_state, point_to_temp_node->last_position_played);
}
//returning the child with the most visits
shared_ptr<board_state_node> best_child(shared_ptr<board_state_node> node){
int most_visits = 0; //keeping trach of the most visits any child has
shared_ptr<board_state_node> the_best_child = NULL; // keeping track of the child with the most visits
// iterating over each child to see which one has the most visits
for(int i =0; i<node->get_children().size(); i++){
// cout<<i<<endl;
if(node->get_children().at(i)->get_visits() > most_visits){
most_visits = node->get_children().at(i)->get_visits();
the_best_child = node->get_children().at(i);
}
}
return the_best_child;
}
//backpropagation function
void backpropagate(shared_ptr<board_state_node> node, double result){
//update the score and the number of visits on each node on this path
node->set_reward(node->get_reward() + result );
node->set_visits(node->get_visits()+1);
//if we hit the root that does not have parent, then we stop.
if(!node->get_parent())
return;
//backpropagate up the tree
backpropagate(node->get_parent(), -result);
}
// Monte Carlo Tree Search Function
shared_ptr<board_state_node> monte_carlo_tree_search(int state[15][15]){
//3 seconds should be limitation
time_t start = time(0);
int iter = 0;
while(difftime(time(0), start) < 3){
iter++;
// for(int i = 0; i < 100; i++){
// Traverse to the bottom of the tree and expand the leaf node if has not been visited
shared_ptr<board_state_node> leaf = traverse(current);
// play a simulation from that node.
int simulation_result = rollout(leaf);
// cout<<"yes 3"<<endl;
backpropagate(leaf,simulation_result);
}
current = best_child(current);
return current;
}
};
/*
* PRECONDITION: Given a 2D array, the color we are playing for, and position array that is going to hold the best position to make a move for the given color.
* POSTCONDITION: Found the best position to place the stone.
* DESCRIPTION: Using Monte Carlo Tree Search to find hte best possible position to place the stone of the given color player, and returning the intiger array that
* represents the best position.
*/
void search_old(int board[15][15], int color, int position[2]){
//initialize root as current board state
board_state_node root = board_state_node(board, NULL);
//Use Monte Carlo Tree Search on this node
MonteCarloTree the_tree = MonteCarloTree(root.game_state, color);
//get the best child
shared_ptr<board_state_node> BEST = the_tree.monte_carlo_tree_search(root.game_state);
// display(BEST->game_state);
cout<<" ";
cout<<"\033[1;31m"<<"The position chosen by AI is: "<<BEST->last_position_played[0]<<" "<<BEST->last_position_played[1]<< "\033[0m"<<endl<<endl;
//update the position parameter with the last positoin chosen by AI
position[0] = BEST->last_position_played[0];
position[1] = BEST->last_position_played[1];
return;
}
void search(int board[15][15], int color, int position[2]){
search_old(board, color+1, position);
}
void human_vs_AI(){
int user_pebble, AI_pebble;
//ask user for the color of pebble he/she wants to use
cout<<"NOTE: Green player plays first!"<<endl<<endl;
while(true){
try{
cout<<"Please enter the number for the color you want to play with:"<<endl<<"1 - green"<<endl<<"2 - white"<<endl<<"Your number is: ";
cin >> user_pebble;
if(user_pebble == 1 || user_pebble == 2)
break;
else
throw(user_pebble);
}
catch (int user_pebbleee){
cout<<"NOTE: When you choose color of the player you can only enter values 1 or 2. Try again!"<<endl<<endl ;
}
}
//get AI pebble
if(user_pebble == 1)
AI_pebble = 2;
else
AI_pebble = 1;
//initial state
int state[15][15] ={{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,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,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,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,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,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,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,0}};
//human will have the value 2, AI will play with value 1.
MonteCarloTree my_tree = MonteCarloTree(state,0);
// bool game_ended = false; //used to see if we have winner or not or if game just ended with draw.
int row, col; // row and column that human will play
int AI_position[2]= {-1,-1};
int user_last_position[2]= {-1,-1};
//play the game until there is a winner or draw;
while(true){
//if user has a white pebble then AI plays first
if(user_pebble == 2){
//let AI play and save the best its choice in the AI_position list
search(state,AI_pebble,AI_position);
//play with those positions
play(state, AI_pebble, AI_position);
}
//check if there is a winner
//Check if the game is over!
if(checkBoardStatus(state, AI_position)== AI_pebble){
display(state);
cout<<"\n\n The winner is AI !\n\n"<<endl<<endl;
break;
}
//User's turn to play
while(true){
display(state);
cout<<endl<<endl<<"Make your move!"<<endl<<endl;
//Ask user for the row and column values until the user inputs valid values.
while(true){
try{
cout<<"Enter row [0-14]: ";
cin >> row;
if(row >= 0 && row <= 14)
break;
else
throw(row);
}
catch (int row_num){
cout<<"NOTE: The row value should be in range 0-14 inclusive.";
}
}
while(true){
try{
cout<<"Enter column [0-14]: ";
cin >> col;
if(col >= 0 && col <= 14)
break;
else
throw(col);
}
catch (int col_num){
cout<<"NOTE: The column value should be in range 0-14 inclusive.";
}
}
//if the position desired is free, place user pebble on that position.
int human_position[2] = {row,col};
if (state[row][col] == 0){
play(state,user_pebble,human_position);
break;
}else{
cout<<"The position is already taken! Try again!"<<endl;
}
}
//After the user played, check if he/she is the winner!
user_last_position[0]=row;
user_last_position[1]=col;
if (checkBoardStatus(state,user_last_position) == user_pebble){
display(state);
cout<<"\n\n The winner is human !\n\n"<<endl<<endl;
break;
}
cout<<endl<<"Let AI think for 3 seconds..."<<endl<<endl;
//if user has a black pebble then AI plays second
if(user_pebble == 1){
//let AI play and save the best its choice in the AI_position list
search(state,AI_pebble,AI_position);
//play with those positions
play(state, AI_pebble, AI_position);
}
//Check if the game is over!
if(checkBoardStatus(state, AI_position)== AI_pebble){
display(state);
cout<<"\n\n The winner is AI !\n\n"<<endl<<endl;
break;
}
}
return;
}
}
int main(){
//seeding the random number generator
srand(time(0));
NAndric::human_vs_AI();
return 0;
}