13 Piece::Colour SetupGame(int argc, char ** argv);
15 void PrintResults(const MovementResult & result, string & buffer);
17 int main(int argc, char ** argv)
24 fprintf(stderr, "Usage: stratego [options] red blue\n");
25 fprintf(stderr, " stratego --help\n");
31 Piece::Colour setupError = SetupGame(argc, argv);
32 MovementResult result = MovementResult::OK;
33 if (setupError == Piece::NONE)
35 result = Game::theGame->Play();
39 result = MovementResult::BAD_SETUP;
40 Game::theGame->ForceTurn(setupError);
43 Game::theGame->PrintEndMessage(result);
46 PrintResults(result, buffer);
48 //Message the AI's the quit message
49 Game::theGame->red->Message("QUIT " + buffer);
50 Game::theGame->blue->Message("QUIT " + buffer);
53 if (Game::theGame->GetLogFile() != stdout)
54 Game::theGame->logMessage("%s\n", buffer.c_str());
56 fprintf(stdout, "%s\n", buffer.c_str());
62 Piece::Colour SetupGame(int argc, char ** argv)
64 char * red = NULL; char * blue = NULL; double timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
65 Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false;
66 for (int ii=1; ii < argc; ++ii)
68 if (argv[ii][0] == '-')
75 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -t switch!\n");
78 if (strcmp(argv[ii+1], "inf") == 0)
81 timeout = atof(argv[ii+1]);
88 printBoard = !printBoard;
97 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
102 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
105 if (strcmp(argv[ii+1], "stdout") == 0)
108 log = fopen(argv[ii+1], "w");
115 if (reveal == Piece::BOTH)
116 reveal = Piece::BLUE;
118 reveal = Piece::NONE;
121 if (reveal == Piece::BOTH)
124 reveal = Piece::NONE;
129 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
132 if (strcmp(argv[ii+1], "inf") == 0)
135 maxTurns = atoi(argv[ii+1]);
141 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
146 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
149 red = (char*)("file");
150 blue = (char*)("file");
151 inputFile = argv[ii+1];
156 system("less manual.txt");
160 if (strcmp(argv[ii]+2, "help") == 0)
163 system("less manual.txt");
168 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
178 else if (blue == NULL)
182 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
190 if (inputFile == NULL)
192 if (red == NULL || blue == NULL) //Not enough arguments
194 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");
197 Game::theGame = new Game(red,blue, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
201 Game::theGame = new Game(inputFile, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
204 if (Game::theGame == NULL)
206 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
211 return Game::theGame->Setup(red, blue);
216 void PrintResults(const MovementResult & result, string & buffer)
219 switch (Game::theGame->Turn())
222 s << Game::theGame->red->name << " RED ";
225 s << Game::theGame->blue->name << " BLUE ";
228 s << "neither BOTH ";
231 s << "neither NONE ";
235 if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
237 else if (!Board::HaltResult(result))
238 s << "INTERNAL_ERROR ";
243 case MovementResult::VICTORY:
246 case MovementResult::SURRENDER:
249 case MovementResult::DRAW:
252 case MovementResult::DRAW_DEFAULT:
253 s << "DRAW_DEFAULT ";
255 case MovementResult::BAD_SETUP:
259 s << "INTERNAL_ERROR ";
264 s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
273 delete Game::theGame;
274 Game::theGame = NULL;