main.cpp 5.06 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdlib.h>
#include <stdio.h>

#include "common.h"

#include "controller.h"
#include "stratego.h"

using namespace std;



#define theBoard Board::theBoard

#ifdef GRAPHICS
	bool CheckForQuitWhilstWaiting(int wait);
#endif //GRAPHICS

Controller * red;
Controller * blue;
21
Colour turn;
Sam Moore's avatar
Sam Moore committed
22
23

void cleanup();
24
25
26

void BrokenPipe(int sig);

Sam Moore's avatar
Sam Moore committed
27
28
29
30
int main(int argc, char ** argv)
{
	assert(argc == 3);
	
Sam Moore's avatar
Sam Moore committed
31
32
	
	for (int y = 4; y < 6; ++y)
Sam Moore's avatar
Sam Moore committed
33
	{
Sam Moore's avatar
Sam Moore committed
34
		for (int x = 2; x < 4; ++x)
Sam Moore's avatar
Sam Moore committed
35
36
37
		{
			theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
		}
Sam Moore's avatar
Sam Moore committed
38
		for (int x = 6; x < 8; ++x)
Sam Moore's avatar
Sam Moore committed
39
40
41
42
		{
			theBoard.AddPiece(x,y,Piece::BOULDER, Piece::NONE);
		}
	}
Sam Moore's avatar
Sam Moore committed
43
	
Sam Moore's avatar
Sam Moore committed
44
45
46
47

	red = new Controller(Piece::RED, argv[1]);
	blue = new Controller(Piece::BLUE, argv[2]);
	atexit(cleanup);
48
	signal(SIGPIPE, BrokenPipe);
Sam Moore's avatar
Sam Moore committed
49

50
51
52
	MovementResult redSetup = red->Setup(argv[2]);
	MovementResult blueSetup = blue->Setup(argv[1]);
	if (redSetup != MovementResult::OK)
Sam Moore's avatar
Sam Moore committed
53
54
55
56
57
58
	{
		fprintf(stderr, "Blue wins by DEFAULT!\n");
		red->SendMessage("ILLEGAL");
		blue->SendMessage("DEFAULT");
		exit(EXIT_SUCCESS);
	}
59
	if (blueSetup != MovementResult::OK)
Sam Moore's avatar
Sam Moore committed
60
61
62
63
64
65
66
	{
		fprintf(stderr, "Red wins by DEFAULT!\n");
		red->SendMessage("DEFAULT");
		blue->SendMessage("ILLEGAL");
		exit(EXIT_SUCCESS);
	}

67
	MovementResult result(MovementResult::OK);
Sam Moore's avatar
Sam Moore committed
68
69
70
71
72
73
74
75
76
77
78
79
	system("clear");
	int count = 1;

	#ifdef GRAPHICS
		if (!Graphics::Initialised())
			Graphics::Initialise("Stratego", theBoard.Width()*32, theBoard.Height()*32);
		
	#endif //GRAPHICS

	string buffer;

	red->SendMessage("START");
80
	turn = Piece::RED;
Sam Moore's avatar
Sam Moore committed
81
82
83
	while (Board::LegalResult(result))
	{

84
		
Sam Moore's avatar
Sam Moore committed
85
		turn = Piece::RED;
86
		fprintf(stderr, "%d RED: ", count);
Sam Moore's avatar
Sam Moore committed
87
88
89
		result = red->MakeMove(buffer);
		red->SendMessage(buffer);
		blue->SendMessage(buffer);
90
		fprintf(stderr, "%s\n", buffer.c_str());
Sam Moore's avatar
Sam Moore committed
91
92
93
94
		if (!Board::LegalResult(result))
			break;
		#ifdef GRAPHICS
			Board::theBoard.Draw();
Sam Moore's avatar
Sam Moore committed
95
			if (CheckForQuitWhilstWaiting(0.5))
Sam Moore's avatar
Sam Moore committed
96
97
98
99
100
101
			{
				red->SendMessage("QUIT");
				blue->SendMessage("QUIT");
				exit(EXIT_SUCCESS);
			}
		#endif //GRAPHICS
102
		
Sam Moore's avatar
Sam Moore committed
103
		turn = Piece::BLUE;
104
		fprintf(stderr, "%d BLU: ", count);
Sam Moore's avatar
Sam Moore committed
105
106
107
		result = blue->MakeMove(buffer);
		blue->SendMessage(buffer);
		red->SendMessage(buffer);
108
		fprintf(stderr, "%s\n", buffer.c_str());
Sam Moore's avatar
Sam Moore committed
109
110
111
112
113
114
115
116

		if (!Board::LegalResult(result))
			break;

		

		#ifdef GRAPHICS
			Board::theBoard.Draw();
Sam Moore's avatar
Sam Moore committed
117
			if (CheckForQuitWhilstWaiting(0.5))
Sam Moore's avatar
Sam Moore committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
			{
				red->SendMessage("QUIT");
				blue->SendMessage("QUIT");
				exit(EXIT_SUCCESS);
			}
		#else
			Board::theBoard.Print(stderr);
			sleep(1);
			system("clear");
		#endif //GRAPHICS
		
		++count;
	}

	printf("Final board state\n");
	#ifdef GRAPHICS
			Board::theBoard.Draw();
Sam Moore's avatar
Sam Moore committed
135

Sam Moore's avatar
Sam Moore committed
136
137
138
139
140
141
			if (CheckForQuitWhilstWaiting(4))
			{
				red->SendMessage("QUIT");
				blue->SendMessage("QUIT");
				exit(EXIT_SUCCESS);
			}
Sam Moore's avatar
Sam Moore committed
142

Sam Moore's avatar
Sam Moore committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	#else
		Board::theBoard.Print(stderr);
	#endif //GRAPHICS
	sleep(2);


	if (turn == Piece::RED)
	{
		fprintf(stderr,"Game ends on RED's turn - REASON: ");	
	}
	else if (turn == Piece::BLUE)
	{
		fprintf(stderr,"Game ends on BLUE's turn - REASON: ");
	}
	else
	{
		fprintf(stderr,"Game ends on ERROR's turn - REASON: ");
			
	}
162
	switch (result.type)
Sam Moore's avatar
Sam Moore committed
163
	{
164
		case MovementResult::NO_BOARD:
Sam Moore's avatar
Sam Moore committed
165
166
			fprintf(stderr,"Board does not exit?!\n");
			break;
167
		case MovementResult::INVALID_POSITION:
Sam Moore's avatar
Sam Moore committed
168
169
			fprintf(stderr,"Coords outside board\n");
			break;
170
		case MovementResult::NO_SELECTION:
Sam Moore's avatar
Sam Moore committed
171
172
			fprintf(stderr,"Move does not select a piece\n");
			break;
173
		case MovementResult::NOT_YOUR_UNIT:
Sam Moore's avatar
Sam Moore committed
174
175
			fprintf(stderr,"Selected piece belongs to other player\n");
			break;
176
		case MovementResult::IMMOBILE_UNIT:
Sam Moore's avatar
Sam Moore committed
177
178
			fprintf(stderr,"Selected piece is not mobile (FLAG or BOMB)\n");
			break;
179
		case MovementResult::INVALID_DIRECTION:
Sam Moore's avatar
Sam Moore committed
180
181
			fprintf(stderr,"Selected unit cannot move that way\n");
			break;
182
		case MovementResult::POSITION_FULL:
Sam Moore's avatar
Sam Moore committed
183
184
			fprintf(stderr,"Attempted move into square occupied by allied piece\n");
			break;
185
		case MovementResult::VICTORY:
Sam Moore's avatar
Sam Moore committed
186
187
			fprintf(stderr,"Captured the flag\n");
			break;
188
		case MovementResult::BAD_RESPONSE:
Sam Moore's avatar
Sam Moore committed
189
190
			fprintf(stderr,"Unintelligable response\n");
			break;
191
		case MovementResult::NO_MOVE:
Sam Moore's avatar
Sam Moore committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
			fprintf(stderr,"Did not make a move (may have exited)\n");
			break;
	}



	exit(EXIT_SUCCESS);
	
	return 0;
}

