e3e435e6c5c764674617554d7e48e02c1e2d5b4a
[progcomp2012.git] / manager / main.cpp
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "common.h"
5
6 #include "controller.h"
7 #include "stratego.h"
8
9 using namespace std;
10
11
12
13 #define theBoard Board::theBoard
14
15 #ifdef GRAPHICS
16         bool CheckForQuitWhilstWaiting(int wait);
17 #endif //GRAPHICS
18
19 Controller * red;
20 Controller * blue;
21 Colour turn;
22
23 void cleanup();
24
25 void BrokenPipe(int sig);
26
27 int main(int argc, char ** argv)
28 {
29         assert(argc == 3);
30         
31         
32         for (int y = 4; y < 6; ++y)
33         {
34                 for (int x = 2; x < 4; ++x)
35                 {
36                         theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
37                 }
38                 for (int x = 6; x < 8; ++x)
39                 {
40                         theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
41                 }
42         }
43         
44
45         red = new Controller(Piece::RED, argv[1]);
46         blue = new Controller(Piece::BLUE, argv[2]);
47         atexit(cleanup);
48         signal(SIGPIPE, BrokenPipe);
49
50         MovementResult redSetup = red->Setup(argv[2]);
51         MovementResult blueSetup = blue->Setup(argv[1]);
52         if (redSetup != MovementResult::OK)
53         {
54                 fprintf(stderr, "Blue wins by DEFAULT!\n");
55                 red->SendMessage("ILLEGAL");
56                 blue->SendMessage("DEFAULT");
57                 exit(EXIT_SUCCESS);
58         }
59         if (blueSetup != MovementResult::OK)
60         {
61                 fprintf(stderr, "Red wins by DEFAULT!\n");
62                 red->SendMessage("DEFAULT");
63                 blue->SendMessage("ILLEGAL");
64                 exit(EXIT_SUCCESS);
65         }
66
67         MovementResult result(MovementResult::OK);
68         system("clear");
69         int count = 1;
70
71         #ifdef GRAPHICS
72                 if (!Graphics::Initialised())
73                         Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
74                 
75         #endif //GRAPHICS
76
77         string buffer;
78
79         red->SendMessage("START");
80         turn = Piece::RED;
81         while (Board::LegalResult(result))
82         {
83
84                 
85                 turn = Piece::RED;
86                 fprintf(stderr, "%d RED: ", count);
87                 result = red->MakeMove(buffer);
88                 red->SendMessage(buffer);
89                 blue->SendMessage(buffer);
90                 fprintf(stderr, "%s\n", buffer.c_str());
91                 if (!Board::LegalResult(result))
92                         break;
93                 #ifdef GRAPHICS
94                         Board::theBoard.Draw();
95                         if (CheckForQuitWhilstWaiting(0.5))
96                         {
97                                 red->SendMessage("QUIT");
98                                 blue->SendMessage("QUIT");
99                                 exit(EXIT_SUCCESS);
100                         }
101                 #endif //GRAPHICS
102                 
103                 turn = Piece::BLUE;
104                 fprintf(stderr, "%d BLU: ", count);
105                 result = blue->MakeMove(buffer);
106                 blue->SendMessage(buffer);
107                 red->SendMessage(buffer);
108                 fprintf(stderr, "%s\n", buffer.c_str());
109
110                 if (!Board::LegalResult(result))
111                         break;
112
113                 
114
115                 #ifdef GRAPHICS
116                         Board::theBoard.Draw();
117                         if (CheckForQuitWhilstWaiting(0.5))
118                         {
119                                 red->SendMessage("QUIT");
120                                 blue->SendMessage("QUIT");
121                                 exit(EXIT_SUCCESS);
122                         }
123                 #else
124                         Board::theBoard.Print(stderr);
125                         sleep(1);
126                         system("clear");
127                 #endif //GRAPHICS
128                 
129                 ++count;
130         }
131
132         printf("Final board state\n");
133         #ifdef GRAPHICS
134                         Board::theBoard.Draw();
135
136                         if (CheckForQuitWhilstWaiting(4))
137                         {
138                                 red->SendMessage("QUIT");
139                                 blue->SendMessage("QUIT");
140                                 exit(EXIT_SUCCESS);
141                         }
142
143         #else
144                 Board::theBoard.Print(stderr);
145         #endif //GRAPHICS
146         sleep(2);
147
148
149         if (turn == Piece::RED)
150         {
151                 fprintf(stderr,"Game ends on RED's turn - REASON: ");   
152         }
153         else if (turn == Piece::BLUE)
154         {
155                 fprintf(stderr,"Game ends on BLUE's turn - REASON: ");
156         }
157         else
158         {
159                 fprintf(stderr,"Game ends on ERROR's turn - REASON: ");
160                         
161         }
162         switch (result.type)
163         {
164                 case MovementResult::NO_BOARD:
165                         fprintf(stderr,"Board does not exit?!\n");
166                         break;
167                 case MovementResult::INVALID_POSITION:
168                         fprintf(stderr,"Coords outside board\n");
169                         break;
170                 case MovementResult::NO_SELECTION:
171                         fprintf(stderr,"Move does not select a piece\n");
172                         break;
173                 case MovementResult::NOT_YOUR_UNIT:
174                         fprintf(stderr,"Selected piece belongs to other player\n");
175                         break;
176                 case MovementResult::IMMOBILE_UNIT:
177                         fprintf(stderr,"Selected piece is not mobile (FLAG or BOMB)\n");
178                         break;
179                 case MovementResult::INVALID_DIRECTION:
180                         fprintf(stderr,"Selected unit cannot move that way\n");
181                         break;
182                 case MovementResult::POSITION_FULL:
183                         fprintf(stderr,"Attempted move into square occupied by allied piece\n");
184                         break;
185                 case MovementResult::VICTORY:
186                         fprintf(stderr,"Captured the flag\n");
187                         break;
188                 case MovementResult::BAD_RESPONSE:
189                         fprintf(stderr,"Unintelligable response\n");
190                         break;
191                 case MovementResult::NO_MOVE:
192                         fprintf(stderr,"Did not make a move (may have exited)\n");
193                         break;
194         }
195
196
197
198         exit(EXIT_SUCCESS);
199         
200         return 0;
201 }
202
203 #ifdef GRAPHICS
204
205 bool CheckForQuitWhilstWaiting(int wait)
206 {
207
208
209         TimerThread timer(wait*1000000); //Wait in seconds
210         timer.Start();
211         while (!timer.Finished())
212         {
213                 SDL_Event  event;
214                 while (SDL_PollEvent(&event))
215                 {
216                         switch (event.type)
217                         {
218                                 case SDL_QUIT:
219                                         timer.Stop();
220                                         return true;
221                                         break;
222                         }
223                 }
224         }
225         timer.Stop();
226         return false;
227 }
228
229 void cleanup()
230 {
231         delete red;
232         delete blue;
233 }
234
235 void BrokenPipe(int sig)
236 {
237         if (turn == Piece::RED)
238         {
239                 fprintf(stderr,"Game ends on RED's turn - REASON: Broken pipe\n");
240                 blue->SendMessage("DEFAULT");   
241         }
242         else if (turn == Piece::BLUE)
243         {
244                 fprintf(stderr,"Game ends on BLUE's turn - REASON: Broken pipe\n");
245                 red->SendMessage("DEFAULT");
246         }
247         else
248         {
249                 fprintf(stderr,"Game ends on ERROR's turn - REASON: Broken pipe\n");
250                         
251         }
252                         Board::theBoard.Draw();
253                         while (true)
254                         {
255                                 if (CheckForQuitWhilstWaiting(4000))
256                                 {
257                                 red->SendMessage("QUIT");
258                                 blue->SendMessage("QUIT");
259                                 exit(EXIT_SUCCESS);
260                                 }
261                         }
262         exit(EXIT_SUCCESS);
263 }
264         
265 #endif //GRAPHICS

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