-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threes.cpp
254 lines (218 loc) · 9.14 KB
/
Threes.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
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <regex>
#include <memory>
#include "Agent.h"
#include "Board64.h"
#include "Action.h"
#include "Episode.h"
#include "Statistic.h"
#include "arena.h"
#include "io.h"
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 0 1 2 3 6 12 24 48 92 192 384 768 1536 3072 6144
int shell(int argc, const char *argv[]) {
arena host("anonymous");
for (int i = 1; i < argc; i++) {
std::string para(argv[i]);
if (para.find("--name=") == 0 || para.find("--account=") == 0) {
host.set_account(para.substr(para.find("=") + 1));
} else if (para.find("--login=") == 0) {
host.set_login(para.substr(para.find("=") + 1));
} else if (para.find("--save=") == 0 || para.find("--dump=") == 0) {
host.set_dump_file(para.substr(para.find("=") + 1));
} else if (para.find("--play") == 0) {
std::cout << "PLAYER ENTER" << std::endl;
std::shared_ptr<Agent> play(new TdLambdaPlayer(para.substr(para.find("=") + 1)));
host.register_agent(play);
std::cout << "PLAYER REGISTERED" << std::endl;
} else if (para.find("--evil") == 0) {
std::cout << "EVIL ENTER" << std::endl;
std::shared_ptr<Agent> evil(new DareDevil(para.substr(para.find("=") + 1)));
host.register_agent(evil);
std::cout << "EVIL REGISTERED" << std::endl;
}
}
std::cout << "LOADED SETTING" << std::endl;
std::regex match_move("^#\\S+ \\S+$"); // e.g. "#M0001 ?", "#M0001 #U"
std::regex match_ctrl("^#\\S+ \\S+ \\S+$"); // e.g. "#M0001 open Slider:Placer", "#M0001 close score=15424"
std::regex arena_ctrl("^[@$].+$"); // e.g. "@ login", "@ error the account "Name" has already been taken"
std::regex arena_info("^[?%].+$"); // e.g. "? message from anonymous: 2048!!!"
for (std::string command; input() >> command;) {
try {
if (std::regex_match(command, match_move)) {
std::string id, move;
std::stringstream(command) >> id >> move;
if (move == "?") {
// your agent need to take an action
Action a = host.at(id).take_action();
host.at(id).ApplyAction(a);
if (a.type() == Action::Place::type_) {
int hint = Action::Place(a).hint(); // your hint tile here
output() << id << ' ' << a << '+' << std::min(4, hint) << std::endl;
} else {
output() << id << ' ' << a << std::endl;
}
} else {
// perform your opponent's action
Action a;
std::stringstream(move) >> a;
int hint = 0;
if (a.type() == Action::Place::type_) {
hint = move[3] - '0'; // move should be "PT+H" where H is '1', '2', '3', or '4'
Action::Place placing_action(Action::Place(a).position(),Action::Place(a).tile(), hint);
Agent::last_move_code = unsigned(placing_action);
} else {
Agent::last_move_code = unsigned(a);
}
host.at(id).ApplyAction(a); // you should pass the hint tile to your player
}
} else if (std::regex_match(command, match_ctrl)) {
std::string id, ctrl, tag;
std::stringstream(command) >> id >> ctrl >> tag;
if (ctrl == "open") {
// a new match is pending
if (host.open(id, tag)) {
output() << id << " open accept" << std::endl;
} else {
output() << id << " open reject" << std::endl;
}
} else if (ctrl == "close") {
// a match is finished
host.close(id, tag);
}
} else if (std::regex_match(command, arena_ctrl)) {
std::string ctrl;
std::stringstream(command).ignore(1) >> ctrl;
if (ctrl == "login") {
// register yourself and your agents
std::stringstream agents;
for (auto who : host.list_agents()) {
agents << " " << who->name() << "(" << who->role() << ")";
}
output("@ ") << "login " << host.login() << agents.str() << std::endl;
} else if (ctrl == "status") {
// display current local status
info() << "+++++ status +++++" << std::endl;
info() << "login: " << host.account();
for (auto who : host.list_agents()) {
info() << " " << who->name() << "(" << who->role() << ")";
}
info() << std::endl;
info() << "match: " << host.list_matches().size() << std::endl;
for (auto ep : host.list_matches()) {
info() << ep->name() << " " << (*ep) << std::endl;
}
info() << "----- status -----" << std::endl;
} else if (ctrl == "error" || ctrl == "exit") {
// some error messages or exit command
std::string message = command.substr(command.find_first_not_of("@$ "));
info() << message << std::endl;
break;
}
} else if (std::regex_match(command, arena_info)) {
// message from arena server
}
} catch (std::exception &ex) {
std::string message = std::string(typeid(ex).name()) + ": " + ex.what();
message = message.substr(0, message.find_first_of("\r\n"));
output("? ") << "exception " << message << " at \"" << command << "\"" << std::endl;
}
}
return 0;
}
int main(int argc, const char *argv[]) {
InitLookUpTables();
std::cout << "Threes-Demo: ";
std::copy(argv, argv + argc, std::ostream_iterator<const char *>(std::cout, " "));
std::cout << std::endl << std::endl;
size_t total = 1000;
size_t block = 0;
size_t limit = 0;
bool learning = false;
std::string play_args;
std::string evil_args;
std::string load;
std::string save;
bool summary = false;
for (int i = 1; i < argc; i++) {
std::string para(argv[i]);
if (para.find("--total=") == 0) {
total = std::stoull(para.substr(para.find("=") + 1));
} else if (para.find("--block=") == 0) {
block = std::stoull(para.substr(para.find("=") + 1));
} else if (para.find("--limit=") == 0) {
limit = std::stoull(para.substr(para.find("=") + 1));
} else if (para.find("--play=") == 0) {
play_args = para.substr(para.find("=") + 1);
} else if (para.find("--evil=") == 0) {
evil_args = para.substr(para.find("=") + 1);
} else if (para.find("--load=") == 0) {
load = para.substr(para.find("=") + 1);
} else if (para.find("--learn") == 0) {
learning = true;
} else if (para.find("--save=") == 0) {
std::string s = para.substr(para.find("=") + 1);
if (s == "epoch") {
save = "../results/" + std::to_string(std::time(nullptr));
} else {
save = s;
}
} else if (para.find("--summary") == 0) {
summary = true;
} else if (para.find("--shell") == 0) {
return shell(argc, argv);
}
}
Statistic stat(total, block, limit);
if (load.size()) {
std::ifstream in(load, std::ios::in);
in >> stat;
in.close();
summary |= stat.IsFinished();
}
Player player(play_args);
DareDevil evil(evil_args);
while (!stat.IsFinished()) {
player.OpenEpisode("~:" + evil.name());
evil.OpenEpisode(player.name() + ":~");
stat.OpenEpisode(player.name() + ":" + evil.name());
Episode &game = stat.Back();
while (true) {
Agent &agent = game.TakeTurns(player, evil);
Action move = agent.TakeAction(game.state());
if (!game.ApplyAction(move)) {
break;
}
if (agent.CheckForWin(game.state())) {
break;
}
}
Agent &win = game.TakeLastTurns(player, evil);
stat.CloseEpisode(win.name());
// if (learning) {
// player.Learn(game);
//
// if (stat.IsBackup()) {
// player.save();
// }
//
// if (stat.NGames() >= total / 2) {
// player.decreaseLearningRate10Times();
// }
// }
player.CloseEpisode(win.name());
evil.CloseEpisode(win.name());
}
if (summary) {
stat.Summary();
}
if (save.size()) {
std::ofstream out(save, std::ios::out | std::ios::trunc);
out << stat;
out.close();
}
return 0;
}