Setup client and server properly
[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         bool server = false; bool client = false;
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 if (strcmp(argv[ii]+2, "server") == 0)
189                                         {
190                                                 if (client == true)
191                                                 {
192                                                         fprintf(stderr, "ARGUMENT_ERROR - Can't be both a server and a client!\n");
193                                                         exit(EXIT_FAILURE);
194                                                 }
195                                                 server = true;
196                                                 
197                                         }
198                                         else if (strcmp(argv[ii]+2, "client") == 0)
199                                         {
200                                                 if (server == true)
201                                                 {
202                                                         fprintf(stderr, "ARGUMENT_ERROR - Can't be both a server and a client!\n");
203                                                         exit(EXIT_FAILURE);
204                                                 }
205                                                 client = true;  
206                                                 
207                                         }
208                                         else
209                                         {
210                                                 fprintf(stderr, "ARGUMENT_ERROR - Unrecognised switch \"%s\"...\n", argv[ii]);
211                                                 exit(EXIT_FAILURE);
212                                         }
213                         }
214                         
215                 }
216                 else
217                 {
218                         if (red == NULL)
219                                 red = argv[ii];
220                         else if (blue == NULL)
221                                 blue = argv[ii];
222                         else
223                         {
224                                 fprintf(stderr, "ARGUMENT_ERROR - Unexpected argument \"%s\"...\n", argv[ii]);
225                                 exit(EXIT_FAILURE);
226                         }
227                 }
228         }
229
230         if (graphics && stallTime == 0.0)
231                 stallTime = 0.00001; //Hack so that SDL events (ie SDL_QUIT) will have time to be captured when graphics are enabled
232
233         if (inputFile == NULL)
234         {
235                 if (server)  
236                 {
237                         if (red != NULL && blue != NULL)
238                         {
239                                 fprintf(stderr, "ARGUMENT_ERROR - When using the --server switch, only supply ONE (1) player.\n");
240                                 exit(EXIT_FAILURE);
241                         }
242                 }
243                 else if (red == NULL || blue == NULL) //Not enough players
244                 {
245                         if (client)
246                                 fprintf(stderr, "ARGUMENT_ERROR - When using the --client switch, supply an IP for the Red player.\n");
247                         else                    
248                                 fprintf(stderr, "ARGUMENT_ERROR - Did not recieve enough players (did you mean to use the -f switch?)\n");      
249                         exit(EXIT_FAILURE);     
250                 }
251                 
252                 if (client)
253                 {
254                         blue = red; red = NULL;
255                 }
256
257                 Game::theGame = new Game(red,blue, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime, server, client);
258         }
259         else
260         {
261                 if (server || client)
262                 {
263                         fprintf(stderr, "ARGUMENT_ERROR - The -f switch is incompatable with the --server or --client switches!\n");
264                         exit(EXIT_FAILURE);
265                 }
266                 Game::theGame = new Game(inputFile, graphics, stallTime, allowIllegal,log, reveal,maxTurns, printBoard, timeoutTime);
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