human_controller.cpp 4.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "human_controller.h"

#include "game.h"

#include <iostream> //Really I can't be bothered with fscanf any more

using namespace std;

MovementResult Human_Controller::QuerySetup(const char * opponentName, string setup[])
{
	
	static bool shownMessage = false;
	if (!shownMessage)
	{
		if (graphicsEnabled)
16
17
			fprintf(stdout, "WARNING: GUI not fully supported. You will be forced to use the default setup.\n");
		else
18
		{
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
			fprintf(stdout,"Enter %d x %d Setup grid\n", Game::theGame->theBoard.Width(), 4);
			fprintf(stdout,"Please enter one line at a time.\n");
			fprintf(stdout, "You must place at least the Flag (%c). Use '%c' for empty squares.\n", Piece::tokens[(int)Piece::FLAG], Piece::tokens[(int)Piece::NOTHING]);
		
			switch (colour)
			{
				case Piece::RED:
					fprintf(stdout, "You are RED and occupy the top 4 rows of the board.\n");
					fprintf(stdout, "NOTE: Enter \"DEFAULT\" to use the setup:\n");
					fprintf(stdout, "FB8sB479B8\nBB31555583\n6724898974\n967B669999\n");
					break;
				case Piece::BLUE:
					fprintf(stdout, "You are BLUE and occupy the bottom 4 rows of the board.\n");
					fprintf(stdout, "NOTE: Enter \"DEFAULT\" to use the setup:\n");
					fprintf(stdout, "967B669999\n6724898974\nBB31555583\nFB8sB479B8\n");
					break;
				default:
					fprintf(stdout, "WARNING: Unknown colour error! Please exit the game.\n");
					break;
			}	
		}	
40
41
42
		
		shownMessage = true;
	}
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

	if (graphicsEnabled)
	{
		switch(colour)
		{
			case Piece::RED:
				setup[0] = "FB8sB479B8"; 
				setup[1] = "BB31555583";
				setup[2] = "6724898974";
				setup[3] = "967B669999";
				break;
			case Piece::BLUE:
				setup[0] = "967B669999";
				setup[1] = "6724898974";
				setup[2] = "BB31555583";
				setup[3] = "FB8sB479B8";
				break;
			default:
				assert(false);
				break;
			}
		return MovementResult::OK;
	}
66
67

	for (int y = 0; y < 4; ++y)
68
	{
69
		cin >> setup[y];
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
		if (y == 0 && setup[0] == "DEFAULT")
		{
			switch(colour)
			{
				case Piece::RED:
					setup[0] = "FB8sB479B8"; 
					setup[1] = "BB31555583";
					setup[2] = "6724898974";
					setup[3] = "967B669999";
					break;
				case Piece::BLUE:
					setup[0] = "967B669999";
					setup[1] = "6724898974";
					setup[2] = "BB31555583";
					setup[3] = "FB8sB479B8";
					break;
				default:
					assert(false);
					break;
			}
			break;
		}
	}
93
94
95
96
97
98
99
100
101
102
	assert(cin.get() == '\n');
	
	return MovementResult::OK;
}

MovementResult Human_Controller::QueryMove(string & buffer)
{
	static bool shownMessage = false;
	if (!shownMessage)
	{
103
104
105
106
107
108
		if (!graphicsEnabled)
		{
			fprintf(stdout, "Please enter your move in the format:\n X Y DIRECTION [MULTIPLIER=1]\n");
			fprintf(stdout, "Where X and Y indicate the coordinates of the piece to move;\n DIRECTION is one of UP, DOWN, LEFT or RIGHT\n and MULTIPLIER is optional (and only valid for scouts (%c))\n", Piece::tokens[(int)(Piece::SCOUT)]);
			
		}
109
110
111
112
113
		shownMessage = true;
	}

	

114
115
116
117
	if (graphicsEnabled)
	{
		fprintf(stdout, "Click to move!\n");
		SDL_Event event; int mouseClick = 0;
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
		int x[] = {-1, -1}; int y[] = {-1, -1};
		while (mouseClick < 2)
		{
			
			while (SDL_PollEvent(&event))
			{
				switch (event.type)
				{
					case SDL_QUIT:
						Game::theGame->logMessage("Exit called by human player!\n");
						exit(EXIT_SUCCESS);
						break;
					case SDL_MOUSEBUTTONDOWN:
					switch (event.button.button)
					{
						case SDL_BUTTON_LEFT:
							SDL_GetMouseState(&x[mouseClick], &y[mouseClick]);
							x[mouseClick] /= 32; y[mouseClick] /= 32; //Adjust based on graphics grid size
							if (mouseClick == 0)
							{
								stringstream s("");
								s << x[0] << " " << y[0] << " ";
								buffer += s.str();
							}
							else if (mouseClick == 1)
							{
								int xDist = x[1] - x[0];
								int yDist = y[1] - y[0];
								if (abs(xDist) > abs(yDist))
								{
									if (xDist < 0)
										buffer += "LEFT";
									else
										buffer += "RIGHT";
								}
								else if (yDist < 0)
									buffer += "UP";
								else
									buffer += "DOWN";
							}
							mouseClick++;
							break;
					}
					break;
				}
			}
		}
		fprintf(stdout, "Move complete!\n");
	}
	else
169
	{
170
171
172
173
174
		buffer.clear();
		for (char in = fgetc(stdin); in != '\n'; in = fgetc(stdin))
		{
			buffer += in;
		}
175
176
177
178
179
180
181
	}
	
	

	return MovementResult::OK;
	
}