14 Piece::Colour SetupGame(int argc, char ** argv);
16 void PrintResults(const MovementResult & result, string & buffer);
18 int main(int argc, char ** argv)
25 fprintf(stderr, "Usage: stratego [options] red blue\n");
26 fprintf(stderr, " stratego --help\n");
32 Piece::Colour setupError = SetupGame(argc, argv);
33 MovementResult result = MovementResult::OK;
34 if (setupError == Piece::NONE)
36 result = Game::theGame->Play();
40 result = MovementResult::BAD_SETUP;
41 Game::theGame->ForceTurn(setupError);
44 Game::theGame->PrintEndMessage(result);
47 PrintResults(result, buffer);
49 //Message the AI's the quit message
50 Game::theGame->red->Message("QUIT " + buffer);
51 Game::theGame->blue->Message("QUIT " + buffer);
54 if (Game::theGame->GetLogFile() != stdout)
55 Game::theGame->logMessage("%s\n", buffer.c_str());
57 fprintf(stdout, "%s\n", buffer.c_str());
63 Piece::Colour SetupGame(int argc, char ** argv)
65 char * red = NULL; char * blue = NULL; double stallTime = 0.0; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
66 Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false; double timeoutTime = 2.0;
67 for (int ii=1; ii < argc; ++ii)
69 if (argv[ii][0] == '-')
76 fprintf(stderr, "ARGUMENT_ERROR - Expected stall time value after -t switch!\n");
79 if (strcmp(argv[ii+1], "inf") == 0)
82 stallTime = atof(argv[ii+1]);
89 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -T switch!\n");
92 if (strcmp(argv[ii+1], "inf") == 0)
95 timeoutTime = atof(argv[ii+1]);
100 #ifdef BUILD_GRAPHICS
101 graphics = !graphics;
103 fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
105 #endif //BUILD_GRAPHICS
109 printBoard = !printBoard;
118 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
123 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
126 if (strcmp(argv[ii+1], "stdout") == 0)
129 log = fopen(argv[ii+1], "w");
136 if (reveal == Piece::BOTH)
137 reveal = Piece::BLUE;
139 reveal = Piece::NONE;
142 if (reveal == Piece::BOTH)
145 reveal = Piece::NONE;
150 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
153 if (strcmp(argv[ii+1], "inf") == 0)
156 maxTurns = atoi(argv[ii+1]);
162 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
167 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
170 red = (char*)("file");
171 blue = (char*)("file");
172 inputFile = argv[ii+1];
177 system("less manual.txt");
181 if (strcmp(argv[ii]+2, "help") == 0)
184 system("less manual.txt");
189 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
199 else if (blue == NULL)
203 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
209 if (graphics && stallTime == 0.0)
210 stallTime = 0.00001; //Hack so that SDL events (ie SDL_QUIT) will have time to be captured when graphics are enabled
212 if (inputFile == NULL)
214 if (red == NULL || blue == NULL) //Not enough arguments
216 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");
219 Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
223 Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
226 if (Game::theGame == NULL)
228 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
233 return Game::theGame->Setup(red, blue);
238 void PrintResults(const MovementResult & result, string & buffer)
241 switch (Game::theGame->Turn())
244 s << Game::theGame->red->name << " RED ";
247 s << Game::theGame->blue->name << " BLUE ";
250 s << "neither BOTH ";
253 s << "neither NONE ";
257 if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
259 else if (!Board::HaltResult(result))
260 s << "INTERNAL_ERROR ";
265 case MovementResult::VICTORY_FLAG:
266 case MovementResult::VICTORY_ATTRITION: //It does not matter how you win, it just matters that you won!
269 case MovementResult::SURRENDER:
272 case MovementResult::DRAW:
275 case MovementResult::DRAW_DEFAULT:
276 s << "DRAW_DEFAULT ";
278 case MovementResult::BAD_SETUP:
282 s << "INTERNAL_ERROR ";
287 s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
296 delete Game::theGame;
297 Game::theGame = NULL;