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

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