Revamped manager program and added manual page
[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 int main(int argc, char ** argv)
14 {
15
16         char * red = NULL; char * blue = NULL; double timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
17         Piece::Colour reveal = Piece::BOTH;
18         for (int ii=1; ii < argc; ++ii)
19         {
20                 if (argv[ii][0] == '-')
21                 {
22                         switch (argv[ii][1])
23                         {
24                                 case 't':
25                                         if (argc - ii <= 1)
26                                         {
27                                                 fprintf(stderr, "Expected timeout value after -t switch!\n");
28                                                 exit(EXIT_FAILURE);
29                                         }
30                                         timeout = atof(argv[ii+1]);
31                                         ++ii;
32                                         break;
33                                 case 'g':
34                                         graphics = true;
35                                         break;
36                                 case 'i':
37                                         allowIllegal = true;
38                                         break;
39
40                                 case 'o':
41                                         if (argc - ii <= 1)
42                                         {
43                                                 fprintf(stderr, "Expected filename or \"stdout\" after -o switch!\n");
44                                                 exit(EXIT_FAILURE);
45                                         }
46                                         if (log != NULL)
47                                         {
48                                                 fprintf(stderr, "Expected at most ONE -o switch!\n");
49                                                 exit(EXIT_FAILURE);
50                                         }
51                                         if (strcmp(argv[ii+1], "stdout") == 0)
52                                                 log = stdout;
53                                         else
54                                                 log = fopen(argv[ii+1], "w");
55                                         setbuf(log, NULL);
56                                 
57                                         ++ii;
58                                         break;  
59
60                                 case 'r':
61                                         if (reveal == Piece::BOTH)
62                                                 reveal = Piece::BLUE;
63                                         else
64                                                 reveal = Piece::NONE;
65                                         break;                  
66                                 case 'b':
67                                         if (reveal == Piece::BOTH)
68                                                 reveal = Piece::RED;
69                                         else
70                                                 reveal = Piece::NONE;
71                                         break;
72                                 case 'h':
73                                         system("clear");        
74                                         system("less manual.txt");
75                                         exit(EXIT_SUCCESS);
76                                         break;
77                                 case '-':
78                                         if (strcmp(argv[ii]+2, "help") == 0)
79                                         {
80                                                 system("clear");        
81                                                 system("less manual.txt");
82                                                 exit(EXIT_SUCCESS);
83                                         }
84                                         else
85                                         {
86                                                 fprintf(stderr, "Unrecognised switch \"%s\"...\n", argv[ii]);
87                                                 exit(EXIT_FAILURE);
88                                         }
89                         }
90                         
91                 }
92                 else
93                 {
94                         if (red == NULL)
95                                 red = argv[ii];
96                         else if (blue == NULL)
97                                 blue = argv[ii];
98                         else
99                         {
100                                 fprintf(stderr, "Unexpected argument \"%s\"...\n", argv[ii]);
101                                 exit(EXIT_FAILURE);
102                         }
103                 }
104         }
105         if (argc == 1)
106         {
107                 fprintf(stderr, "Usage: stratego [options] red blue\n");
108                 fprintf(stderr, "       stratego --help\n");
109                 exit(EXIT_SUCCESS);
110                 
111         }
112         
113         Game game(red, blue, graphics, timeout, allowIllegal, log, reveal);
114         
115         
116         if (!game.Setup(red, blue))
117         {
118                 fprintf(stdout, "NONE %d\n",game.TurnCount());
119                 exit(EXIT_SUCCESS);
120         }
121
122         MovementResult result = game.Play();
123         game.PrintEndMessage(result);
124
125         Piece::Colour winner = game.Turn();
126         if (Board::LegalResult(result))
127         {
128                 if (winner == Piece::BOTH)
129                         winner = Piece::NONE;
130                 else
131                 {
132                         if (winner == Piece::RED)
133                                 winner = Piece::BLUE;
134                         else
135                                 winner = Piece::RED;
136                 }
137         }
138         
139
140         switch (winner)
141         {
142                 case Piece::RED:
143                         fprintf(stdout, "%s RED %d\n", red,game.TurnCount());   
144                         break;
145                 case Piece::BLUE:
146                         fprintf(stdout, "%s BLUE %d\n", blue,game.TurnCount()); 
147                         break;
148                 case Piece::BOTH:
149                         fprintf(stdout, "DRAW %d\n",game.TurnCount());  
150                         break;
151                 case Piece::NONE:
152                         fprintf(stdout, "NONE %d\n",game.TurnCount());  
153                         break;
154
155         }
156
157         
158         
159
160
161
162         exit(EXIT_SUCCESS);
163         
164         return 0;
165 }
166
167

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