Wrote python script to simulate a round
[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 void CreateGame(int argc, char ** argv);
14 void DestroyGame();
15 void PrintResults(const MovementResult & result);
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         CreateGame(argc, argv);
30         if (Game::theGame == NULL)
31         {
32                 fprintf(stderr, "ERROR: Couldn't create a game!\n");
33                 exit(EXIT_FAILURE);
34         }
35
36         MovementResult result = Game::theGame->Play();
37         Game::theGame->PrintEndMessage(result);
38         PrintResults(result);
39
40         exit(EXIT_SUCCESS);
41         return 0;
42 }
43
44 void CreateGame(int argc, char ** argv)
45 {
46         char * red = NULL; char * blue = NULL; double timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
47         Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false;
48         for (int ii=1; ii < argc; ++ii)
49         {
50                 if (argv[ii][0] == '-')
51                 {
52                         switch (argv[ii][1])
53                         {
54                                 case 't':
55                                         if (argc - ii <= 1)
56                                         {
57                                                 fprintf(stderr, "Expected timeout value after -t switch!\n");
58                                                 exit(EXIT_FAILURE);
59                                         }
60                                         timeout = atof(argv[ii+1]);
61                                         ++ii;
62                                         break;
63                                 case 'g':
64                                         graphics = !graphics;
65                                         break;
66                                 case 'p':
67                                         printBoard = !printBoard;
68                                         break;
69                                 case 'i':
70                                         allowIllegal = !allowIllegal;
71                                         break;
72
73                                 case 'o':
74                                         if (argc - ii <= 1)
75                                         {
76                                                 fprintf(stderr, "Expected filename or \"stdout\" after -o switch!\n");
77                                                 exit(EXIT_FAILURE);
78                                         }
79                                         if (log != NULL)
80                                         {
81                                                 fprintf(stderr, "Expected at most ONE -o switch!\n");
82                                                 exit(EXIT_FAILURE);
83                                         }
84                                         if (strcmp(argv[ii+1], "stdout") == 0)
85                                                 log = stdout;
86                                         else
87                                                 log = fopen(argv[ii+1], "w");
88                                         setbuf(log, NULL);
89                                 
90                                         ++ii;
91                                         break;  
92
93                                 case 'r':
94                                         if (reveal == Piece::BOTH)
95                                                 reveal = Piece::BLUE;
96                                         else
97                                                 reveal = Piece::NONE;
98                                         break;                  
99                                 case 'b':
100                                         if (reveal == Piece::BOTH)
101                                                 reveal = Piece::RED;
102                                         else
103                                                 reveal = Piece::NONE;
104                                         break;
105                                 case 'm':
106                                         if (argc - ii <= 1)
107                                         {
108                                                 fprintf(stderr, "Expected max_turns value after -m switch!\n");
109                                                 exit(EXIT_FAILURE);
110                                         }
111                                         if (strcmp(argv[ii+1], "inf"))
112                                                 maxTurns = -1;
113                                         else
114                                                 maxTurns = atoi(argv[ii+1]);
115                                         ++ii;
116                                         break;
117                                 case 'f':
118                                         if (argc - ii <= 1)
119                                         {
120                                                 fprintf(stderr, "Expected filename after -f switch!\n");
121                                                 exit(EXIT_FAILURE);
122                                         }
123                                         if (log != NULL)
124                                         {
125                                                 fprintf(stderr, "Expected at most ONE -f switch!\n");
126                                                 exit(EXIT_FAILURE);
127                                         }
128                                         red = (char*)("file");
129                                         blue = (char*)("file");
130                                         inputFile = argv[ii+1];
131                                         ++ii;
132                                         break;
133                                 case 'h':
134                                         system("clear");        
135                                         system("less manual.txt");
136                                         exit(EXIT_SUCCESS);
137                                         break;
138                                 case '-':
139                                         if (strcmp(argv[ii]+2, "help") == 0)
140                                         {
141                                                 system("clear");        
142                                                 system("less manual.txt");
143                                                 exit(EXIT_SUCCESS);
144                                         }
145                                         else
146                                         {
147                                                 fprintf(stderr, "Unrecognised switch \"%s\"...\n", argv[ii]);
148                                                 exit(EXIT_FAILURE);
149                                         }
150                         }
151                         
152                 }
153                 else
154                 {
155                         if (red == NULL)
156                                 red = argv[ii];
157                         else if (blue == NULL)
158                                 blue = argv[ii];
159                         else
160                         {
161                                 fprintf(stderr, "Unexpected argument \"%s\"...\n", argv[ii]);
162                                 exit(EXIT_FAILURE);
163                         }
164                 }
165         }
166
167         if (inputFile == NULL)
168         {
169                 Game::theGame = new Game(red,blue, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
170         }
171         else
172         {
173                 Game::theGame = new Game(inputFile, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
174         }
175         if (!Game::theGame->Setup(red, blue))
176         {
177                 fprintf(stdout, "NONE %d\n",Game::theGame->TurnCount());
178                 exit(EXIT_SUCCESS);
179         }
180         
181         atexit(DestroyGame);
182
183 }
184
185 void PrintResults(const MovementResult & result)
186 {
187         Piece::Colour winner = Game::theGame->Turn();
188         if (Board::LegalResult(result))
189         {
190                 if (winner == Piece::BOTH)
191                         winner = Piece::NONE;
192                 else
193                 {
194                         if (winner == Piece::RED)
195                                 winner = Piece::BLUE;
196                         else
197                                 winner = Piece::RED;
198                 }
199         }
200         
201
202         switch (winner)
203         {
204                 case Piece::RED:
205                         fprintf(stdout, "%s RED %d\n", Game::theGame->red->name.c_str(),Game::theGame->TurnCount());    
206                         Game::theGame->logMessage("%s RED %d\n", Game::theGame->red->name.c_str(),Game::theGame->TurnCount());
207                         break;
208                 case Piece::BLUE:
209                         fprintf(stdout, "%s BLUE %d\n", Game::theGame->blue->name.c_str(),Game::theGame->TurnCount());  
210                         Game::theGame->logMessage("%s BLUE %d\n", Game::theGame->blue->name.c_str(),Game::theGame->TurnCount());
211                         break;
212                 case Piece::BOTH:
213                         fprintf(stdout, "DRAW %d\n",Game::theGame->TurnCount());
214                         Game::theGame->logMessage("DRAW %d\n",Game::theGame->TurnCount());      
215                         break;
216                 case Piece::NONE:
217                         fprintf(stdout, "NONE %d\n",Game::theGame->TurnCount());        
218                         Game::theGame->logMessage("NONE %d\n",Game::theGame->TurnCount());
219                         break;
220
221         }
222 }
223
224 void DestroyGame()
225 {
226         delete Game::theGame;
227         Game::theGame = NULL;
228 }

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