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

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