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

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