-
Notifications
You must be signed in to change notification settings - Fork 0
/
utttbot.cpp
113 lines (103 loc) · 2.74 KB
/
utttbot.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
// utttbot.cpp
// Jeffrey Drost
#include "utttbot.h"
#include <iostream>
#include <sstream>
#include <chrono>
void UTTTBot::run() {
std::string line;
while (std::getline(std::cin, line)) input(line);
}
void UTTTBot::move(int timeout) {
if(firstMove){
firstMove = false;
Move r = Move{4,4};
std::cout << "place_disc " << r << std::endl;
}else {
Move m = UTTTAI::findBestMove(state, timeout, time_per_move);
std::cout << "place_disc " << m << std::endl;
}
}
void UTTTBot::update(std::string &key, std::string &value) {
if (key == "round") {
round = std::stoi(value);
} else if (key == "field") {
int row = 0;
int col = 0;
std::vector<std::string> fields = split(value, ',');
for (std::string &field : fields) {
if (field == "0") {
state.board[row][col] = Player::X;
} else if (field == "1") {
state.board[row][col] = Player::O;
} else {
state.board[row][col] = Player::None;
}
col++;
if (col == 9) {
row++;
col = 0;
}
}
} else if (key == "macroboard") {
int row = 0;
int col = 0;
std::vector<std::string> fields = split(value, ',');
for (std::string &field : fields) {
if (field == "-1") {
state.macroboard[row][col] = Player::Active;
} else if (field == "0") {
state.macroboard[row][col] = Player::X;
} else if (field == "1") {
state.macroboard[row][col] = Player::O;
} else {
state.macroboard[row][col] = Player::None;
}
col++;
if (col == 3) {
row++;
col = 0;
}
}
}
}
void UTTTBot::setting(std::string &key, std::string &value) {
if (key == "timebank") {
timebank = std::stoi(value);
} else if (key == "time_per_move") {
time_per_move = std::stoi(value);
} else if (key == "player_names") {
std::vector<std::string> names = split(value, ',');
player_names[0] = names[0];
player_names[1] = names[1];
} else if (key == "your_bot") {
if(value == player_names[0]){
firstMove = true;
}
your_bot = value;
} else if (key == "your_botid") {
your_botid = std::stoi(value);
}
}
std::vector<std::string> UTTTBot::split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
void UTTTBot::input(std::basic_string<char> & line)
{
std::vector<std::string> command = split(line, ' ');
if (command[0] == "settings") {
setting(command[1], command[2]);
} else if (command[0] == "update" && command[1] == "game") {
update(command[2], command[3]);
} else if (command[0] == "action" && command[1] == "move") {
move(std::stoi(command[2]));
} else {
std::cerr << "Unknown command: " << line << std::endl;
}
}