-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
217 lines (186 loc) · 7.09 KB
/
main.cpp
File metadata and controls
217 lines (186 loc) · 7.09 KB
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
#include <GameEngine/GameEngine.h>
#include <GameEngine/HumanPlayer.h>
#include <Raphael/Raphael.h>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <optional>
using std::cout;
using std::ifstream;
using std::nullopt;
using std::optional;
using std::string;
using std::vector;
using std::visit;
/** Creates a specified GamePlayer
*
* \param playertype player type as a null-terminated cstring
* \param name name of player as a null-terminated cstring
* \returns a dynamically allocated player
*/
optional<cge::GameEngine::Player> player_factory(char* playertype, char* name) {
if (!strcmp(playertype, "human") || !strcmp(playertype, "Human")) {
auto player = new cge::HumanPlayer(name);
return cge::GameEngine::Player{
.player = player, .type = cge::GameEngine::PlayerType::HUMAN
};
} else if (!strcmp(playertype, "Raphael")) {
auto player = new raphael::Raphael(name);
return cge::GameEngine::Player{
.player = player, .type = cge::GameEngine::PlayerType::ENGINE
};
}
// invalid
printf("Invalid player type: %s\n", playertype);
printf("Valid player types are:\n");
printf(" human: Human-controlled player\n");
printf(" Raphael: Raphael %s\n", raphael::Raphael::version.c_str());
return nullopt;
}
/* Prints help/usage text */
void print_usage() {
cout << "Usage: main.exe <p1type> <p1name> <p2type> <p2name> [mode] [options]\n\n"
<< "Modes:\n"
<< " [int] Number of matches (defaults to 1 if not specified)\n"
<< " -c Comparison mode (options will be ignored)\n" // comparing 2 engines
<< " -r Results mode (options will be ignored)\n\n" // records fen + wdl
<< "Options:\n"
<< " -t <int> <int> Time (sec) for white and black (defaults to 10min each)\n"
<< " -i <int> Time increment (sec) (defaults to 0sec)\n"
<< " -f <FENstring> Starting position FEN (defaults to standard chess board)\n"
<< " -s <filename> Filename for saving as pgn (saves inside /logs folder)\n\n";
}
/*
Example
main.exe human "Adam" Raphael "Raphael"
1 rapid match, human (white) vs Raphael (black)
main.exe human "Adam" Raphael "Raphael" 5
5 rapid matches, human vs Raphael (human plays white 3 times)
main.exe human "Adam" human "Bob" -t 60 60
1 bullet match, both human players
main.exe Raphael "Raphael" Human "Bob" 3 -f "8/8/2q5/2k5/8/5K2/8/8 w - - 0 1"
3 rapid matches with set starting position, Raphael vs human (human plays white 1 time)
main.exe Raphael "Raph1" Raphael "Raph2" 3 -f "8/8/2q5/2k5/8/5K2/8/8 w - - 0 1" -t 120 120 -i 1
3 2|1 blitz matches with set starting position, both Raphael\
main.exe Raphael "new" Raphaelv1.0 "old" -c
400 comparison matches between the newest version of Raphael and Raphaelv1.0
*/
int main(int argc, char** argv) {
// invalid
if (argc < 5) {
print_usage();
return -1;
}
// create players
auto p1 = player_factory(argv[1], argv[2]);
auto p2 = player_factory(argv[3], argv[4]);
if (!p1.has_value() || !p2.has_value()) return -1;
vector<cge::GameEngine::Player> players = {*p1, *p2};
// parse mode
vector<cge::GameEngine::GameOptions> gameoptions;
i32 i = 5;
if (argc >= 6) {
// comparison mode
if (!strcmp(argv[i], "-c")) {
ifstream pgns("src/Games/randomGames.txt");
string pgn;
bool p1_is_white = true;
// create 400 matches with alterating color
while (getline(pgns, pgn)) {
gameoptions.push_back({
.p1_is_white = p1_is_white,
.interactive = false,
.t_inc = 0,
.t_remain = {20000, 20000},
.start_fen = pgn,
.pgn_file = "./logs/compare.pgn"
});
p1_is_white = !p1_is_white;
}
pgns.close();
i = INT_MAX; // ignore all options
}
else if (!strcmp(argv[i], "-r")) {
ifstream pgns("src/Games/randomGamesBig.txt");
string pgn;
bool p1_is_white = true;
// create 400 matches with alterating color
while (getline(pgns, pgn)) {
gameoptions.push_back({
.p1_is_white = p1_is_white,
.interactive = false,
.t_inc = 20,
.t_remain = {1000, 1000},
.start_fen = pgn,
.pgn_file = "./logs/results.pgn"
});
p1_is_white = !p1_is_white;
}
pgns.close();
i = INT_MAX; // ignore all options
}
// number of games
else {
// create n matches with alternating color
i32 n_matches = atoi(argv[i]);
if (n_matches) {
bool p1_is_white = true;
for (i32 n = 0; n < n_matches; n++) {
gameoptions.push_back({.p1_is_white = p1_is_white});
p1_is_white = !p1_is_white;
}
i++;
} else
gameoptions.push_back({}); // defaults to 1 match
}
} else
gameoptions.push_back({}); // defaults to 1 match
// parse arguments
while (i < argc) {
// time
if (!strcmp(argv[i], "-t")) {
// set the time for every match
float t_white = (float)atof(argv[++i]);
float t_black = (float)atof(argv[++i]);
for (auto& gopt : gameoptions) {
gopt.t_remain[0] = t_white * 1000;
gopt.t_remain[1] = t_black * 1000;
}
}
// time increment
else if (!strcmp(argv[i], "-i")) {
float t_inc = (float)atof(argv[++i]);
for (auto& gopt : gameoptions) gopt.t_inc = t_inc * 1000;
}
// fen
else if (!strcmp(argv[i], "-f") || !strcmp(argv[i], "-fen")) {
i++;
for (auto& gopt : gameoptions) gopt.start_fen = argv[i];
}
// pgn save
else if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "-save")) {
string pgn_file = "./logs/" + (string)argv[++i];
for (auto& gopt : gameoptions) gopt.pgn_file = pgn_file;
}
else {
print_usage();
return -1;
}
i++;
}
// Initialize GameEngine
cge::GameEngine ge(players);
// Play Matches
i32 matchn = 1;
i32 n_matches = gameoptions.size();
for (auto gopt : gameoptions) {
printf("Starting match %i of %i\n", matchn, n_matches);
ge.run_match(gopt);
matchn++;
}
ge.print_report();
visit([](auto player) { delete player; }, players[0].player);
visit([](auto player) { delete player; }, players[1].player);
return 0;
}