stratego.cpp 11.9 KB
Newer Older
1

Sam Moore's avatar
Sam Moore committed
2
3
4
5
6
7
8
9

#include "stratego.h"

using namespace std;

/**
 * Static variables
 */
10

Sam Moore's avatar
Sam Moore committed
11
//nothing, boulder, flag, spy, scout, miner, sergeant, lietenant, captain, major, colonel, general, marshal, bomb, error
12
char  Piece::tokens[] = {'.','*','F','s','9','8','7','6','5','4','3','2','1','B','?'};
Sam Moore's avatar
Sam Moore committed
13
14
15
16
int Piece::maxUnits[] = {0,0,1,1,8,5,4,4,4,3,2,1,1,6,0};



17
18
19

Piece::TextureManager Piece::textures;

Sam Moore's avatar
Sam Moore committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43



Piece::TextureManager::~TextureManager()
{
	Array<Texture*>::Iterator i(*this);
	while (i.Good())
	{
		delete (*i);
		++i;
	}
}

Texture & Piece::TextureManager::operator[](const LUint & at)
{
	while (Array<Texture*>::Size() <= at)
	{
		char buffer[BUFSIZ];
		sprintf(buffer, "images/piece%lu.bmp", Array<Texture*>::Size());
		Array<Texture*>::Add(new Texture(buffer, false));
		
	}
	return *(Array<Texture*>::operator[](at));
}
44

Sam Moore's avatar
Sam Moore committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

/**
 * Gets the type of a piece, based off a character token
 * @param fromToken - character identifying the piece
 * @returns The type of the piece
 */
Piece::Type Piece::GetType(char fromToken)
{
	for (int ii=0; ii <= (int)(Piece::BOMB); ++ii)
	{
		if (tokens[ii] == fromToken)
		{
			return Type(Piece::NOTHING + ii);
		}
	}
	return Piece::BOULDER;
}

/**
 * Construct a new, empty board
 * @param newWidth - the width of the board
 * @param newHeight - the height of the board
 */
68
Board::Board(int newWidth, int newHeight) : winner(Piece::NONE), width(newWidth), height(newHeight), board(NULL), pieces()
Sam Moore's avatar
Sam Moore committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
	board = new Piece**[width];
	for (int x=0; x < width; ++x)
	{
		board[x] = new Piece*[height];
		for (int y=0; y < height; ++y)
			board[x][y] = NULL;
	}
}

/**
 * Cleanup a board
 */
Board::~Board()
{
	for (int x=0; x < width; ++x)
	{
		for (int y=0; y < height; ++y)
			delete board[x][y];
		delete [] board[x];
	}
}

/**
 * Print textual representation of the board to a stream
 * @param stream - the stream to print information to
95
 * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
Sam Moore's avatar
Sam Moore committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 */
void Board::Print(FILE * stream, const Piece::Colour & reveal)
{
	for (int y=0; y < height; ++y)
	{
		for (int x=0; x < width; ++x)
		{
			Piece * piece = board[x][y];
			if (piece == NULL)
			{
				fprintf(stream, ".");
			}
			else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
			{
110

Sam Moore's avatar
Sam Moore committed
111
				fprintf(stream, "%c", Piece::tokens[piece->type]);
112
113


Sam Moore's avatar
Sam Moore committed
114
115
116
117
118
119
			}
			else
			{
				switch (piece->colour)
				{
					case Piece::RED:
120
					case Piece::BLUE:
Sam Moore's avatar
Sam Moore committed
121
122
						fprintf(stream, "#");
						break;
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
					case Piece::NONE:
						fprintf(stream, "+");
						break;
					case Piece::BOTH:
						fprintf(stream, "$");
						break;
				}
			}
		}
		fprintf(stream, "\n");
	}
	
}

/**
 * Print textual representation of the board to a stream
 * @param stream - the stream to print information to
 * @param reveal - Pieces matching this colour will have their identify revealed, other pieces will be shown as '#'
 */
