7 Game* Game::theGame = NULL;
8 bool Game::gameCreated = false;
10 Game::Game(const char * redPath, const char * bluePath, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard)
15 fprintf(stderr, "Game::Game - Error - Tried to create more than one Game!\n");
20 signal(SIGPIPE, Game::HandleBrokenPipe);
23 if (graphicsEnabled && (!Graphics::Initialised()))
24 Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
26 if (strcmp(redPath, "human") == 0)
27 red = new Human_Controller(Piece::RED, graphicsEnabled);
29 red = new AI_Controller(Piece::RED, redPath);
32 if (strcmp(bluePath, "human") == 0)
33 blue = new Human_Controller(Piece::BLUE, graphicsEnabled);
35 blue = new AI_Controller(Piece::BLUE, bluePath);
40 Game::Game(const char * fromFile, const bool enableGraphics, double newStallTime, const bool allowIllegal, FILE * newLog, const Piece::Colour & newReveal, int newMaxTurns, bool newPrintBoard) : red(NULL), blue(NULL), turn(Piece::RED), theBoard(10,10), graphicsEnabled(enableGraphics), stallTime(newStallTime), allowIllegalMoves(allowIllegal), log(newLog), reveal(newReveal), turnCount(0), input(NULL), maxTurns(newMaxTurns), printBoard(newPrintBoard)
45 fprintf(stderr, "Game::Game - Error - Tried to create more than one Game!\n");
50 signal(SIGPIPE, Game::HandleBrokenPipe);
53 if (graphicsEnabled && (!Graphics::Initialised()))
54 Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
56 input = fopen(fromFile, "r");
58 red = new FileController(Piece::RED, input);
59 blue = new FileController(Piece::BLUE, input);
70 if (log != NULL && log != stdout && log != stderr)
73 if (input != NULL && input != stdin)
78 * Attempts to setup the board and controllers
79 * @param redName the name of the red AI
80 * @param blueName the name of the blue AI
81 * @returns A colour, indicating if there were any errors
82 Piece::NONE indicates no errors
83 Piece::BOTH indicates errors with both AI
84 Piece::RED / Piece::BLUE indicates an error with only one of the two AI
86 Piece::Colour Game::Setup(const char * redName, const char * blueName)
91 logMessage("Controller for Player RED is invalid!\n");
95 logMessage("Controller for Player BLUE is invalid!\n");
103 else if (!blue->Valid())
108 for (int y = 4; y < 6; ++y)
110 for (int x = 2; x < 4; ++x)
112 theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
114 for (int x = 6; x < 8; ++x)
116 theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
121 MovementResult redSetup = red->Setup(blueName);
122 MovementResult blueSetup = blue->Setup(redName);
125 Piece::Colour result = Piece::NONE;
126 if (redSetup != MovementResult::OK)
128 if (blueSetup != MovementResult::OK)
130 logMessage("BOTH players give invalid setup!\n");
131 result = Piece::BOTH;
135 logMessage("Player RED gave an invalid setup!\n");
140 else if (blueSetup != MovementResult::OK)
142 logMessage("Player BLUE gave an invalid setup!\n");
143 result = Piece::BLUE;
146 logMessage("%s RED SETUP\n", red->name.c_str());
147 for (int y=0; y < 4; ++y)
149 for (int x=0; x < theBoard.Width(); ++x)
151 if (theBoard.GetPiece(x, y) != NULL)
152 logMessage("%c", Piece::tokens[(int)(theBoard.GetPiece(x, y)->type)]);
159 logMessage("%s BLUE SETUP\n", blue->name.c_str());
160 for (int y=0; y < 4; ++y)
162 for (int x=0; x < theBoard.Width(); ++x)
163 logMessage("%c", Piece::tokens[(int)(theBoard.GetPiece(x, theBoard.Height()-4 + y)->type)]);
172 void Game::Wait(double wait)
177 TimerThread timer(wait*1000000); //Wait in seconds
180 if (!graphicsEnabled)
182 while (!timer.Finished());
188 while (!timer.Finished())
191 while (SDL_PollEvent(&event))
206 void Game::HandleBrokenPipe(int sig)
210 fprintf(stderr, "ERROR - Recieved SIGPIPE during game exit!\n");
213 if (theGame->turn == Piece::RED)
215 theGame->logMessage("Game ends on RED's turn - REASON: ");
216 theGame->blue->Message("DEFAULT");
218 else if (theGame->turn == Piece::BLUE)
221 theGame->logMessage("Game ends on BLUE's turn - REASON: ");
222 theGame->red->Message("DEFAULT");
226 theGame->logMessage("Game ends on ERROR's turn - REASON: ");
230 theGame->logMessage("SIGPIPE - Broken pipe (AI program may have segfaulted)\n");
232 if (Game::theGame->printBoard)
233 Game::theGame->theBoard.PrintPretty(stdout, Piece::BOTH);
235 if (Game::theGame->graphicsEnabled && theGame->log == stdout)
237 theGame->logMessage("CLOSE WINDOW TO EXIT\n");
238 Game::theGame->theBoard.Draw(Piece::BOTH);
242 while (SDL_PollEvent(&event))
255 if (theGame->log == stdout)
257 theGame->logMessage( "PRESS ENTER TO EXIT\n");
258 theGame->theBoard.Print(theGame->log);
259 while (fgetc(stdin) != '\n');
267 void Game::PrintEndMessage(const MovementResult & result)
271 logMessage("Game ends in the SETUP phase - REASON: ");
275 if (turn == Piece::RED)
277 logMessage("Game ends on RED's turn - REASON: ");
279 else if (turn == Piece::BLUE)
281 logMessage("Game ends on BLUE's turn - REASON: ");
285 logMessage("Game ends on ERROR's turn - REASON: ");
291 case MovementResult::OK:
292 logMessage("Status returned OK, unsure why game halted...\n");
294 case MovementResult::DIES:
295 logMessage("Status returned DIES, unsure why game halted...\n");
297 case MovementResult::KILLS:
298 logMessage("Status returned KILLS, unsure why game halted...\n");
300 case MovementResult::BOTH_DIE:
301 logMessage("Status returned BOTH_DIE, unsure why game halted...\n");
303 case MovementResult::NO_BOARD:
304 logMessage("Board does not exit?!\n");
306 case MovementResult::INVALID_POSITION:
307 logMessage("Coords outside board\n");
309 case MovementResult::NO_SELECTION:
310 logMessage("Move does not select a piece\n");
312 case MovementResult::NOT_YOUR_UNIT:
313 logMessage("Selected piece belongs to other player\n");
315 case MovementResult::IMMOBILE_UNIT:
316 logMessage("Selected piece is not mobile (FLAG or BOMB)\n");
318 case MovementResult::INVALID_DIRECTION:
319 logMessage("Selected unit cannot move that way\n");
321 case MovementResult::POSITION_FULL:
322 logMessage("Attempted move into square occupied by neutral or allied piece\n");
324 case MovementResult::VICTORY:
325 logMessage("Captured the flag\n");
327 case MovementResult::BAD_RESPONSE:
328 logMessage("Unintelligable response\n");
330 case MovementResult::NO_MOVE:
331 logMessage("Did not make a move (may have exited)\n");
333 case MovementResult::COLOUR_ERROR:
334 logMessage("Internal controller error - COLOUR_ERROR\n");
336 case MovementResult::ERROR:
337 logMessage("Internal controller error - Unspecified ERROR\n");
339 case MovementResult::DRAW_DEFAULT:
340 logMessage("Game declared a draw after %d turns\n", turnCount);
342 case MovementResult::DRAW:
343 logMessage("Game declared a draw because neither player has mobile pieces\n");
345 case MovementResult::SURRENDER:
346 logMessage("This player has surrendered!\n");
348 case MovementResult::BAD_SETUP:
352 logMessage("An illegal setup was made by RED\n");
355 logMessage("An illegal setup was made by BLUE\n");
358 logMessage("An illegal setup was made by BOTH players\n");
361 logMessage("Unknown internal error.\n");
371 fprintf(stdout, "%d Final State\n", turnCount);
372 theBoard.PrintPretty(stdout, Piece::BOTH);
373 fprintf(stdout, "\n");
375 if (graphicsEnabled && log == stdout)
377 logMessage("CLOSE WINDOW TO EXIT\n");
378 theBoard.Draw(Piece::BOTH);
382 while (SDL_PollEvent(&event))
397 logMessage("PRESS ENTER TO EXIT\n");
398 while (fgetc(stdin) != '\n');
406 MovementResult Game::Play()
409 MovementResult result = MovementResult::OK;
413 Piece::Colour toReveal = reveal;
419 red->Message("START");
423 while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0))
425 if (red->HumanController())
426 toReveal = Piece::RED;
431 fprintf(stdout, "START:\n");
433 fprintf(stdout, "%d BLUE:\n", turnCount);
434 theBoard.PrintPretty(stdout, toReveal);
435 fprintf(stdout, "\n\n");
439 theBoard.Draw(toReveal);
442 logMessage( "%d RED: ", turnCount);
443 result = red->MakeMove(buffer);
444 red->Message(buffer);
445 blue->Message(buffer);
446 logMessage( "%s\n", buffer.c_str());
447 if (Board::HaltResult(result))
455 if (blue->HumanController())
456 toReveal = Piece::BLUE;
460 fprintf(stdout, "%d RED:\n", turnCount);
461 theBoard.PrintPretty(stdout, toReveal);
462 fprintf(stdout, "\n\n");
465 theBoard.Draw(toReveal);
470 logMessage( "%d BLU: ", turnCount);
471 result = blue->MakeMove(buffer);
472 blue->Message(buffer);
473 red->Message(buffer);
474 logMessage( "%s\n", buffer.c_str());
476 if (Board::HaltResult(result))
488 if (theBoard.MobilePieces(Piece::BOTH) == 0)
489 result = MovementResult::DRAW;
494 if ((maxTurns >= 0 && turnCount >= maxTurns) && result == MovementResult::OK)
496 result = MovementResult::DRAW_DEFAULT;
507 * Logs a message to the game's log file if it exists
508 * @param format the format string
509 * @param additional parameters - printed using va_args
510 * @returns the result of vfprintf or a negative number if the log file does not exist
512 int Game::logMessage(const char * format, ...)
517 va_start(ap, format);
519 int result = vfprintf(log, format, ap);
526 * Waits for a user command
527 * Currently ignores the command.
529 void Game::ReadUserCommand()
531 fprintf(stdout, "Waiting for user to press enter...\n");
533 for (char c = fgetc(stdin); c != '\n' && (int)(c) != EOF; c = fgetc(stdin))
539 MovementResult FileController::QuerySetup(const char * opponentName, std::string setup[])
542 char c = fgetc(file);
550 while (fgetc(file) != '\n');
552 for (int y = 0; y < 4; ++y)
555 for (int x = 0; x < Game::theGame->theBoard.Width(); ++x)
557 setup[y] += fgetc(file);
560 if (fgetc(file) != '\n')
562 return MovementResult::BAD_RESPONSE;
565 return MovementResult::OK;
570 MovementResult FileController::QueryMove(std::string & buffer)
574 fgets(buf, sizeof(buf), file);
575 char * s = (char*)(buf);
576 while (*s != ':' && *s != '\0')
582 return MovementResult::OK;