-
Notifications
You must be signed in to change notification settings - Fork 11
/
player.cpp
121 lines (91 loc) · 2.88 KB
/
player.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
#include "player.h"
#include <iostream>
#include <cassert>
#include <ctime>
Player::Player(const std::string &name,Token player) : name(name), player(player) {
assert(not player==NOT_PLAYED);
}
Token Player::get_player() const {
return player;
}
void Player::print() const {
std::cout<<name<<" "<<player;
}
void PlayerBot::print() const {
Player::print();
std::cout<<" max_sec="<<max_sec<<" max_iteration="<<max_iteration<<" uct_constant="<<root->get_uct_constant();
}
PlayerBot::PlayerBot(Token player,double max_sec,int max_iteration,Value uct_constant) : Player("bot",player), max_sec(max_sec),max_iteration(max_iteration),root(new Node(uct_constant)) {}
Move *PlayerBot::get_move(const Board *board, const Move * last_move) {
//std::cout<<"playing enemy move"<<std::endl;
//root->print_tree();
//reuse last simulations if possibles
if (last_move) root=root->advance_and_detach(last_move);
Count saved_simulations=root->get_nb();
//std::cout<<"before simulations"<<std::endl;
//root->print_tree();
clock_t start=clock(),end=clock();
int k;
for (k=0; (k<max_iteration or not max_iteration) and root->get_mode()==NORMAL and end-start<max_sec*CLOCKS_PER_SEC; k++) {
Board *copy=board->deepcopy();
Token winner=root->play_random_game(copy,player);
delete copy;
end=clock();
}
const Node *best_child=root->get_best_child();
if (not best_child) return nullptr;
const Move *move=best_child->get_move();
//debug report
//std::cout<<"after simulations"<<std::endl;
//root->print_tree();
//root->print_best_branch_down();
//std::cout<<std::endl;
//simulation report
std::cout<<"simulated "<<k<<" games ("<<saved_simulations<<" saved) in "<<float(end-start)/CLOCKS_PER_SEC<<"s"<<std::endl;
//root->print_tree(0,2);
//move report
std::cout<<"playing ";
switch (root->get_mode()) {
case NORMAL:
std::cout<<"normal "<<best_child->get_winning_probability()<<" ";
break;
case WINNER:
std::cout<<"loosing ";
break;
case LOOSER:
std::cout<<"winning ";
break;
}
std::cout<<"move ";
move->print();
std::cout<<std::endl;
//play best_move
root=root->advance_and_detach(move);
//std::cout<<"after playing best_move"<<std::endl;
//root->print_tree();
Move *copy=move->deepcopy();
return copy;
}
PlayerBot::~PlayerBot() {
delete root;
}
PlayerHuman::PlayerHuman(Token player) : Player("human",player) {}
Move *PlayerHuman::get_move(const Board *board,const Move * last_move) {
Move *move= nullptr;
while (not move) {
std::cout<<"choose move: ";
char string[MAX_INPUT_LENGTH];
std::cin.getline(string,MAX_INPUT_LENGTH);
if (not std::cin.good()) {
std::cout<<std::endl;
break;
}
move=board->parse_move_string(player,string);
}
//move report
//if (not move) return NULL;
//std::cout<<"playing move ";
//move->print();
//std::cout<<std::endl;
return move;
}