-
Notifications
You must be signed in to change notification settings - Fork 0
/
Moderator.cpp
576 lines (538 loc) · 16.2 KB
/
Moderator.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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "Moderator.h"
Moderator::Moderator()
{
commandLine = false;
gamestate = GAME_STOPPED;
timer = new QTimer();
connect(timer,SIGNAL(timeout()),SLOT(updateTimer()));
timePerTurnTimer = new QTimer();
connect(this->timePerTurnTimer,SIGNAL(timeout()),this,SLOT(decrementTimePerTurnTimer()));
timePerMove = 10000;
delayBeforeMove = 1000;
//choose your directory
player1GoesFirst = true;
player1 = NULL;
player2 = NULL;
}
Moderator::~Moderator()
{
}
#ifdef Q_OS_WIN
#include <windows.h> // for Sleep
#endif
void Moderator::qSleep(int ms)
{
#ifdef Q_OS_WIN
Sleep(uint(ms));
#else
struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
nanosleep(&ts, NULL);
#endif
}
void Moderator::directoryTextBoxEdited(){
}
bool Moderator::playerMove(bool isPlayer1,int input){
bool retVal = true;
QPair<int,QPair<int,int> > playerOutput = currentGame.playMove(input);
if(playerOutput.first==-1){
if(isPlayer1){
if(player1->isManual) {
console("INVALID");
writeLineToTerminal("INVALID");
return false;
}
console("Player 1 placed a piece in an invalid location. ("+QString().setNum(input)+")");
player2Wins(true);
}
else{
if(player2->isManual) {
console("INVALID");
writeLineToTerminal("INVALID");
return false;
}
console("Player 2 placed a piece in an invalid location. ("+QString().setNum(input)+")");
player1Wins(true);
}
return false;
}
emit(piecePlaced(playerOutput.second.first,playerOutput.second.second));
if(playerOutput.first==-2){
tieGame();
retVal = false;
}
if(playerOutput.first==0){
if(isPlayer1){
if(commandLine)
writeLineToTerminal("PLAYER1MOVE"+QString().setNum(playerOutput.second.first)+QString().setNum(playerOutput.second.second));
player1Wins(false);
}
else{
if(commandLine)
writeLineToTerminal("PLAYER2MOVE"+QString().setNum(playerOutput.second.first)+QString().setNum(playerOutput.second.second));
player2Wins(false);
}
retVal = false;
}
return retVal;
}
void Moderator::lookForMove(){
if(gamestate==PLAYER_1_TO_MOVE)
player1HasMoved();
if(gamestate==PLAYER_2_TO_MOVE)
player2HasMoved();
if(gamestate==PLAYER_1_TO_MOVE && player1MadeAMove){
int move = plyr1Move;
QString moveChars = QString().setNum(move);
moveChars.append("\n");
player1MadeAMove = false;
console(QString("Player 1's move: " + QString().setNum(move)));
if(playerMove(true,move)==false){
return;
}
writeLineToTerminal("PLAYER1MOVE"+QString().setNum(move)+QString().setNum(currentGame.getColFill(move-1))); // 1 has already been added to colfill
if(!player2->isManual){
player2->write(moveChars.toStdString().c_str());
player2->waitForBytesWritten();
}
gamestate = PLAYER_2_TO_MOVE;
emit(player2ToMove(player2->isManual));
timeRemaining = timePerMove;
timeUntilMove = delayBeforeMove;
emit timeUntilMoveChanged(timePerMove,true);
emit timeUntilMoveChanged(timePerMove,false);
}
else{
if(gamestate==PLAYER_2_TO_MOVE && player2MadeAMove){
int move = plyr2Move;
QString moveChars = QString().setNum(move);
moveChars.append("\n");
player2MadeAMove = false;
console(QString("Player 2's move: " + QString().setNum(move)));
if(playerMove(false, move)==false){
return;
}
writeLineToTerminal("PLAYER2MOVE"+QString().setNum(move)+QString().setNum(currentGame.getColFill(move-1)));
if(!player1->isManual){
player1->write(moveChars.toStdString().c_str());
player1->waitForBytesWritten();
}
gamestate = PLAYER_1_TO_MOVE;
emit(player1ToMove(player1->isManual));
timeRemaining = timePerMove;
timeUntilMove = delayBeforeMove;
emit timeUntilMoveChanged(timePerMove,true);
emit timeUntilMoveChanged(timePerMove,false);
}
}
}
void Moderator::playerHasMoved(bool isPlayer1){
Player* player;
if(isPlayer1) player = player1;
else player = player2;
if(!player)
return;
if(gamestate!=GAME_STOPPED){
QString input = QString(player->readNewInput());
QTextStream qtin(stdin);
if(player->isManual && commandLine){
timer->stop();
timePerTurnTimer->stop();
qtin >> input;
timer->start(10);
timePerTurnTimer->start(10);
}
if((!player->isManual)&&player->state()==QProcess::NotRunning){
if(isPlayer1)
player2Wins(true);
else
player1Wins(true);
return;
}
if(input=="")
return;
if(isPlayer1)
player1MadeAMove = true;
else
player2MadeAMove = true;
if(isPlayer1)
plyr1Move = QString(input).toInt();
else
plyr2Move = QString(input).toInt();
}
}
void Moderator::player1HasMoved(){
playerHasMoved(true);
}
void Moderator::player2HasMoved(){
playerHasMoved(false);
}
void Moderator::player1DroppedPiece(int col){
if(gamestate!=GAME_STOPPED){
player1MadeAMove = true;
plyr1Move = col;
}
}
void Moderator::player2DroppedPiece(int col){
if(gamestate!=GAME_STOPPED){
player2MadeAMove = true;
plyr2Move = col;
}
}
void Moderator::player1Wins(bool dueToError, bool tookTooLong){
playerWins(true,dueToError,tookTooLong);
}
void Moderator::player2Wins(bool dueToError, bool tookTooLong){
playerWins(false,dueToError,tookTooLong);
}
void Moderator::playerWins(bool isPlayer1, bool dueToError, bool tookTooLong){
if(isPlayer1)
console("PLAYER 1 WINS");
else
console("PLAYER 2 WINS");
if(commandLine)
qSleep(1000);
if(dueToError){
if(tookTooLong){
if(isPlayer1){
writeLineToTerminal("PLAYER 1 WINS BECAUSE PLAYER 2 TOOK TOO LONG");
emit gameOver(-2);
}
else{
writeLineToTerminal("PLAYER 2 WINS BECAUSE PLAYER 1 TOOK TOO LONG");
emit gameOver(-1);
}
}
else{
if(isPlayer1){
writeLineToTerminal("PLAYER 1 WINS DUE TO PLAYER 2 ERROR");
emit gameOver(-2);
}
else{
writeLineToTerminal("PLAYER 2 WINS DUE TO PLAYER 1 ERROR");
emit gameOver(-1);
}
}
if(player1)
player1->write("0\n");
if(player2)
player2->write("0\n");
}
else{
if(isPlayer1){
writeLineToTerminal("PLAYER 1 WINS");
emit gameOver(1);
player1->write("-1\n");
player2->write("-1\n");
}
else{
writeLineToTerminal("PLAYER 2 WINS");
emit gameOver(2);
player1->write("-2\n");
player2->write("-2\n");
}
}
endGame();
}
void Moderator::tieGame(){
if(commandLine)
qSleep(1000);
emit gameOver(0);
console("TIE GAME");
writeLineToTerminal("TIE");
player1->write("-3\n");
player2->write("-3\n");
endGame();
}
void Moderator::endGame(){
if(player1&&!player1->isManual){
player1->disconnect();
player1->close();
}
if(player2&&!player2->isManual){
player2->disconnect();
player2->close();
}
QString moveString = currentGame.getMoveString();
if(moveString!=""){
moveString +="\n";
QString filePath = logFilePath;
if(!commandLine){
filePath+= "/log.txt";
QFile file(filePath);
file.open(QIODevice::WriteOnly | QIODevice::Append);
file.write(QByteArray(moveString.toStdString().c_str()));
file.close();
}
else{
exit(1);
}
}
if(player1)
player1->deleteLater();
player1 = NULL;
if(player2)
player2->deleteLater();
player2 = NULL;
gamestate = GAME_STOPPED;
emit gamestring(currentGame.getMoveString());
emit gameHasEnded();
}
bool Moderator::startGame(QStringList player1FileName, QStringList player2FileName, QString logFolder, bool swapTurns, bool cmd, int timeToRespond){
commandLine = cmd;
logFilePath = logFolder;
if(startProgram(player1FileName,true, timeToRespond)&&startProgram(player2FileName,false,timeToRespond)){
if(!player1||!player2)
return false;
if (player1GoesFirst || (!swapTurns)) {
player1GoesFirst = false;
if(!player1->isManual){
player1->write("1\n");
player1->waitForBytesWritten();
}
if(!player2->isManual){
player2->write("2\n");
player2->waitForBytesWritten();
gamestate=PLAYER_2_QUESTION_MARK;
}
else{
gamestate=PLAYER_1_TO_MOVE;
}
}
else {
player1GoesFirst = true;
if(!player2->isManual){
player2->write("1\n");
player2->waitForBytesWritten();
}
if(!player1->isManual){
player1->write("2\n");
player1->waitForBytesWritten();
gamestate=PLAYER_1_QUESTION_MARK;
}
else{
gamestate=PLAYER_2_TO_MOVE;
}
}
if(gamestate==PLAYER_1_QUESTION_MARK){
if(player1->getQuestionMark()){
gamestate = PLAYER_2_TO_MOVE;
}
else{
console("Player 1 failed to output a '?', so player 2 wins.");
player2Wins(true);
emit loadFailed(player1FileName.join(" "));
return false;
}
}
if(gamestate==PLAYER_2_QUESTION_MARK){
if(player2->getQuestionMark()){
gamestate = PLAYER_1_TO_MOVE;
}
else{
console("Player 2 failed to output a '?', so player 1 wins.");
player1Wins(true);
emit loadFailed(player2FileName.join(" "));
return false;
}
}
player1MadeAMove = false;
player2MadeAMove = false;
if(!player1||!player2)
return false;
if(!player1->isManual){
//connect(player1,SIGNAL(readyReadStandardError()),this,SLOT(player1Debug()));
}
else{
emit acceptManualInput();
}
if(!player2->isManual){
//connect(player2,SIGNAL(readyReadStandardError()),this,SLOT(player2Debug()));
}
else{
emit acceptManualInput();
}
timeRemaining = timePerMove;
timeUntilMove = delayBeforeMove;
emit timeUntilMoveChanged(timePerMove,true);
emit timeUntilMoveChanged(timePerMove,false);
timer->start(10);
timePerTurnTimer->start(10);
currentGame = Game();
return true;
}
return false;
}
void Moderator::pauseGame(){
if(gamestate==PLAYER_1_TO_MOVE){
gamestate = GAME_PAUSED_PLAYER_1_TO_MOVE;
}
else if(gamestate==PLAYER_2_TO_MOVE){
gamestate = GAME_PAUSED_PLAYER_2_TO_MOVE;
}
}
void Moderator::resumeGame(){
if(gamestate==GAME_PAUSED_PLAYER_1_TO_MOVE){
gamestate = PLAYER_1_TO_MOVE;
}
else if(gamestate==GAME_PAUSED_PLAYER_2_TO_MOVE){
gamestate = PLAYER_2_TO_MOVE;
}
}
bool Moderator::initializeProgram(Player* &player, QString progName, QStringList args, int timeToRespond){
QStringList fullProgram = args;
fullProgram.prepend(progName);
try{
player = new Player(true,progName,args, timeToRespond);
}
catch(bool){
delete player;
if(progName.endsWith(".exe")){
try{
player = new Player(true,"wine",QStringList(fullProgram),timeToRespond);
}
catch(bool){
delete player;
return false;
}
}
else if(progName.endsWith(".py")){
try{
player = new Player(true,"python",QStringList(fullProgram),timeToRespond);
}
catch(bool){
delete player;
return false;
}
}
else if(progName.endsWith(".jar")){
try{
QStringList fullProgramWithJarFlag = fullProgram;
fullProgramWithJarFlag.prepend("-jar");
player = new Player(true,"java",QStringList(fullProgramWithJarFlag),500);
}
catch(bool){
delete player;
return false;
}
}
else{
delete player;
return false;
}
}
return true;
}
bool Moderator::testProgram(QString progName, QStringList args, int timeToRespond){
Player* player = NULL;
// attempt to initialize the program. If it does not init, return false
if(!initializeProgram(player, progName, args, timeToRespond)){
return false;
}
player->write("2\n");
if(player->getQuestionMark()){
delete player;
return true;
}
else{
delete player;
return false;
}
}
bool Moderator::startProgram(QStringList programName, bool isPlayer1, int timeToRespond){
QString progName = QString("");
if(!programName.isEmpty())
progName= programName.first();
QStringList args = programName;
if(!args.isEmpty())
args.removeFirst();
console(progName);
Player* player;
if(isPlayer1){
player = player1;
if(player1!=NULL){
player1->disconnect();
player1->close();
player1->deleteLater();
player1 = NULL;
}
}
else{
player = player2;
if(player2!=NULL){
player2->disconnect();
player2->close();
player2->deleteLater();
player2 = NULL;
}
}
// attempt to initialize the program. If it does not init, return false
if(!initializeProgram(player, progName, args, timeToRespond)){
return false;
}
if(isPlayer1){
player1 = player;
connect(player1,SIGNAL(readyReadStandardError()),SLOT(player1Debug()));
}
else{
player2 = player;
connect(player2,SIGNAL(readyReadStandardError()),SLOT(player2Debug()));
}
emit loadSuccess(programName);
return true;
}
void Moderator::player1Debug(){
QString input = player1->readAllStandardError();
if(input!=""){
input = "Player 1 says: " + input;
console(input);
}
}
void Moderator::player2Debug(){
QString input = player2->readAllStandardError();
if(input!=""){
input = "Player 2 says: " + input;
console(input);
}
}
void Moderator::alert(QString message){
QMessageBox myBox;
myBox.setText(message);
myBox.exec();
}
void Moderator::console(QString message){
emit consoleOutput(message);
}
void Moderator::writeLineToTerminal(QString message){
if(commandLine){
std::cout << message.toStdString().c_str() << std::endl;
}
}
void Moderator::decrementTimePerTurnTimer(){
timeUntilMove-=10;
if(!player1||!player2)
return;
if(((timeUntilMove<=0))||((gamestate==PLAYER_1_TO_MOVE)&&(player1->isManual)
)||((gamestate==PLAYER_2_TO_MOVE)&&(player2->isManual))){
lookForMove();
}
}
void Moderator::updateTimer()
{
if (gamestate==PLAYER_1_TO_MOVE && !player1->isManual) {
this->timeRemaining-=10;
if(timeRemaining<=0) player2Wins(true, true);
} else if (gamestate==PLAYER_2_TO_MOVE && !player2->isManual) {
this->timeRemaining-=10;
if(timeRemaining<=0) player1Wins(true, true);
}
}
int Moderator::getCurrentPlayer(){
if(gamestate==PLAYER_1_TO_MOVE)
return 1;
if(gamestate==PLAYER_2_TO_MOVE)
return 2;
return 0;
}
void Moderator::setTimeUntilMove(int deciseconds){
delayBeforeMove = deciseconds*100;
}