void Board::PrintPretty(FILE * stream, const Piece::Colour & reveal)
{
	for (int y=0; y < height; ++y)
	{
		for (int x=0; x < width; ++x)
		{
			Piece * piece = board[x][y];
			if (piece == NULL)
			{
				fprintf(stream, ".");
			}
			else if (piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
			{
				switch (piece->colour)	
				{
					case Piece::RED:
						fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,40);
						break;
Sam Moore's avatar
Sam Moore committed
160
					case Piece::BLUE:
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
						fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,40);
						break;
					default:
						break;
				}
				fprintf(stream, "%c", Piece::tokens[piece->type]);

			}
			else
			{
				switch (piece->colour)
				{
					case Piece::RED:
						fprintf(stream, "%c[%d;%d;%dm",0x1B,1,31,41);

						break;
					case Piece::BLUE:
						fprintf(stream, "%c[%d;%d;%dm",0x1B,1,34,44);
Sam Moore's avatar
Sam Moore committed
179
180
						break;
					case Piece::NONE:
181
						fprintf(stream, "%c[%d;%d;%dm",0x1B,1,37,47);
Sam Moore's avatar
Sam Moore committed
182
183
						break;
					case Piece::BOTH:
184
185
						//Should never see this
						fprintf(stream, "%c[%d;%d;%dm",0x1B,1,33,43);
Sam Moore's avatar
Sam Moore committed
186
						break;
187
188
189
190

				}	
				fprintf(stream, "#");
				
Sam Moore's avatar
Sam Moore committed
191
			}
192
			fprintf(stream, "%c[%d;%d;%dm",0x1B,0,7,0);
Sam Moore's avatar
Sam Moore committed
193
194
195
196
197
198
199
		}
		fprintf(stream, "\n");
	}
	
}


200

Sam Moore's avatar
Sam Moore committed
201
202
203
204
/**
 * Draw the board state to graphics
 * @param reveal - Pieces matching this colour will be revealed. All others will be shown as blank coloured squares.
 */
