-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
219 lines (187 loc) · 7.18 KB
/
main.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
#include <GameEngine/GameEngine.h>
#include <GameEngine/HumanPlayer.h>
#include <string.h>
#include <Raphael/Raphael_v1.0.hpp>
#include <Raphael/Raphael_v1.8.hpp>
#include <Raphael/Raphael_v2.0.hpp>
#include <fstream>
using std::cout;
using std::ifstream;
using std::string;
using std::vector;
/** 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
*/
cge::GamePlayer* player_factory(char* playertype, char* name) {
if (!strcmp(playertype, "human") || !strcmp(playertype, "Human"))
return new cge::HumanPlayer(name);
else if (!strcmp(playertype, "Raphaelv1.0"))
return new Raphael::v1_0(name);
else if (!strcmp(playertype, "Raphaelv1.8"))
return new Raphael::v1_8(name);
else if (!strcmp(playertype, "Raphaelv2.0"))
return new Raphael::v2_0(name);
else if (!strcmp(playertype, "Raphael"))
return new Raphael::v1_8(name); // FIXME: use v1.8 for now until v2 is done
// invalid
printf("Invalid player type: %s\n", playertype);
printf("Valid player types are:\n");
printf(" human:\t cge::HumanPlayer\n");
printf(" Raphael:\t Raphael::v1_8\n"); // FIXME:
printf(" Raphaelv1.0:\t Raphael::v1_0\n");
printf(" Raphaelv1.8:\t Raphael::v1_8\n");
printf(" Raphaelv2.0:\t Raphael::v2_0\n");
return nullptr;
}
/* 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
// << " -e Evaluation mode (options will be ignored)\n\n" // records fen + eval
<< "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) {
cge::GamePlayer* p1;
cge::GamePlayer* p2;
vector<cge::GameEngine::GameOptions> gameoptions;
// invalid
if (argc < 5) {
print_usage();
return -1;
}
// create players
p1 = player_factory(argv[1], argv[2]);
p2 = player_factory(argv[3], argv[4]);
if (!p1 || !p2) return -1;
// parse mode
int 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
int n_matches = atoi(argv[i]);
if (n_matches) {
bool p1_is_white = true;
for (int 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
vector<cge::GamePlayer*> players = {p1, p2};
cge::GameEngine ge(players);
// Play Matches
int matchn = 1;
int 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();
delete p1;
delete p2;
return 0;
}