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

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