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)
208 if (theGame->turn == Piece::RED)
210 theGame->logMessage("Game ends on RED's turn - REASON: ");
211 theGame->blue->Message("DEFAULT");
213 else if (theGame->turn == Piece::BLUE)
216 theGame->logMessage("Game ends on BLUE's turn - REASON: ");
217 theGame->red->Message("DEFAULT");
221 theGame->logMessage("Game ends on ERROR's turn - REASON: ");
225 theGame->logMessage("SIGPIPE - Broken pipe (AI program may have segfaulted)\n");
227 if (Game::theGame->printBoard)
228 Game::theGame->theBoard.PrintPretty(stdout, Piece::BOTH);
230 if (Game::theGame->graphicsEnabled && theGame->log == stdout)
232 theGame->logMessage("CLOSE WINDOW TO EXIT\n");
233 Game::theGame->theBoard.Draw(Piece::BOTH);
237 while (SDL_PollEvent(&event))
250 if (theGame->log == stdout)
252 theGame->logMessage( "PRESS ENTER TO EXIT\n");
253 theGame->theBoard.Print(theGame->log);
254 while (fgetc(stdin) != '\n');
262 void Game::PrintEndMessage(const MovementResult & result)
266 logMessage("Game ends in the SETUP phase - REASON: ");
270 if (turn == Piece::RED)
272 logMessage("Game ends on RED's turn - REASON: ");
274 else if (turn == Piece::BLUE)
276 logMessage("Game ends on BLUE's turn - REASON: ");
280 logMessage("Game ends on ERROR's turn - REASON: ");
286 case MovementResult::OK:
287 logMessage("Status returned OK, unsure why game halted...\n");
289 case MovementResult::DIES:
290 logMessage("Status returned DIES, unsure why game halted...\n");
292 case MovementResult::KILLS:
293 logMessage("Status returned KILLS, unsure why game halted...\n");
295 case MovementResult::BOTH_DIE:
296 logMessage("Status returned BOTH_DIE, unsure why game halted...\n");
298 case MovementResult::NO_BOARD:
299 logMessage("Board does not exit?!\n");
301 case MovementResult::INVALID_POSITION:
302 logMessage("Coords outside board\n");
304 case MovementResult::NO_SELECTION:
305 logMessage("Move does not select a piece\n");
307 case MovementResult::NOT_YOUR_UNIT:
308 logMessage("Selected piece belongs to other player\n");
310 case MovementResult::IMMOBILE_UNIT:
311 logMessage("Selected piece is not mobile (FLAG or BOMB)\n");
313 case MovementResult::INVALID_DIRECTION:
314 logMessage("Selected unit cannot move that way\n");
316 case MovementResult::POSITION_FULL:
317 logMessage("Attempted move into square occupied by neutral or allied piece\n");
319 case MovementResult::VICTORY:
320 logMessage("Captured the flag\n");
322 case MovementResult::BAD_RESPONSE:
323 logMessage("Unintelligable response\n");
325 case MovementResult::NO_MOVE:
326 logMessage("Did not make a move (may have exited)\n");
328 case MovementResult::COLOUR_ERROR:
329 logMessage("Internal controller error - COLOUR_ERROR\n");
331 case MovementResult::ERROR:
332 logMessage("Internal controller error - Unspecified ERROR\n");
334 case MovementResult::DRAW_DEFAULT:
335 logMessage("Game declared a draw after %d turns\n", turnCount);
337 case MovementResult::DRAW:
338 logMessage("Game declared a draw because neither player has mobile pieces\n");
340 case MovementResult::SURRENDER:
341 logMessage("This player has surrendered!\n");
343 case MovementResult::BAD_SETUP:
347 logMessage("An illegal setup was made by RED\n");
350 logMessage("An illegal setup was made by BLUE\n");
353 logMessage("An illegal setup was made by BOTH players\n");
356 logMessage("Unknown internal error.\n");
366 fprintf(stdout, "%d Final State\n", turnCount);
367 theBoard.PrintPretty(stdout, Piece::BOTH);
368 fprintf(stdout, "\n");
370 if (graphicsEnabled && log == stdout)
372 logMessage("CLOSE WINDOW TO EXIT\n");
373 theBoard.Draw(Piece::BOTH);
377 while (SDL_PollEvent(&event))
392 logMessage("PRESS ENTER TO EXIT\n");
393 while (fgetc(stdin) != '\n');
401 MovementResult Game::Play()
404 MovementResult result = MovementResult::OK;
408 Piece::Colour toReveal = reveal;
414 red->Message("START");
418 while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0))
420 if (red->HumanController())
421 toReveal = Piece::RED;
426 fprintf(stdout, "START:\n");
428 fprintf(stdout, "%d BLUE:\n", turnCount);
429 theBoard.PrintPretty(stdout, toReveal);
430 fprintf(stdout, "\n\n");
434 theBoard.Draw(toReveal);
437 logMessage( "%d RED: ", turnCount);
438 result = red->MakeMove(buffer);
439 red->Message(buffer);
440 blue->Message(buffer);
441 logMessage( "%s\n", buffer.c_str());
442 if (Board::HaltResult(result))
450 if (blue->HumanController())
451 toReveal = Piece::BLUE;
455 fprintf(stdout, "%d RED:\n", turnCount);
456 theBoard.PrintPretty(stdout, toReveal);
457 fprintf(stdout, "\n\n");
460 theBoard.Draw(toReveal);
465 logMessage( "%d BLU: ", turnCount);
466 result = blue->MakeMove(buffer);
467 blue->Message(buffer);
468 red->Message(buffer);
469 logMessage( "%s\n", buffer.c_str());
471 if (Board::HaltResult(result))
483 if (theBoard.MobilePieces(Piece::BOTH) == 0)
484 result = MovementResult::DRAW;
489 if ((maxTurns >= 0 && turnCount >= maxTurns) && result == MovementResult::OK)
491 result = MovementResult::DRAW_DEFAULT;
502 * Logs a message to the game's log file if it exists
503 * @param format the format string
504 * @param additional parameters - printed using va_args
505 * @returns the result of vfprintf or a negative number if the log file does not exist
507 int Game::logMessage(const char * format, ...)
512 va_start(ap, format);
514 int result = vfprintf(log, format, ap);
521 * Waits for a user command
522 * Currently ignores the command.
524 void Game::ReadUserCommand()
526 fprintf(stdout, "Waiting for user to press enter...\n");
528 for (char c = fgetc(stdin); c != '\n' && (int)(c) != EOF; c = fgetc(stdin))
534 MovementResult FileController::QuerySetup(const char * opponentName, std::string setup[])
537 char c = fgetc(file);
545 while (fgetc(file) != '\n');
547 for (int y = 0; y < 4; ++y)
550 for (int x = 0; x < Game::theGame->theBoard.Width(); ++x)
552 setup[y] += fgetc(file);
555 if (fgetc(file) != '\n')
557 return MovementResult::BAD_RESPONSE;
560 return MovementResult::OK;
565 MovementResult FileController::QueryMove(std::string & buffer)
569 fgets(buf, sizeof(buf), file);
570 char * s = (char*)(buf);
571 while (*s != ':' && *s != '\0')
577 return MovementResult::OK;