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;
68 for (int ii=1; ii < argc; ++ii)
70 if (argv[ii][0] == '-')
77 fprintf(stderr, "ARGUMENT_ERROR - Expected stall time value after -t switch!\n");
80 if (strcmp(argv[ii+1], "inf") == 0)
83 stallTime = atof(argv[ii+1]);
90 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -T switch!\n");
93 if (strcmp(argv[ii+1], "inf") == 0)
96 timeoutTime = atof(argv[ii+1]);
101 #ifdef BUILD_GRAPHICS
102 graphics = !graphics;
104 fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
106 #endif //BUILD_GRAPHICS
110 printBoard = !printBoard;
119 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
124 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
127 if (strcmp(argv[ii+1], "stdout") == 0)
130 log = fopen(argv[ii+1], "w");
137 if (reveal == Piece::BOTH)
138 reveal = Piece::BLUE;
140 reveal = Piece::NONE;
143 if (reveal == Piece::BOTH)
146 reveal = Piece::NONE;
151 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
154 if (strcmp(argv[ii+1], "inf") == 0)
157 maxTurns = atoi(argv[ii+1]);
163 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
168 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
171 red = (char*)("file");
172 blue = (char*)("file");
173 inputFile = argv[ii+1];
178 system("less manual.txt");
182 if (strcmp(argv[ii]+2, "help") == 0)
185 system("less manual.txt");
190 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
200 else if (blue == NULL)
204 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
210 if (graphics && stallTime == 0.0)
211 stallTime = 0.00001; //Hack so that SDL events (ie SDL_QUIT) will have time to be captured when graphics are enabled
213 if (inputFile == NULL)
215 if (red == NULL || blue == NULL) //Not enough players
217 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");
221 Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
225 Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
228 if (Game::theGame == NULL)
230 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
235 return Game::theGame->Setup(red, blue);
240 void PrintResults(const MovementResult & result, string & buffer)
243 switch (Game::theGame->Turn())
246 s << Game::theGame->red->name << " RED ";
249 s << Game::theGame->blue->name << " BLUE ";
252 s << "neither BOTH ";
255 s << "neither NONE ";
259 if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
261 else if (!Board::HaltResult(result))
262 s << "INTERNAL_ERROR ";
267 case MovementResult::VICTORY_FLAG:
268 case MovementResult::VICTORY_ATTRITION: //It does not matter how you win, it just matters that you won!
271 case MovementResult::SURRENDER:
274 case MovementResult::DRAW:
277 case MovementResult::DRAW_DEFAULT:
278 s << "DRAW_DEFAULT ";
280 case MovementResult::BAD_SETUP:
284 s << "INTERNAL_ERROR ";
289 s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
298 delete Game::theGame;
299 Game::theGame = NULL;