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 timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
66 Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false;
67 for (int ii=1; ii < argc; ++ii)
69 if (argv[ii][0] == '-')
76 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -t switch!\n");
79 if (strcmp(argv[ii+1], "inf") == 0)
82 timeout = atof(argv[ii+1]);
89 fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
91 #endif //BUILD_GRAPHICS
95 printBoard = !printBoard;
104 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
109 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
112 if (strcmp(argv[ii+1], "stdout") == 0)
115 log = fopen(argv[ii+1], "w");
122 if (reveal == Piece::BOTH)
123 reveal = Piece::BLUE;
125 reveal = Piece::NONE;
128 if (reveal == Piece::BOTH)
131 reveal = Piece::NONE;
136 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
139 if (strcmp(argv[ii+1], "inf") == 0)
142 maxTurns = atoi(argv[ii+1]);
148 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
153 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
156 red = (char*)("file");
157 blue = (char*)("file");
158 inputFile = argv[ii+1];
163 system("less manual.txt");
167 if (strcmp(argv[ii]+2, "help") == 0)
170 system("less manual.txt");
175 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
185 else if (blue == NULL)
189 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
197 if (inputFile == NULL)
199 if (red == NULL || blue == NULL) //Not enough arguments
201 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");
204 Game::theGame = new Game(red,blue, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
208 Game::theGame = new Game(inputFile, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
211 if (Game::theGame == NULL)
213 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
218 return Game::theGame->Setup(red, blue);
223 void PrintResults(const MovementResult & result, string & buffer)
226 switch (Game::theGame->Turn())
229 s << Game::theGame->red->name << " RED ";
232 s << Game::theGame->blue->name << " BLUE ";
235 s << "neither BOTH ";
238 s << "neither NONE ";
242 if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
244 else if (!Board::HaltResult(result))
245 s << "INTERNAL_ERROR ";
250 case MovementResult::VICTORY:
253 case MovementResult::SURRENDER:
256 case MovementResult::DRAW:
259 case MovementResult::DRAW_DEFAULT:
260 s << "DRAW_DEFAULT ";
262 case MovementResult::BAD_SETUP:
266 s << "INTERNAL_ERROR ";
271 s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
280 delete Game::theGame;
281 Game::theGame = NULL;