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

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