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

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