-
Notifications
You must be signed in to change notification settings - Fork 1
/
2048.cpp
95 lines (84 loc) · 2.63 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
93
94
95
/**
* Framework for 2048 & 2048-Like Games (C++ 11)
* 2048.cpp: Main file for the 2048 framework
*
* Author: Hung Guei
* 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 "statistics.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 slide_args, place_args;
std::string load_path, save_path;
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
auto match_arg = [&](std::string flag) -> bool {
auto it = arg.find_first_not_of('-');
return arg.find(flag, it) == it;
};
auto next_opt = [&]() -> std::string {
auto it = arg.find('=') + 1;
return it ? arg.substr(it) : argv[++i];
};
if (match_arg("total")) {
total = std::stoull(next_opt());
} else if (match_arg("block")) {
block = std::stoull(next_opt());
} else if (match_arg("limit")) {
limit = std::stoull(next_opt());
} else if (match_arg("slide") || match_arg("play")) {
slide_args = next_opt();
} else if (match_arg("place") || match_arg("env")) {
place_args = next_opt();
} else if (match_arg("load")) {
load_path = next_opt();
} else if (match_arg("save")) {
save_path = next_opt();
}
}
statistics stats(total, block, limit);
if (load_path.size()) {
std::ifstream in(load_path, std::ios::in);
in >> stats;
in.close();
if (stats.is_finished()) stats.summary();
}
random_slider slide(slide_args);
random_placer place(place_args);
while (!stats.is_finished()) {
// std::cerr << "======== Game " << stats.step() << " ========" << std::endl;
slide.open_episode("~:" + place.name());
place.open_episode(slide.name() + ":~");
stats.open_episode(slide.name() + ":" + place.name());
episode& game = stats.back();
while (true) {
agent& who = game.take_turns(slide, place);
action move = who.take_action(game.state());
// std::cerr << game.state() << "#" << game.step() << " " << who.name() << ": " << move << std::endl;
if (game.apply_action(move) != true) break;
if (who.check_for_win(game.state())) break;
}
agent& win = game.last_turns(slide, place);
stats.close_episode(win.name());
slide.close_episode(win.name());
place.close_episode(win.name());
}
if (save_path.size()) {
std::ofstream out(save_path, std::ios::out | std::ios::trunc);
out << stats;
out.close();
}
return 0;
}