#ifdef GRAPHICS

bool CheckForQuitWhilstWaiting(int wait)
{


	TimerThread timer(wait*1000000); //Wait in seconds
	timer.Start();
	while (!timer.Finished())
	{
		SDL_Event  event;
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
				case SDL_QUIT:
					timer.Stop();
					return true;
					break;
			}
		}
	}
	timer.Stop();
	return false;
}

void cleanup()
{
	delete red;
	delete blue;
}

235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
void BrokenPipe(int sig)
{
	if (turn == Piece::RED)
	{
		fprintf(stderr,"Game ends on RED's turn - REASON: Broken pipe\n");
		blue->SendMessage("DEFAULT");	
	}
	else if (turn == Piece::BLUE)
	{
		fprintf(stderr,"Game ends on BLUE's turn - REASON: Broken pipe\n");
		red->SendMessage("DEFAULT");
	}
	else
	{
		fprintf(stderr,"Game ends on ERROR's turn - REASON: Broken pipe\n");
			
	}
Sam Moore's avatar
Sam Moore committed
252
253
254
255
256
257
258
259
260
261
			Board::theBoard.Draw();
			while (true)
			{
				if (CheckForQuitWhilstWaiting(4000))
				{
				red->SendMessage("QUIT");
				blue->SendMessage("QUIT");
				exit(EXIT_SUCCESS);
				}
			}
262
263
264
	exit(EXIT_SUCCESS);
}
	
Sam Moore's avatar
Sam Moore committed
265
#endif //GRAPHICS