This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
threes.cpp
101 lines (90 loc) · 2.7 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
#include "action.h"
#include "agent.h"
#include "board.h"
#include "episode.h"
#include "statistic.h"
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
int main(int argc, const char *argv[]) {
// show arguments
std::cout << "Threes-Demo: ";
std::copy(argv, argv + argc,
std::ostream_iterator<const char *>(std::cout, " "));
std::cout << std::endl << std::endl;
// parse arguments
size_t total = 1000, block = 0, limit = 0;
std::string play_args, evil_args;
std::string load, 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("--save=") == 0) {
save = para.substr(para.find('=') + 1);
} else if (para.find("--summary") == 0) {
summary = true;
}
}
statistic stat(total, block, limit);
// load statistic
if (!load.empty()) {
std::ifstream in(load, std::ios::in);
in >> stat;
in.close();
summary |= stat.is_finished();
}
deep_greedy_player play(play_args);
rndenv evil(evil_args);
while (!stat.is_finished()) {
play.open_episode("~:" + evil.name());
evil.open_episode(play.name() + ":~");
stat.open_episode(play.name() + ":" + evil.name());
episode &game = stat.back();
for (size_t i = 0; i < 9; ++i) {
game.take_turns(play, evil);
game.apply_action(evil.init_action(i));
}
unsigned move_;
while (true) {
// std::cout << game.step(-1) << "URDL"[move_] << game.state() <<
// std::endl;
agent &who = game.take_turns(play, evil);
action move = who.take_action(game.state(), move_);
move_ = move.event() & 0b11;
if (!game.apply_action(move)) {
break;
}
if (who.check_for_win(game.state())) {
break;
}
}
agent &win = game.last_turns(play, evil);
stat.close_episode(win.name());
play.close_episode(win.name());
evil.close_episode(win.name());
}
// show statistic
if (summary) {
stat.summary();
}
// save statistic
if (!save.empty()) {
std::ofstream out(save, std::ios::out | std::ios::trunc);
out << stat;
out.close();
}
return 0;
}