Tweaking manager/simulate.py, updating manual and webpage
[progcomp2012.git] / judge / manager / main.cpp
1 #include <stdlib.h>
2 #include <stdio.h>
3
4
5
6
7
8
9 #include "game.h"
10
11
12 using namespace std;
13
14 Piece::Colour SetupGame(int argc, char ** argv);
15 void DestroyGame();
16 void PrintResults(const MovementResult & result, string & buffer);
17
18 int main(int argc, char ** argv)
19 {
20         
21
22
23         if (argc == 1)
24         {
25                 fprintf(stderr, "Usage: stratego [options] red blue\n");
26                 fprintf(stderr, "       stratego --help\n");
27                 exit(EXIT_SUCCESS);
28                 
29         }
30         
31
32         Piece::Colour setupError = SetupGame(argc, argv);
33         MovementResult result = MovementResult::OK;
34         if (setupError == Piece::NONE)
35         {
36                 result = Game::theGame->Play();
37         }
38         else
39         {
40                 result = MovementResult::BAD_SETUP;
41                 Game::theGame->ForceTurn(setupError);
42         }
43         
44         Game::theGame->PrintEndMessage(result);
45
46         string buffer = "";
47         PrintResults(result, buffer);
48
49         //Message the AI's the quit message
50         Game::theGame->red->Message("QUIT " + buffer);
51         Game::theGame->blue->Message("QUIT " + buffer);
52
53         //Log the message
54         if (Game::theGame->GetLogFile() != stdout)
55                 Game::theGame->logMessage("%s\n", buffer.c_str());
56
57         fprintf(stdout, "%s\n", buffer.c_str());
58
59         exit(EXIT_SUCCESS);
60         return 0;
61 }
62
63 Piece::Colour SetupGame(int argc, char ** argv)
64 {
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
68         for (int ii=1; ii < argc; ++ii)
69         {
70                 if (argv[ii][0] == '-')
71                 {
72                         switch (argv[ii][1])
73                         {
74                                 case 't':
75                                         if (argc - ii <= 1)
76                                         {
77                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected stall time value after -t switch!\n");
78                                                 exit(EXIT_FAILURE);
79                                         }
80                                         if (strcmp(argv[ii+1], "inf") == 0)
81                                                 stallTime = -1;
82                                         else
83                                                 stallTime = atof(argv[ii+1]);
84                                         ++ii;
85                                         break;
86
87                                 case 'T':
88                                         if (argc - ii <= 1)
89                                         {
90                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -T switch!\n");
91                                                 exit(EXIT_FAILURE);
92                                         }
93                                         if (strcmp(argv[ii+1], "inf") == 0)
94                                                 timeoutTime = -1;
95                                         else
96                                                 timeoutTime = atof(argv[ii+1]);
97                                         ++ii;
98                                         break;
99
100                                 case 'g':
101                                         #ifdef BUILD_GRAPHICS
102                                         graphics = !graphics;
103                                         #else
104                                         fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
105                                         exit(EXIT_FAILURE);
106                                         #endif //BUILD_GRAPHICS
107
108                                         break;
109                                 case 'p':
110                                         printBoard = !printBoard;
111                                         break;
112                                 case 'i':
113                                         allowIllegal = true;
114                                         break;
115
116                                 case 'o':
117                                         if (argc - ii <= 1)
118                                         {
119                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
120                                                 exit(EXIT_FAILURE);
121                                         }
122                                         if (log != NULL)
123                                         {
124                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
125                                                 exit(EXIT_FAILURE);
126                                         }
127                                         if (strcmp(argv[ii+1], "stdout") == 0)
128                                                 log = stdout;
129                                         else
130                                                 log = fopen(argv[ii+1], "w");
131                                         setbuf(log, NULL);
132                                 
133                                         ++ii;
134                                         break;  
135
136                                 case 'r':
137                                         if (reveal == Piece::BOTH)
138                                                 reveal = Piece::BLUE;
139                                         else
140                                                 reveal = Piece::NONE;
141                                         break;                  
142                                 case 'b':
143                                         if (reveal == Piece::BOTH)
144                                                 reveal = Piece::RED;
145                                         else
146                                                 reveal = Piece::NONE;
147                                         break;
148                                 case 'm':
149                                         if (argc - ii <= 1)
150                                         {
151                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
152                                                 exit(EXIT_FAILURE);
153                                         }
154                                         if (strcmp(argv[ii+1], "inf") == 0)
155                                                 maxTurns = -1;
156                                         else
157                                                 maxTurns = atoi(argv[ii+1]);
158                                         ++ii;
159                                         break;
160                                 case 'f':
161                                         if (argc - ii <= 1)
162                                         {
163                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
164                                                 exit(EXIT_FAILURE);
165                                         }
166                                         if (log != NULL)
167                                         {
168                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
169                                                 exit(EXIT_FAILURE);
170                                         }
171                                         red = (char*)("file");
172                                         blue = (char*)("file");
173                                         inputFile = argv[ii+1];
174                                         ++ii;
175                                         break;
176                                 case 'h':
177                                         system("clear");        
178                                         system("less manual.txt");
179                                         exit(EXIT_SUCCESS);
180                                         break;
181                                 case '-':
182                                         if (strcmp(argv[ii]+2, "help") == 0)
183                                         {
184                                                 system("clear");        
185                                                 system("less manual.txt");
186                                                 exit(EXIT_SUCCESS);
187                                         }
188                                         else
189                                         {
190                                                 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
191                                                 exit(EXIT_FAILURE);
192                                         }
193                         }
194                         
195                 }
196                 else
197                 {
198                         if (red == NULL)
199                                 red = argv[ii];
200                         else if (blue == NULL)
201                                 blue = argv[ii];
202                         else
203                         {
204                                 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
205                                 exit(EXIT_FAILURE);
206                         }
207                 }
208         }
209
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
212
213         if (inputFile == NULL)
214         {
215                 if (red == NULL || blue == NULL) //Not enough players
216                 {
217                         fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");      
218                         exit(EXIT_FAILURE);     
219                 }
220
221                 Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
222         }
223         else
224         {
225                 Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
226         }
227
228         if (Game::theGame == NULL)
229         {
230                 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
231                 exit(EXIT_FAILURE);
232         }
233         atexit(DestroyGame);
234         
235         return Game::theGame->Setup(red, blue);
236         
237
238 }
239
240 void PrintResults(const MovementResult & result, string & buffer)
241 {
242         stringstream s("");
243         switch (Game::theGame->Turn())
244         {
245                 case Piece::RED:
246                         s << Game::theGame->red->name << " RED ";
247                         break;
248                 case Piece::BLUE:
249                         s << Game::theGame->blue->name << " BLUE ";
250                         break;
251                 case Piece::BOTH:
252                         s << "neither BOTH ";
253                         break;
254                 case Piece::NONE:
255                         s << "neither NONE ";
256                         break;
257         }
258
259         if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
260                 s << "ILLEGAL ";
261         else if (!Board::HaltResult(result))
262                 s << "INTERNAL_ERROR ";
263         else
264         {
265                 switch (result.type)
266                 {
267                         case MovementResult::VICTORY_FLAG:
268                         case MovementResult::VICTORY_ATTRITION: //It does not matter how you win, it just matters that you won!
269                                 s <<  "VICTORY ";
270                                 break;
271                         case MovementResult::SURRENDER:
272                                 s << "SURRENDER ";
273                                 break;
274                         case MovementResult::DRAW:
275                                 s << "DRAW ";
276                                 break;
277                         case MovementResult::DRAW_DEFAULT:
278                                 s << "DRAW_DEFAULT ";
279                                 break;
280                         case MovementResult::BAD_SETUP:
281                                 s << "BAD_SETUP ";
282                                 break;  
283                         default:
284                                 s << "INTERNAL_ERROR ";
285                                 break;  
286                 }
287         }
288         
289         s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
290
291         buffer = s.str();
292         
293
294 }
295
296 void DestroyGame()
297 {
298         delete Game::theGame;
299         Game::theGame = NULL;
300 }

UCC git Repository :: git.ucc.asn.au