205
void Board::Draw(const Piece::Colour & reveal, bool showRevealed)
Sam Moore's avatar
Sam Moore committed
206
207
208
{
	if (!Graphics::Initialised())
	{
209
210
211
		fprintf(stderr, "ERROR - Board::Draw called whilst graphics disabled!!!\n");
		exit(EXIT_FAILURE);
		
Sam Moore's avatar
Sam Moore committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	}

	Graphics::ClearScreen();
	
	for (int y=0; y < height; ++y)
	{
		for (int x=0; x < width; ++x)
		{
			Piece * piece = board[x][y];
			if (piece == NULL)
			{
				//Don't display anything

			}
226
227
			else if ((piece->colour != Piece::NONE && (piece->colour == reveal || reveal == Piece::BOTH))
					|| (piece->beenRevealed && showRevealed))
Sam Moore's avatar
Sam Moore committed
228
229
230
231
232
233
234
235
236
237
			{
				//Display the piece
				Piece::textures[(int)(piece->type)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
				
			}
			else
			{
				switch (piece->colour)
				{
					case Piece::RED:
238
						Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
Sam Moore's avatar
Sam Moore committed
239
240
						break;
					case Piece::BLUE:
241
						Piece::textures[(int)(Piece::NOTHING)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
Sam Moore's avatar
Sam Moore committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
						break;
					case Piece::NONE:
						Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
						break;
					case Piece::BOTH:
						Piece::textures[(int)(Piece::BOULDER)].DrawColour(x*32,y*32,0,1, Piece::GetGraphicsColour(piece->colour));
						break;
				}
			}
		}
		
	}
	Graphics::UpdateScreen();
	
}

/**
 * Adds a piece to the board
 * @param x - x-coord to place the piece at, starting at zero, must be less than board width
 * @param y - y-coord to place the piece at, starting at zero, must be less than board height
 * @param newType - the Type of the piece
 * @param newColour - the Colour of the piece
 * @returns true if and only if the piece could be successfully added.
 */
bool Board::AddPiece(int x, int y, const Piece::Type & newType, const Piece::Colour & newColour)
{
	if (board == NULL || x < 0 || y < 0 || x >= width || y >= width || board[x][y] != NULL)
		return false;

	Piece * piece = new Piece(newType, newColour);
	board[x][y] = piece;
273
274

	pieces.push_back(piece);
Sam Moore's avatar
Sam Moore committed
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
	return true;
}

/**
 * Gets a pointer to a piece at a board location
 * UNUSED
 * @param x - x-coord of the piece
 * @param y - y-coord of the piece
 * @returns pointer to the piece, or NULL if the board location was empty
 * @throws error if board is null or coords are invalid
 */
Piece * Board::GetPiece(int x, int y)
{
	assert(board != NULL);
	assert(x >= 0 && x < width && y >= 0 && y < height);
	return board[x][y];
}

/**
 * Moves a piece at a specified position in the specified direction, handles combat if necessary
 * @param x - x-coord of the piece
 * @param y - y-coord of the piece
 * @param direction - Direction in which to move (UP, DOWN, LEFT or RIGHT)
 * @param colour - Colour which the piece must match for the move to be valid
 * @returns A MovementResult which indicates the result of the move - OK is good, VICTORY means that a flag was captured, anything else is an error
 */
301
MovementResult Board::MovePiece(int x, int y, const Direction & direction, int multiplier,const Piece::Colour & colour)
Sam Moore's avatar
Sam Moore committed
302
303
304
{
	if (board == NULL) 
	{
305
		return MovementResult(MovementResult::NO_BOARD);
Sam Moore's avatar
Sam Moore committed
306
307
308
	}
	if (!(x >= 0 && x < width && y >= 0 && y < height)) 
	{
309
		return MovementResult(MovementResult::INVALID_POSITION);
Sam Moore's avatar
Sam Moore committed
310
311
312
313
	}
	Piece * target = board[x][y];
	if (target == NULL) 
	{
314
		return MovementResult(MovementResult::NO_SELECTION);
Sam Moore's avatar
Sam Moore committed
315
316
317
	}
	if (!(colour == Piece::NONE || target->colour == colour)) 
	{
318
		return MovementResult(MovementResult::NOT_YOUR_UNIT);
Sam Moore's avatar
Sam Moore committed
319
320
321
	}
	if (target->type == Piece::FLAG || target->type == Piece::BOMB || target->type == Piece::BOULDER) 
	{
322
		return MovementResult(MovementResult::IMMOBILE_UNIT);
Sam Moore's avatar
Sam Moore committed
323
324
325
	}
	if (multiplier > 1 && target->type != Piece::SCOUT)
	{
326
		return MovementResult(MovementResult::INVALID_DIRECTION); //Can only move a scout multiple times.
Sam Moore's avatar
Sam Moore committed
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
	}
	int x2 = x; int y2 = y;

	for (int ii=0; ii < multiplier; ++ii)
	{
		switch (direction)
		{
			case UP:
				--y2;
				break;
			case DOWN:
				++y2;
				break;
			case LEFT:
				--x2;
				break;
			case RIGHT:
				++x2;
				break;
		}
		if (!(x2 >= 0 && x2 < width && y2 >= 0 && y2 < height)) 
		{
349
			return MovementResult(MovementResult::INVALID_DIRECTION);
Sam Moore's avatar
Sam Moore committed
350
351
352
		}
		if (ii < multiplier-1 && board[x2][y2] != NULL)
		{
353
			return MovementResult(MovementResult::POSITION_FULL);
Sam Moore's avatar
Sam Moore committed
354
355
356
357
358
359
360
361
362
363
		}
	}
	Piece * defender = board[x2][y2];
	if (defender == NULL)
	{
		board[x][y] = NULL;
		board[x2][y2] = target;
	}
	else if (defender->colour != target->colour)
	{
364
365
366
		defender->beenRevealed = true;
		target->beenRevealed = true;

367
368
369
		Piece::Type defenderType = defender->type;
		Piece::Type attackerType = target->type;

Sam Moore's avatar
Sam Moore committed
370
371
		if (defender->colour == Piece::NONE) 
		{
372
			return MovementResult(MovementResult::POSITION_FULL);
Sam Moore's avatar
Sam Moore committed
373
374
375
376
		}
		if (defender->type == Piece::FLAG)
		{
			winner = target->colour;
377
			return MovementResult(MovementResult::VICTORY);
Sam Moore's avatar
Sam Moore committed
378
379
380
381
382
		}
		else if (defender->type == Piece::BOMB)
		{
			if (target->type == Piece::MINER)
			{
383
				RemovePiece(defender);
Sam Moore's avatar
Sam Moore committed
384
385
386
				delete defender;
				board[x][y] = NULL;
				board[x2][y2] = target;
387
				return MovementResult(MovementResult::KILLS, attackerType, defenderType);
Sam Moore's avatar
Sam Moore committed
388
389
390
			}
			else
			{
391
392
				RemovePiece(defender);
				RemovePiece(target);
Sam Moore's avatar
Sam Moore committed
393
394
395
396
				delete defender;
				delete target;
				board[x][y] = NULL;
				board[x2][y2] = NULL;
397
				return MovementResult(MovementResult::BOTH_DIE, attackerType, defenderType);
Sam Moore's avatar
Sam Moore committed
398
399
400
401
			}
		}
		else if (defender->type == Piece::MARSHAL && target->type == Piece::SPY)
		{
402
			RemovePiece(defender);
Sam Moore's avatar
Sam Moore committed
403
404
405
			delete defender;
			board[x][y] = NULL;
			board[x2][y2] = target;
406
			return MovementResult(MovementResult::KILLS, attackerType, defenderType);
Sam Moore's avatar
Sam Moore committed
407
408
409
		}
		else if (target->operator > (*defender))
		{
410
			RemovePiece(defender);
Sam Moore's avatar
Sam Moore committed
411
412
413
			delete defender;
			board[x][y] = NULL;
			board[x2][y2] = target;
414
			return MovementResult(MovementResult::KILLS, attackerType, defenderType);
Sam Moore's avatar
Sam Moore committed
415
416
417
		}
		else if (target->operator==(*defender) && rand() % 2 == 0)
		{
418
			RemovePiece(defender);
Sam Moore's avatar
Sam Moore committed
419
420
421
			delete defender;
			board[x][y] = NULL;
			board[x2][y2] = target;	
422
			return MovementResult(MovementResult::KILLS, attackerType, defenderType);
Sam Moore's avatar
Sam Moore committed
423
424
425
		}
		else
		{
426
			RemovePiece(target);
Sam Moore's avatar
Sam Moore committed
427
428
			delete target;
			board[x][y] = NULL;
429
			return MovementResult(MovementResult::DIES, attackerType, defenderType);
Sam Moore's avatar
Sam Moore committed
430
431
432
433
		}
	}
	else
	{
434
		return MovementResult(MovementResult::POSITION_FULL);
Sam Moore's avatar
Sam Moore committed
435
	}
436
	return MovementResult(MovementResult::OK);
Sam Moore's avatar
Sam Moore committed
437
438
}	

439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/**
 * Removes a piece from the board
 * @param piece The piece to remove
 * @returns true iff the piece actually existed
 */
bool Board::RemovePiece(Piece * piece)
{
	bool result = false;
	for (int x = 0; x < width; ++x)
	{
		for (int y = 0; y < height; ++y)	
		{
			if (board[x][y] == piece)
			{
				result = true;
				board[x][y] = NULL;
			}
		}
	}

	vector<Piece*>::iterator i = pieces.begin();
	while (i != pieces.end())
	{
		if ((*i) == piece)
		{
			i = pieces.erase(i);
			result = true;
			continue;
		}
		++i;
	}
	return result;
}

/**
 * Returns the total value of pieces belonging to colour
 * @param colour the colour
 * @returns the total value of pieces belonging to colour.
 *	(Redundant repetition <3)
 */
int Board::TotalPieceValue(const Piece::Colour & colour) const
{
	int result = 0;
	for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
	{
		if ((*i)->colour == colour || colour == Piece::BOTH)
		{
			result += (*i)->PieceValue();
		}
	}
	return result;
}

/**
 * Returns the total number of mobile pieces belonging to colour
 * @param colour the colour
 * @returns the total value of mobile pieces belonging to colour.
 *	(Redundant repetition <3)
 */
int Board::MobilePieces(const Piece::Colour & colour) const
{
	int result = 0;
	for (vector<Piece*>::const_iterator i = pieces.begin(); i != pieces.end(); ++i)
	{
		if ((*i)->colour == colour || colour == Piece::BOTH)
		{
			if ((*i)->type <= Piece::MARSHAL && (*i)->type >= Piece::SPY)
				result++;
		}
	}
	return result;
}
Sam Moore's avatar
Sam Moore committed
511
512