summaryrefslogtreecommitdiffstats
path: root/TrollTron.cpp
blob: a8af7d720c1b5663ba5871cc08960e71629436cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
95
96
97
98
99
100
101
102
103
104
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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// TrollTron.cpp : définit le point d'entrée pour l'application console.

#include <iostream>
#include <string.h>
#include <time.h>
#include "MainWindow.h"
#include "KeyboardController.h"
#include "AIController.h"


int main(int argc, char **argv) {
	
	srand((unsigned) time(NULL));

	// This will define the grid
	Game game(100, 100);

	// init GLUT and create window
	MainWindow& mainWindow = MainWindow::getInstance();
	mainWindow.init(&argc, argv);

	const KeyCode keys[] = {KEY_LEFT, KEY_RIGHT, KEY_A, KEY_E, KEY_I, KEY_P, KEY_C, KEY_B};
	const PlayerNumber maxNumHumanPlayers = 4;
	PlayerNumber currNumHumanPlayers = 0;
	PlayerNumber currNumPlayers = 0;

	Player *pPlayer;

	/*
	 * Usage (regex style) : trolltron(.exe)? (player|easy|medium|hard)+ (gameWidth game height)? (windowWidht windowHeight)?
	 * 4 human players max. 1 player min (human or not)
	 * if undefined, gameWidth gameHeight = 100 100, windowWidth windowHeight = 320 320
	 */

	// If this bool goes to true, then the parameters are messed up
	//We don't start the game and display the usage
	bool argFail = false;
	// The higher the easier
	int botInhibitDelay;
	int botRandomTurn;
	// Start at 1 because 0 is the path/name
	int argNum=1;
	// We need this to see if we have either 0, 2 or 4 numbers passed and where to store them
	char pairOfValuesDefined = 0;
	// Default values
	uint gameWidth = 100;
	uint gameHeight = 100;
	int windowWidth = 320;
	int windowHeight = 320;

	// Go through all parameters
	while(argNum < argc)
	{
		// If it's not a number
		if (atoi(argv[argNum]) == 0)
		{
			// If we started to inputing numbers, we shouldn't input players anymore
			if (pairOfValuesDefined > 0)
			{
				argFail = true;
			}
			// If it's "player"
			if (! strcmp(argv[argNum], "player"))
			{
				if(currNumHumanPlayers < maxNumHumanPlayers)
				{
					

					// Add him
					pPlayer = game.addPlayer();

					// And create its controller
					pPlayer->setController(mainWindow.generateNewKeyboardController(pPlayer, 
																					keys[currNumHumanPlayers*2],
																					keys[currNumHumanPlayers*2+1]));
					// Count 1 more (human) player
					currNumPlayers ++;
					currNumHumanPlayers ++;
				}
			}
			else
			{
				bool isABot = false;
				// If it's "easy"
				if(! strcmp(argv[argNum], "easy"))
				{
					isABot = true;
					botInhibitDelay = 10;
					botRandomTurn = 10;
				}
				else if(! strcmp(argv[argNum], "medium"))
				{
					isABot = true;
					botInhibitDelay = 3;
					botRandomTurn = 6;
				}
				else if(! strcmp(argv[argNum], "hard"))
				{
					isABot = true;
					botInhibitDelay = 1;
					botRandomTurn = 3;
				} else argFail = true;
				if(isABot)
				{
					// Count 1 more (non human) player
					currNumPlayers ++;
					pPlayer = game.addPlayer();
					// And instanciate its AI
					pPlayer->setController(new AIController(pPlayer, game.getField(), botInhibitDelay, botRandomTurn));
				}
			}

		}
		// Else if it IS a number
		else
		{
			// Numbers are only valid as pairs
			if (atoi(argv[argNum]) > 0 && argNum < argc -1 && atoi(argv[argNum+1]) > 0)
			{
				// Depending on if it's...
				switch(pairOfValuesDefined)
				
				{
					//The first pair : game dimensions
				case 0:
					pairOfValuesDefined ++;
					gameWidth = atoi(argv[argNum]);
					gameHeight = atoi(argv[argNum+1]);
					// It's a pair so we take 2 args at once
					argNum ++;
					break;
					//The 2nd pair : window dimensions
				case 1:
					pairOfValuesDefined ++;
					windowWidth = atoi(argv[argNum]);
					windowHeight = atoi(argv[argNum+1]);
					argNum ++;
					break;
					// Another pair : user mistake
				default:
					argFail = true;
				}
			}
			// If we input an odd number of numbers
			else
			{
				argFail = true;
			}
		}
		argNum++;

		if(argFail)
		{
			// Stop looping, we lost already
			break;
		}
	}
	// If too many or too few players;
	if(currNumHumanPlayers > maxNumHumanPlayers || currNumPlayers < 1)
	{
		argFail = true;
	}
	if(argFail)
	{
		std::cout << "Usage (regex style) : trolltron(.exe)? (player|easy|medium|hard)+ (gameWidth gameHeight)? (windowWidht windowHeight)?" << std::endl;
		std::cout << "4 human players max. 1 player min (human or not)" << std::endl;
		std::cout << "if undefined, gameWidth gameHeight = 100 100, windowWidth windowHeight = 320 320" << std::endl;
		exit(1);
	}
	
	// This will define the size and position, in pixel, of the window, originally. It can be moved and resized.
	mainWindow.create("Troll Tron", 100, 100, windowWidth, windowWidth);
	game.setSize(gameWidth, gameHeight);
	mainWindow.setField(&(game.getField()));
	mainWindow.setGame(&game);
	game.start();
	mainWindow.startLoop();
	return 0;
}