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 bool server = false; bool client = false;
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");
188 else if (strcmp(argv[ii]+2, "server") == 0)
192 fprintf(stderr, "ARGUMENT_ERROR - Can't be both a server and a client!\n");
198 else if (strcmp(argv[ii]+2, "client") == 0)
202 fprintf(stderr, "ARGUMENT_ERROR - Can't be both a server and a client!\n");
210 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
220 else if (blue == NULL)
224 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
230 if (graphics && stallTime == 0.0)
231 stallTime = 0.00001; //Hack so that SDL events (ie SDL_QUIT) will have time to be captured when graphics are enabled
233 if (inputFile == NULL)
237 if (red != NULL && blue != NULL)
239 fprintf(stderr, "ARGUMENT_ERROR - When using the --server switch, only supply ONE (1) player.\n");
243 else if (red == NULL || blue == NULL) //Not enough players
245 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");
251 blue = red; red = NULL;
254 Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, server, client);
258 if (server || client)
260 fprintf(stderr, "ARGUMENT_ERROR - The -f switch is incompatable with the --server or --client switches!\n");
263 Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
266 if (Game::theGame == NULL)
268 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
273 return Game::theGame->Setup(red, blue);
278 void PrintResults(const MovementResult & result, string & buffer)
281 switch (Game::theGame->Turn())
284 s << Game::theGame->red->name << " RED ";
287 s << Game::theGame->blue->name << " BLUE ";
290 s << "neither BOTH ";
293 s << "neither NONE ";
297 if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
299 else if (!Board::HaltResult(result))
300 s << "INTERNAL_ERROR ";
305 case MovementResult::VICTORY_FLAG:
306 case MovementResult::VICTORY_ATTRITION: //It does not matter how you win, it just matters that you won!
309 case MovementResult::SURRENDER:
312 case MovementResult::DRAW:
315 case MovementResult::DRAW_DEFAULT:
316 s << "DRAW_DEFAULT ";
318 case MovementResult::BAD_SETUP:
322 s << "INTERNAL_ERROR ";
327 s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
336 delete Game::theGame;
337 Game::theGame = NULL;