-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject4.cgi.cpp
More file actions
388 lines (294 loc) · 11.3 KB
/
Copy pathproject4.cgi.cpp
File metadata and controls
388 lines (294 loc) · 11.3 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* Handle the webapp junk.
* Shaun Meyer, Mar 2012
*/
// TODO: Game serialization for saving + loading
// TODO: game_id persistance
// TODO: game_id timeouts
// TODO: ?
#include <iostream>
#include <sstream>
#include <vector>
#include "session.hpp"
#include "game.hpp"
#include "neural.hpp"
using namespace std;
#define MAX_GAME_AGE (60*5)
void die(string msg) {
cout << "ERROR_CAUSED_SHUTDOWN" << endl
<< msg << endl;
exit(1);
}
int main() {
string game_id;
string session_id;
string instr; // initial POST instruction
string param; // a place holder for reading in the parameters
vector<string> params; // subsequent POSTS.
getline(cin, instr); // all requests are by POST.
// Initialize `params`, skipping empty lines.
while (getline(cin, param))
if (param != "")
params.push_back(param);
cout << "Content-Type: text/plain\n\n";
// Filter out blank instructions (initial instruction).
if (instr == "")
die("msg: Expected parameter.");
/*
* S E T U P
*/
if (instr == "SETUP")
{
// check for four characters, noting all options
// have size() == 4, begin with h and end with 1 or 2.
// Housekeeping: delete all expired games.
vector<string> games = list_games();
for (int i = 0; i < games.size(); i++)
if (gameid_age(games[i]) > MAX_GAME_AGE)
remove_game(games[i]);
bool human_only;
bool creator_is_white;
string gameid;
string sessionkey;
string game_type = params[0];
if (game_type.size() != 3 || game_type[0] != 'h')
die("Invalid parameter: " + param);
if (game_type[1] == 'h')
human_only = true;
else if (game_type[1] == 'c')
human_only = false;
else
die("2nd character must be h or c.");
if (game_type[2] == '1')
creator_is_white = true;
else if (game_type[2] == '2')
creator_is_white = false;
else
die("3rd character must be 1 or 2.");
// Generate a unique game id and session key.
for (int i = 0; i < NUM_WORDS; i++)
{
gameid = get_word(i);
sessionkey = get_word(++i);
if (!gameid_exists(gameid))
break;
}
ofstream game_file(gameid_file_path(gameid));
if (!game_file.good())
die("Unable to create new game file at '" +
string(gameid_file_path(gameid)) + "'.");
PenteGame *game = new PenteNeuralAI();
if (human_only)
{
if (creator_is_white) // hh1
game->players[0] = sessionkey;
else // hh2
game->players[1] = sessionkey;
}
else
{
if (creator_is_white) // hc1
{
game->players[0] = sessionkey;
game->players[1] = "COMPUTER";
}
else // hc2
{
game->players[0] = "COMPUTER";
game->players[1] = sessionkey;
}
}
// Finally, place the white piece on the board
game->playToken(9,9,WHITE);
// return the details to the client
cout << "SETUP" << endl
<< gameid << endl
<< sessionkey << endl
<< "9" << endl
<< "9" << endl;
if (game_type == "hh1")
cout << "WAITING" << endl;
else if (game_type == "hh2")
cout << "MOVE" << endl;
else if (game_type == "hc1")
{
// generate a computer move and return it.
Weight weights(WEIGHTS_FILE);
weights.load();
game->makeMove(weights);
cout << game->lastRow() << endl
<< game->lastCol() << endl
<< "MOVE" << endl;
}
else if (game_type == "hc2")
cout << "MOVE";
// Save the file.
game_file << game->serialize();
game_file.close();
}
/*
* J O I N
*/
else if (instr == "JOIN")
{
// TODO: Assign a valid session ID otherwise.
// TODO: Or report that the game is full.
/*
JOIN\n<gameid> JOIN\n<gameid>\n<sessionid>
JOIN\n<gameid> JOIN\n<gameid>\nGAME_UNDERWAY
*/
string gameid = params[0];
if (params.size() != 1)
die("Invalid number of parameters.");
if (!gameid_exists(gameid))
die("No game identified by '" + gameid + "'.");
PenteGame *game = new PenteNeuralAI();
ifstream game_file( gameid_file_path(gameid) );
game->deserialize(game_file);
game_file.close();
cout << "JOIN" << endl
<< gameid << endl;
// Both players have joined (are != WAITING)
if (game->players[0] != "WAITING" && game->players[1] != "WAITING")
cout << "GAME_UNDERWAY" << endl;
// The player has joined as player2
if (game->players[0] == "WAITING") {
game->players[0] = generate_sessionid();
cout << game->players[0] << endl
<< "hh2" << endl
<< "9" << endl
<< "9" << endl
<< "WAITING" << endl;
}
// The player has joined as player1
else if (game->players[1] == "WAITING") {
game->players[1] = generate_sessionid();
cout << game->players[1] << endl
<< "hh1" << endl
<< "9" << endl
<< "9" << endl
<< "MOVE" << endl;
}
// Save our game.
ofstream ogame_file( gameid_file_path(gameid) );
ogame_file << game->serialize() << endl;
ogame_file.close();
}
/*
* M O V E
*/
else if (instr == "MOVE")
{
/* POST: MOVE\n<gameid>\n<sessionid>\n<row>\n<col>
RESPONSE:
MOVE\n<gameid>\n<sessionid>
MOVE\n<gameid>\n<sessionid>\nWIN
*/
// TODO: Validate gameid, sessionid, and feed the computer a move.
if (params.size() != 4)
die("Invalid number of parameters.");
string gameid = params[0];
string sessionid = params[1];
int row = atoi(params[2].c_str());
int col = atoi(params[3].c_str());
if (!gameid_exists(gameid))
die("No such gameid '" + gameid + "'.");
PenteGame *game = new PenteNeuralAI();
ifstream infile(gameid_file_path(gameid));
game->deserialize( infile );
infile.close();
// Die for invalid sessionid
if (game->playerNumber(sessionid) == -1)
die("'" + sessionid + "' is not a valid sessionid.");
// Die for invalid coords
if (!game->isValidCoords(row, col))
die("Invalid coordinates."); // this should never happen
// Die for piece already layed.
if(!game->isEmpty(row, col)) {
stringstream ss;
ss << "Cell already has a piece at (" << row << ',' << col << ")";
die(ss.str());
}
// Die if the game is already completed.
if (game->gameOutcome(WHITE) != 0)
die("This game has already been decided.");
// Validate that it is, in fact, our turn.
if (game->playerTurn() != sessionid)
die("It's not even your turn to move!");
// go ahead and lay the piece.
game->playToken(row, col, game->playerColor(sessionid));
// Save the game
ofstream game_file(gameid_file_path(gameid));
game_file << game->serialize();
game_file.close();
cout << "MOVE" << endl
<< gameid << endl
<< sessionid << endl;
if (game->gameOutcome(game->playerColor(sessionid)) == 1)
cout << "WIN" << endl;
}
/*
* C H E C K
*/
else if (instr == "CHECK")
{
// TODO: Return computer (or other player) move.
/* POST:
CHECK\n<gameid>\n<sessionid>
RESPONSE:
CHECK\n<gameid>\n<sessionid>\nWAITING
CHECK\n<gameid>\n<sessionid>\n<row>\n<col>
CHECK\n<gameid>\n<sessionid>\n<row>\n<col>\n{WIN|LOSE}
CHECK\n<gameid>\n<sessionid>\n<row>\n<col>\nTIMEOUT
*/
if (params.size() != 2)
die("Invalid number of CHECK parameters.");
string gameid = params[0];
string sessionid = params[1];
cout << "CHECK" << endl
<< gameid << endl
<< sessionid << endl;
if (!gameid_exists(gameid) || gameid_age(gameid) > MAX_GAME_AGE) {
cout << "9" << endl // bogus row + col because it can't matter.
<< "9" << endl
<< "TIMEOUT" << endl;
// remove the old game.
remove_game(gameid);
exit(1);
}
// Load the game for more checks
PenteGame *game = new PenteNeuralAI();
ifstream game_file( gameid_file_path(gameid) );
game->deserialize(game_file);
game_file.close();
// Validate sessionid
if (game->playerNumber(sessionid) == -1)
die("Invalid sessionid: '" + sessionid + "'");
// If the computer is playing and we're waiting on them,
// go ahead and initiate a computer move.
if (game->playerTurn() == "COMPUTER") {
// Generate a computer move.
Weight weights(WEIGHTS_FILE);
weights.load();
game->makeMove(weights);
// Save the computer move to file.
ofstream ogame_file( gameid_file_path(gameid) );
ogame_file << game->serialize() << endl;
ogame_file.close();
}
// Stop here if it is not the requesting players' turn.
else if (game->playerTurn() != sessionid) {
cout << "WAITING" << endl;
exit(1);
}
// Output row + col of last move:
cout << game->lastRow() << endl
<< game->lastCol() << endl;
// Report a possible victory.
if (game->gameOutcome( game->playerColor(sessionid) ) == 1)
cout << "WIN" << endl;
else if (game->gameOutcome( game->playerColor(sessionid) ) == -1)
cout << "LOSE" << endl;
}
return 0;
}