-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.cpp
92 lines (80 loc) · 2.42 KB
/
2048.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
/**
* Framework for 2048 & 2048-like Games (C++ 11)
* 2048.cpp: Main file for the 2048 framework
*
* Author: Theory of Computer Games (TCG 2021)
* Computer Games and Intelligence (CGI) Lab, NYCU, Taiwan
* https://cgilab.nctu.edu.tw/
*/
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include "board.h"
#include "action.h"
#include "agent.h"
#include "episode.h"
#include "statistic.h"
int main(int argc, const char* argv[]) {
std::cout << "2048-Demo: ";
std::copy(argv, argv + argc, std::ostream_iterator<const char*>(std::cout, " "));
std::cout << std::endl << std::endl;
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);
if (load.size()) {
std::ifstream in(load, std::ios::in);
in >> stat;
in.close();
summary |= stat.is_finished();
}
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();
while (true) {
agent& who = game.take_turns(play, evil);
action move = who.take_action(game.state());
if (game.apply_action(move) != true) 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());
}
if (summary) {
stat.summary();
}
if (save.size()) {
std::ofstream out(save, std::ios::out | std::ios::trunc);
out << stat;
out.close();
}
return 0;
}