main.cpp 4.85 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
#include <stdlib.h>
#include <stdio.h>






9
#include "game.h"
10

11
using namespace std;
12

13
14
15
16
void CreateGame(int argc, char ** argv);
void DestroyGame();
void PrintResults(const MovementResult & result);

Sam Moore's avatar
Sam Moore committed
17
18
int main(int argc, char ** argv)
{
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	


	if (argc == 1)
	{
		fprintf(stderr, "Usage: stratego [options] red blue\n");
		fprintf(stderr, "       stratego --help\n");
		exit(EXIT_SUCCESS);
		
	}
	CreateGame(argc, argv);
	if (Game::theGame == NULL)
	{
		fprintf(stderr, "ERROR: Couldn't create a game!\n");
		exit(EXIT_FAILURE);
	}

	MovementResult result = Game::theGame->Play();
	Game::theGame->PrintEndMessage(result);
	PrintResults(result);

	exit(EXIT_SUCCESS);
	return 0;
}
43

44
45
void CreateGame(int argc, char ** argv)
{
46
	char * red = NULL; char * blue = NULL; double timeout = 0.00001; bool graphics = false; bool allowIllegal = false; FILE * log = NULL;
47
	Piece::Colour reveal = Piece::BOTH; char * inputFile = NULL; int maxTurns = 5000; bool printBoard = false;
48
	for (int ii=1; ii < argc; ++ii)
Sam Moore's avatar
Sam Moore committed
49
	{
50
		if (argv[ii][0] == '-')
Sam Moore's avatar
Sam Moore committed
51
		{
52
53
54
55
56
57
58
59
60
61
62
63
			switch (argv[ii][1])
			{
				case 't':
					if (argc - ii <= 1)
					{
						fprintf(stderr, "Expected timeout value after -t switch!\n");
						exit(EXIT_FAILURE);
					}
					timeout = atof(argv[ii+1]);
					++ii;
					break;
				case 'g':
64
65
66
67
					graphics = !graphics;
					break;
				case 'p':
					printBoard = !printBoard;
68
69
					break;
				case 'i':
70
					allowIllegal = !allowIllegal;
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
					break;

				case 'o':
					if (argc - ii <= 1)
					{
						fprintf(stderr, "Expected filename or \"stdout\" after -o switch!\n");
						exit(EXIT_FAILURE);
					}
					if (log != NULL)
					{
						fprintf(stderr, "Expected at most ONE -o switch!\n");
						exit(EXIT_FAILURE);
					}
					if (strcmp(argv[ii+1], "stdout") == 0)
						log = stdout;
					else
						log = fopen(argv[ii+1], "w");
					setbuf(log, NULL);
				
					++ii;
					break;	

				case 'r':
					if (reveal == Piece::BOTH)
						reveal = Piece::BLUE;
					else
						reveal = Piece::NONE;
					break;			
				case 'b':
					if (reveal == Piece::BOTH)
						reveal = Piece::RED;
					else
						reveal = Piece::NONE;
					break;
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
				case 'm':
					if (argc - ii <= 1)
					{
						fprintf(stderr, "Expected max_turns value after -m switch!\n");
						exit(EXIT_FAILURE);
					}
					if (strcmp(argv[ii+1], "inf"))
						maxTurns = -1;
					else
						maxTurns = atoi(argv[ii+1]);
					++ii;
					break;
				case 'f':
					if (argc - ii <= 1)
					{
						fprintf(stderr, "Expected filename after -f switch!\n");
						exit(EXIT_FAILURE);
					}
					if (log != NULL)
					{
						fprintf(stderr, "Expected at most ONE -f switch!\n");
						exit(EXIT_FAILURE);
					}
					red = (char*)("file");
					blue = (char*)("file");
					inputFile = argv[ii+1];
					++ii;
					break;
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
				case 'h':
					system("clear");	
					system("less manual.txt");
					exit(EXIT_SUCCESS);
					break;
				case '-':
					if (strcmp(argv[ii]+2, "help") == 0)
					{
						system("clear");	
						system("less manual.txt");
						exit(EXIT_SUCCESS);
					}
					else
					{
						fprintf(stderr, "Unrecognised switch \"%s\"...\n", argv[ii]);
						exit(EXIT_FAILURE);
					}
			}
			
Sam Moore's avatar
Sam Moore committed
152
		}
153
		else
Sam Moore's avatar
Sam Moore committed
154
		{
155
156
157
158
159
160
161
162
163
			if (red == NULL)
				red = argv[ii];
			else if (blue == NULL)
				blue = argv[ii];
			else
			{
				fprintf(stderr, "Unexpected argument \"%s\"...\n", argv[ii]);
				exit(EXIT_FAILURE);
			}
Sam Moore's avatar
Sam Moore committed
164
165
		}
	}
166
167

	if (inputFile == NULL)
Sam Moore's avatar
Sam Moore committed
168
	{
169
		Game::theGame = new Game(red,blue, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
Sam Moore's avatar
Sam Moore committed
170
	}
171
172
173
174
175
	else
	{
		Game::theGame = new Game(inputFile, graphics, timeout, allowIllegal,log, reveal,maxTurns, printBoard);
	}
	if (!Game::theGame->Setup(red, blue))
Sam Moore's avatar
Sam Moore committed
176
	{
177
		fprintf(stdout, "NONE %d\n",Game::theGame->TurnCount());
Sam Moore's avatar
Sam Moore committed
178
179
		exit(EXIT_SUCCESS);
	}
180
181
	
	atexit(DestroyGame);
Sam Moore's avatar
Sam Moore committed
182

183
}
Sam Moore's avatar
Sam Moore committed
184

185
186
187
void PrintResults(const MovementResult & result)
{
	Piece::Colour winner = Game::theGame->Turn();
188
	if (Board::LegalResult(result))
Sam Moore's avatar
Sam Moore committed
189
	{
190
191
192
193
194
195
196
197
198
		if (winner == Piece::BOTH)
			winner = Piece::NONE;
		else
		{
			if (winner == Piece::RED)
				winner = Piece::BLUE;
			else
				winner = Piece::RED;
		}
Sam Moore's avatar
Sam Moore committed
199
	}
200
	
Sam Moore's avatar
Sam Moore committed
201

202
	switch (winner)
Sam Moore's avatar
Sam Moore committed
203
	{
204
		case Piece::RED:
205
206
			fprintf(stdout, "%s RED %d\n", Game::theGame->red->name.c_str(),Game::theGame->TurnCount());	
			Game::theGame->logMessage("%s RED %d\n", Game::theGame->red->name.c_str(),Game::theGame->TurnCount());
Sam Moore's avatar
Sam Moore committed
207
			break;
208
		case Piece::BLUE:
209
210
			fprintf(stdout, "%s BLUE %d\n", Game::theGame->blue->name.c_str(),Game::theGame->TurnCount());	
			Game::theGame->logMessage("%s BLUE %d\n", Game::theGame->blue->name.c_str(),Game::theGame->TurnCount());
Sam Moore's avatar
Sam Moore committed
211
			break;
212
		case Piece::BOTH:
213
214
			fprintf(stdout, "DRAW %d\n",Game::theGame->TurnCount());
			Game::theGame->logMessage("DRAW %d\n",Game::theGame->TurnCount());	
Sam Moore's avatar
Sam Moore committed
215
			break;
216
		case Piece::NONE:
217
218
			fprintf(stdout, "NONE %d\n",Game::theGame->TurnCount());	
			Game::theGame->logMessage("NONE %d\n",Game::theGame->TurnCount());
Sam Moore's avatar
Sam Moore committed
219
			break;
220

Sam Moore's avatar
Sam Moore committed
221
222
223
	}
}

224
225
226
227
228
void DestroyGame()
{
	delete Game::theGame;
	Game::theGame = NULL;
}