14 Piece::Colour SetupGame(int argc, char ** argv);
16 void PrintResults(const MovementResult & result, string & buffer);
20 int main(int argc, char ** argv)
27 fprintf(stderr, "Usage: stratego [options] red blue\n");
28 fprintf(stderr, " stratego --help\n");
34 Piece::Colour setupError = SetupGame(argc, argv);
35 MovementResult result = MovementResult::OK;
36 if (setupError == Piece::NONE)
38 result = Game::theGame->Play();
42 result = MovementResult::BAD_SETUP;
43 Game::theGame->ForceTurn(setupError);
46 Game::theGame->PrintEndMessage(result);
49 PrintResults(result, buffer);
51 //Message the AI's the quit message
52 Game::theGame->red->Message("QUIT " + buffer);
53 Game::theGame->blue->Message("QUIT " + buffer);
57 string command = "mv "; command += video; command += " tmp; cd tmp; ffmpeg -r 10 -b 1024k -i %d.bmp "; command += video;
58 command += ";mv "; command += video; command += " ../; cd ../; rm -rf tmp;";
59 system(command.c_str());
63 if (Game::theGame->GetLogFile() != stdout)
64 Game::theGame->logMessage("%s\n", buffer.c_str());
66 fprintf(stdout, "%s\n", buffer.c_str());
74 Piece::Colour SetupGame(int argc, char ** argv)
76 char * red = NULL; char * blue = NULL; double stallTime = 0.0; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
77 Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false; double timeoutTime = 2.0;
78 char * imageOutput = (char*)"";
81 for (int ii=1; ii < argc; ++ii)
83 if (argv[ii][0] == '-')
90 fprintf(stderr, "ARGUMENT_ERROR - Expected stall time value after -t switch!\n");
93 if (strcmp(argv[ii+1], "inf") == 0)
96 stallTime = atof(argv[ii+1]);
103 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -T switch!\n");
106 if (strcmp(argv[ii+1], "inf") == 0)
109 timeoutTime = atof(argv[ii+1]);
114 #ifdef BUILD_GRAPHICS
117 fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
119 #endif //BUILD_GRAPHICS
123 printBoard = !printBoard;
132 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
137 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
140 if (strcmp(argv[ii+1], "stdout") == 0)
143 log = fopen(argv[ii+1], "w");
150 if (reveal == Piece::BOTH)
151 reveal = Piece::BLUE;
153 reveal = Piece::NONE;
156 if (reveal == Piece::BOTH)
159 reveal = Piece::NONE;
164 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
167 if (strcmp(argv[ii+1], "inf") == 0)
170 maxTurns = atoi(argv[ii+1]);
176 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
181 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
184 red = (char*)("file");
185 blue = (char*)("file");
186 inputFile = argv[ii+1];
190 #ifdef BUILD_GRAPHICS
192 #endif //BUILD_GRAPHICS
195 #ifdef BUILD_GRAPHICS
199 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -I switch!\n");
202 imageOutput = argv[ii+1];
203 string m("mkdir -p "); m += imageOutput;
207 fprintf(stderr, "ERROR: -%c switch supplied, but the program was not built with graphics.");
209 #endif //BUILD_GRAPHICS
219 system("less manual.txt");
223 if (strcmp(argv[ii]+2, "help") == 0)
226 system("less manual.txt");
231 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
241 else if (blue == NULL)
245 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
251 if (graphics && stallTime == 0.0)
252 stallTime = 0.00001; //Hack so that SDL events (ie SDL_QUIT) will have time to be captured when graphics are enabled
254 if (inputFile == NULL)
256 if (red == NULL || blue == NULL) //Not enough players
258 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");
262 Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, imageOutput);
266 Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, imageOutput);
269 if (Game::theGame == NULL)
271 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
276 return Game::theGame->Setup(red, blue);
281 void PrintResults(const MovementResult & result, string & buffer)
284 switch (Game::theGame->Turn())
287 s << Game::theGame->red->name << " RED ";
290 s << Game::theGame->blue->name << " BLUE ";
293 s << "neither BOTH ";
296 s << "neither NONE ";
300 if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
302 else if (!Board::HaltResult(result))
303 s << "INTERNAL_ERROR ";
308 case MovementResult::VICTORY_FLAG:
309 case MovementResult::VICTORY_ATTRITION: //It does not matter how you win, it just matters that you won!
312 case MovementResult::SURRENDER:
315 case MovementResult::DRAW:
318 case MovementResult::DRAW_DEFAULT:
319 s << "DRAW_DEFAULT ";
321 case MovementResult::BAD_SETUP:
325 s << "INTERNAL_ERROR ";
330 s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
339 delete Game::theGame;
340 Game::theGame = NULL;