Bug in simulate.py caused by "INTERNAL_ERROR"
[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 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)
68         {
69                 if (argv[ii][0] == '-')
70                 {
71                         switch (argv[ii][1])
72                         {
73                                 case 't':
74                                         if (argc - ii <= 1)
75                                         {
76                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected timeout value after -t switch!\n");
77                                                 exit(EXIT_FAILURE);
78                                         }
79                                         if (strcmp(argv[ii+1], "inf") == 0)
80                                                 timeout = -1;
81                                         else
82                                                 timeout = atof(argv[ii+1]);
83                                         ++ii;
84                                         break;
85                                 case 'g':
86                                         #ifdef BUILD_GRAPHICS
87                                         graphics = !graphics;
88                                         #else
89                                         fprintf(stderr, "ERROR: -g switch supplied, but the program was not built with graphics.\n Please do not use the -g switch.");
90                                         exit(EXIT_FAILURE);
91                                         #endif //BUILD_GRAPHICS
92
93                                         break;
94                                 case 'p':
95                                         printBoard = !printBoard;
96                                         break;
97                                 case 'i':
98                                         allowIllegal = true;
99                                         break;
100
101                                 case 'o':
102                                         if (argc - ii <= 1)
103                                         {
104                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected filename or \"stdout\" after -o switch!\n");
105                                                 exit(EXIT_FAILURE);
106                                         }
107                                         if (log != NULL)
108                                         {
109                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -o switch!\n");
110                                                 exit(EXIT_FAILURE);
111                                         }
112                                         if (strcmp(argv[ii+1], "stdout") == 0)
113                                                 log = stdout;
114                                         else
115                                                 log = fopen(argv[ii+1], "w");
116                                         setbuf(log, NULL);
117                                 
118                                         ++ii;
119                                         break;  
120
121                                 case 'r':
122                                         if (reveal == Piece::BOTH)
123                                                 reveal = Piece::BLUE;
124                                         else
125                                                 reveal = Piece::NONE;
126                                         break;                  
127                                 case 'b':
128                                         if (reveal == Piece::BOTH)
129                                                 reveal = Piece::RED;
130                                         else
131                                                 reveal = Piece::NONE;
132                                         break;
133                                 case 'm':
134                                         if (argc - ii <= 1)
135                                         {
136                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected max_turns value after -m switch!\n");
137                                                 exit(EXIT_FAILURE);
138                                         }
139                                         if (strcmp(argv[ii+1], "inf") == 0)
140                                                 maxTurns = -1;
141                                         else
142                                                 maxTurns = atoi(argv[ii+1]);
143                                         ++ii;
144                                         break;
145                                 case 'f':
146                                         if (argc - ii <= 1)
147                                         {
148                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected filename after -f switch!\n");
149                                                 exit(EXIT_FAILURE);
150                                         }
151                                         if (log != NULL)
152                                         {
153                                                 fprintf(stderr, "ARGUMENT_ERROR - Expected at most ONE -f switch!\n");
154                                                 exit(EXIT_FAILURE);
155                                         }
156                                         red = (char*)("file");
157                                         blue = (char*)("file");
158                                         inputFile = argv[ii+1];
159                                         ++ii;
160                                         break;
161                                 case 'h':
162                                         system("clear");        
163                                         system("less manual.txt");
164                                         exit(EXIT_SUCCESS);
165                                         break;
166                                 case '-':
167                                         if (strcmp(argv[ii]+2, "help") == 0)
168                                         {
169                                                 system("clear");        
170                                                 system("less manual.txt");
171                                                 exit(EXIT_SUCCESS);
172                                         }
173                                         else
174                                         {
175                                                 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
176                                                 exit(EXIT_FAILURE);
177                                         }
178                         }
179                         
180                 }
181                 else
182                 {
183                         if (red == NULL)
184                                 red = argv[ii];
185                         else if (blue == NULL)
186                                 blue = argv[ii];
187                         else
188                         {
189                                 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
190                                 exit(EXIT_FAILURE);
191                         }
192                 }
193         }
194
195
196
197         if (inputFile == NULL)
198         {
199                 if (red == NULL || blue == NULL) //Not enough arguments
200                 {
201                         fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");      
202                         exit(EXIT_FAILURE);     
203                 }
204                 Game::theGame = new Game(red,blue, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
205         }
206         else
207         {
208                 Game::theGame = new Game(inputFile, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
209         }
210
211         if (Game::theGame == NULL)
212         {
213                 fprintf(stderr,"INTERNAL_ERROR - Error creating Game!\n");
214                 exit(EXIT_FAILURE);
215         }
216         atexit(DestroyGame);
217         
218         return Game::theGame->Setup(red, blue);
219         
220
221 }
222
223 void PrintResults(const MovementResult & result, string & buffer)
224 {
225         stringstream s("");
226         switch (Game::theGame->Turn())
227         {
228                 case Piece::RED:
229                         s << Game::theGame->red->name << " RED ";
230                         break;
231                 case Piece::BLUE:
232                         s << Game::theGame->blue->name << " BLUE ";
233                         break;
234                 case Piece::BOTH:
235                         s << "neither BOTH ";
236                         break;
237                 case Piece::NONE:
238                         s << "neither NONE ";
239                         break;
240         }
241
242         if (!Board::LegalResult(result) && result != MovementResult::BAD_SETUP)
243                 s << "ILLEGAL ";
244         else if (!Board::HaltResult(result))
245                 s << "INTERNAL_ERROR ";
246         else
247         {
248                 switch (result.type)
249                 {
250                         case MovementResult::VICTORY:
251                                 s <<  "VICTORY ";
252                                 break;
253                         case MovementResult::SURRENDER:
254                                 s << "SURRENDER ";
255                                 break;
256                         case MovementResult::DRAW:
257                                 s << "DRAW ";
258                                 break;
259                         case MovementResult::DRAW_DEFAULT:
260                                 s << "DRAW_DEFAULT ";
261                                 break;
262                         case MovementResult::BAD_SETUP:
263                                 s << "BAD_SETUP ";
264                                 break;  
265                         default:
266                                 s << "INTERNAL_ERROR ";
267                                 break;  
268                 }
269         }
270         
271         s << Game::theGame->TurnCount() << " " << Game::theGame->theBoard.TotalPieceValue(Piece::RED) << " " << Game::theGame->theBoard.TotalPieceValue(Piece::BLUE);
272
273         buffer = s.str();
274         
275
276 }
277
278 void DestroyGame()
279 {
280         delete Game::theGame;
281         Game::theGame = NULL;
282 }

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