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);
24 if (graphicsEnabled && (!Graphics::Initialised()))
25 Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
26 #endif //BUILD_GRAPHICS
28 if (strcmp(redPath, "human") == 0)
29 red = new Human_Controller(Piece::RED, graphicsEnabled);
31 red = new AI_Controller(Piece::RED, redPath);
34 if (strcmp(bluePath, "human") == 0)
35 blue = new Human_Controller(Piece::BLUE, graphicsEnabled);
37 blue = new AI_Controller(Piece::BLUE, bluePath);
42 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)
47 fprintf(stderr, "Game::Game - Error - Tried to create more than one Game!\n");
52 signal(SIGPIPE, Game::HandleBrokenPipe);
55 if (graphicsEnabled && (!Graphics::Initialised()))
56 Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
57 #endif //BUILD_GRAPHICS
59 input = fopen(fromFile, "r");
61 red = new FileController(Piece::RED, input);
62 blue = new FileController(Piece::BLUE, input);
73 if (log != NULL && log != stdout && log != stderr)
76 if (input != NULL && input != stdin)
81 * Attempts to setup the board and controllers
82 * @param redName the name of the red AI
83 * @param blueName the name of the blue AI
84 * @returns A colour, indicating if there were any errors
85 Piece::NONE indicates no errors
86 Piece::BOTH indicates errors with both AI
87 Piece::RED / Piece::BLUE indicates an error with only one of the two AI
89 Piece::Colour Game::Setup(const char * redName, const char * blueName)
94 logMessage("Controller for Player RED is invalid!\n");
95 if (!red->HumanController())
96 logMessage("Check that program \"%s\" exists and has executable permissions set.\n", redName);
100 logMessage("Controller for Player BLUE is invalid!\n");
101 if (!blue->HumanController())
102 logMessage("Check that program \"%s\" exists and has executable permissions set.\n", blueName);
110 else if (!blue->Valid())
115 for (int y = 4; y < 6; ++y)
117 for (int x = 2; x < 4; ++x)
119 theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
121 for (int x = 6; x < 8; ++x)
123 theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
128 MovementResult redSetup = red->Setup(blueName);
129 MovementResult blueSetup = blue->Setup(redName);
132 Piece::Colour result = Piece::NONE;
133 if (redSetup != MovementResult::OK)
135 if (blueSetup != MovementResult::OK)
137 logMessage("BOTH players give invalid setup!\n");
138 result = Piece::BOTH;
142 //logMessage("Player RED gave an invalid setup!\n");
147 else if (blueSetup != MovementResult::OK)
149 //logMessage("Player BLUE gave an invalid setup!\n");
150 result = Piece::BLUE;
154 logMessage("%s RED SETUP\n", red->name.c_str());
155 if (redSetup == MovementResult::OK)
157 for (int y=0; y < 4; ++y)
159 for (int x=0; x < theBoard.Width(); ++x)
161 if (theBoard.GetPiece(x, y) != NULL)
162 logMessage("%c", Piece::tokens[(int)(theBoard.GetPiece(x, y)->type)]);
171 logMessage("INVALID!\n");
174 logMessage("%s BLUE SETUP\n", blue->name.c_str());
175 if (blueSetup == MovementResult::OK)
177 for (int y=0; y < 4; ++y)
179 for (int x=0; x < theBoard.Width(); ++x)
181 if (theBoard.GetPiece(x, theBoard.Height()-4+y) != NULL)
182 logMessage("%c", Piece::tokens[(int)(theBoard.GetPiece(x, theBoard.Height()-4+y)->type)]);
191 logMessage("INVALID!\n");
199 void Game::Wait(double wait)
204 TimerThread timer(wait*1000000); //Wait in seconds
207 #ifdef BUILD_GRAPHICS
208 if (!graphicsEnabled)
210 while (!timer.Finished());
214 #endif //BUILD_GRAPHICS
216 while (!timer.Finished())
218 #ifdef BUILD_GRAPHICS
220 while (SDL_PollEvent(&event))
230 #endif //BUILD_GRAPHICS
236 void Game::HandleBrokenPipe(int sig)
240 fprintf(stderr, "ERROR - Recieved SIGPIPE during game exit!\n");
243 if (theGame->turn == Piece::RED)
245 theGame->logMessage("Game ends on RED's turn - REASON: ");
246 theGame->blue->Message("DEFAULT");
248 else if (theGame->turn == Piece::BLUE)
251 theGame->logMessage("Game ends on BLUE's turn - REASON: ");
252 theGame->red->Message("DEFAULT");
256 theGame->logMessage("Game ends on ERROR's turn - REASON: ");
260 theGame->logMessage("SIGPIPE - Broken pipe (AI program may have segfaulted)\n");
262 if (Game::theGame->printBoard)
263 Game::theGame->theBoard.PrintPretty(stdout, Piece::BOTH);
266 #ifdef BUILD_GRAPHICS
267 if (Game::theGame->graphicsEnabled && theGame->log == stdout)
269 theGame->logMessage("CLOSE WINDOW TO EXIT\n");
270 Game::theGame->theBoard.Draw(Piece::BOTH);
274 while (SDL_PollEvent(&event))
286 #endif //BUILD_GRAPHICS
288 if (theGame->log == stdout)
290 theGame->logMessage( "PRESS ENTER TO EXIT\n");
291 theGame->theBoard.Print(theGame->log);
292 while (fgetc(stdin) != '\n');
300 void Game::PrintEndMessage(const MovementResult & result)
304 logMessage("Game ends in the SETUP phase - REASON: ");
308 if (turn == Piece::RED)
310 logMessage("Game ends on RED's turn - REASON: ");
312 else if (turn == Piece::BLUE)
314 logMessage("Game ends on BLUE's turn - REASON: ");
318 logMessage("Game ends on ERROR's turn - REASON: ");
324 case MovementResult::OK:
325 logMessage("Status returned OK, unsure why game halted...\n");
327 case MovementResult::DIES:
328 logMessage("Status returned DIES, unsure why game halted...\n");
330 case MovementResult::KILLS:
331 logMessage("Status returned KILLS, unsure why game halted...\n");
333 case MovementResult::BOTH_DIE:
334 logMessage("Status returned BOTH_DIE, unsure why game halted...\n");
336 case MovementResult::NO_BOARD:
337 logMessage("Board does not exit?!\n");
339 case MovementResult::INVALID_POSITION:
340 logMessage("Coords outside board\n");
342 case MovementResult::NO_SELECTION:
343 logMessage("Move does not select a piece\n");
345 case MovementResult::NOT_YOUR_UNIT:
346 logMessage("Selected piece belongs to other player\n");
348 case MovementResult::IMMOBILE_UNIT:
349 logMessage("Selected piece is not mobile (FLAG or BOMB)\n");
351 case MovementResult::INVALID_DIRECTION:
352 logMessage("Selected unit cannot move that way\n");
354 case MovementResult::POSITION_FULL:
355 logMessage("Attempted move into square occupied by neutral or allied piece\n");
357 case MovementResult::VICTORY:
358 logMessage("Captured the flag\n");
360 case MovementResult::BAD_RESPONSE:
361 logMessage("Unintelligable response\n");
363 case MovementResult::NO_MOVE:
364 logMessage("Did not make a move (may have exited)\n");
366 case MovementResult::COLOUR_ERROR:
367 logMessage("Internal controller error - COLOUR_ERROR\n");
369 case MovementResult::ERROR:
370 logMessage("Internal controller error - Unspecified ERROR\n");
372 case MovementResult::DRAW_DEFAULT:
373 logMessage("Game declared a draw after %d turns\n", turnCount);
375 case MovementResult::DRAW:
376 logMessage("Game declared a draw because neither player has mobile pieces\n");
378 case MovementResult::SURRENDER:
379 logMessage("This player has surrendered!\n");
381 case MovementResult::BAD_SETUP:
385 logMessage("An illegal setup was made by RED\n");
388 logMessage("An illegal setup was made by BLUE\n");
391 logMessage("An illegal setup was made by BOTH players\n");
394 logMessage("Unknown internal error.\n");
404 fprintf(stdout, "%d Final State\n", turnCount);
405 theBoard.PrintPretty(stdout, Piece::BOTH);
406 fprintf(stdout, "\n");
409 #ifdef BUILD_GRAPHICS
410 if (graphicsEnabled && log == stdout)
412 logMessage("CLOSE WINDOW TO EXIT\n");
413 theBoard.Draw(Piece::BOTH);
417 while (SDL_PollEvent(&event))
429 #endif //BUILD_GRAPHICS
433 logMessage("PRESS ENTER TO EXIT\n");
434 while (fgetc(stdin) != '\n');
442 MovementResult Game::Play()
445 MovementResult result = MovementResult::OK;
449 Piece::Colour toReveal = reveal;
455 red->Message("START");
459 while (!Board::HaltResult(result) && (turnCount < maxTurns || maxTurns < 0))
461 if (red->HumanController() && blue->HumanController())
462 toReveal = Piece::RED;
467 fprintf(stdout, "START:\n");
469 fprintf(stdout, "%d BLUE:\n", turnCount);
470 theBoard.PrintPretty(stdout, toReveal);
471 fprintf(stdout, "\n\n");
474 #ifdef BUILD_GRAPHICS
476 theBoard.Draw(toReveal);
477 #endif //BUILD_GRAPHICS
480 logMessage( "%d RED: ", turnCount);
481 result = red->MakeMove(buffer);
482 red->Message(buffer);
483 blue->Message(buffer);
484 logMessage( "%s\n", buffer.c_str());
485 if (Board::HaltResult(result))
493 if (blue->HumanController() && red->HumanController())
494 toReveal = Piece::BLUE;
498 fprintf(stdout, "%d RED:\n", turnCount);
499 theBoard.PrintPretty(stdout, toReveal);
500 fprintf(stdout, "\n\n");
503 #ifdef BUILD_GRAPHICS
505 theBoard.Draw(toReveal);
506 #endif //BUILD_GRAPHICS
511 logMessage( "%d BLU: ", turnCount);
512 result = blue->MakeMove(buffer);
513 blue->Message(buffer);
514 red->Message(buffer);
515 logMessage( "%s\n", buffer.c_str());
517 if (Board::HaltResult(result))
529 if (theBoard.MobilePieces(Piece::BOTH) == 0)
530 result = MovementResult::DRAW;
535 if ((maxTurns >= 0 && turnCount >= maxTurns) && result == MovementResult::OK)
537 result = MovementResult::DRAW_DEFAULT;
548 * Logs a message to the game's log file if it exists
549 * @param format the format string
550 * @param additional parameters - printed using va_args
551 * @returns the result of vfprintf or a negative number if the log file does not exist
553 int Game::logMessage(const char * format, ...)
558 va_start(ap, format);
560 int result = vfprintf(log, format, ap);
567 * Waits for a user command
568 * Currently ignores the command.
570 void Game::ReadUserCommand()
572 fprintf(stdout, "Waiting for user to press enter...\n");
574 for (char c = fgetc(stdin); c != '\n' && (int)(c) != EOF; c = fgetc(stdin))
580 MovementResult FileController::QuerySetup(const char * opponentName, std::string setup[])
583 char c = fgetc(file);
591 while (fgetc(file) != '\n');
593 for (int y = 0; y < 4; ++y)
596 for (int x = 0; x < Game::theGame->theBoard.Width(); ++x)
598 setup[y] += fgetc(file);
601 if (fgetc(file) != '\n')
603 return MovementResult::BAD_RESPONSE;
606 return MovementResult::OK;
611 MovementResult FileController::QueryMove(std::string & buffer)
615 fgets(buf, sizeof(buf), file);
616 char * s = (char*)(buf);
617 while (*s != ':' && *s != '\0')
623 return MovementResult::OK;