controller.cpp 4.19 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
2
3
4
5
6
7
8
9
10
#include <sstream>

#include "stratego.h"

#include "controller.h"

using namespace std;

/**
 * Queries the AI program to setup its pieces
11
 * @param opponentName - string containing the name/id of the opponent AI program
Sam Moore's avatar
Sam Moore committed
12
13
 * @returns the result of the response
 */
14
MovementResult Controller::Setup(const char * opponentName)
Sam Moore's avatar
Sam Moore committed
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
{
	int y;
	switch (colour)
	{
		case Piece::RED:
			assert(SendMessage("RED %s %d %d", opponentName, Board::theBoard.Width(), Board::theBoard.Height()));
			y = 0;
			
			break;
		case Piece::BLUE:
			assert(SendMessage("BLUE %s %d %d", opponentName, Board::theBoard.Width(), Board::theBoard.Height()));
			y = Board::theBoard.Height()-4;
			
			break;
		case Piece::NONE:
		case Piece::BOTH:
			//Should never see this;
			assert(false);
			break;
	}


	int usedUnits[(int)(Piece::BOMB)];
	for (int ii = 0; ii <= (int)(Piece::BOMB); ++ii)
		usedUnits[ii] = 0;

	//The setup is spread across 4 lines of the board - blue at the top, red at the bottom. AI has 2.5s for each line.
	



	for (int ii=0; ii < 4; ++ii)
	{
		string line="";
		if (!GetMessage(line, 2.5))
		{
			fprintf(stderr, "Timeout on setup\n");
52
			return MovementResult::BAD_RESPONSE;
Sam Moore's avatar
Sam Moore committed
53
54
55
56
		}
		if ((int)(line.size()) != Board::theBoard.Width())
		{
			fprintf(stderr, "Bad length of \"%s\" on setup\n", line.c_str());
57
			return MovementResult::BAD_RESPONSE;
Sam Moore's avatar
Sam Moore committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
		}
	
		for (int x = 0; x < (int)(line.size()); ++x)
		{
			Piece::Type type = Piece::GetType(line[x]);
			if (type != Piece::NOTHING)
			{
//fprintf(stderr, "x y %d %d\n", x, y+ii);
//					fprintf(stderr, "Found unit of type '%c' (%d '%c') %d vs %d\n", line[x], (int)(type), Piece::tokens[(int)(type)], usedUnits[(int)(type)], Piece::maxUnits[(int)type]);
		///			fprintf(stderr, "Marshal is %d '%c', flag is %d '%c'\n", (int)Piece::MARSHAL, Piece::tokens[(int)(Piece::MARSHAL)], (int)Piece::FLAG, Piece::tokens[(int)(Piece::FLAG)]);

				usedUnits[(int)(type)] += 1;
				if (usedUnits[type] > Piece::maxUnits[(int)type])
				{
					fprintf(stderr, "Too many units of type %c\n", Piece::tokens[(int)(type)]);
73
					return MovementResult::BAD_RESPONSE;
Sam Moore's avatar
Sam Moore committed
74
75
76
77
78
79
80
81
82
				}
	
				Board::theBoard.AddPiece(x, y+ii, type, colour);
			}
		}	
	}

	if (usedUnits[(int)Piece::FLAG] <= 0)
	{
83
		return MovementResult::BAD_RESPONSE; //You need to include a flag!
Sam Moore's avatar
Sam Moore committed
84
85
	}

86
	return MovementResult::OK;
Sam Moore's avatar
Sam Moore committed
87
88
89
90
91
92
93
}


/**
 * Queries the AI program to respond to a state of Board::theBoard
 * @returns The result of the response and/or move if made
 */
94
MovementResult Controller::MakeMove(string & buffer)
Sam Moore's avatar
Sam Moore committed
95
96
97
{
	
	if (!Running())
98
		return MovementResult::NO_MOVE; //AI has quit
Sam Moore's avatar
Sam Moore committed
99
100
101
102
103
104
105
106
	Board::theBoard.Print(output, colour);

	

	
	buffer.clear();
	if (!GetMessage(buffer,2))
	{
107
		return MovementResult::NO_MOVE; //AI did not respond. It will lose by default.
Sam Moore's avatar
Sam Moore committed
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
	}

	int x; int y; string direction="";
	stringstream s(buffer);
	s >> x;
	s >> y;
	

	s >> direction;
	Board::Direction dir;
	if (direction == "UP")
	{
		dir = Board::UP;
	}
	else if (direction == "DOWN")
	{
		dir = Board::DOWN;
	}
	else if (direction == "LEFT")
	{
		dir = Board::LEFT;
	}
	else if (direction == "RIGHT")
	{
		dir = Board::RIGHT;
	}	
	else
	{
		fprintf(stderr, "BAD_RESPONSE \"%s\"\n", buffer.c_str());
137
		return MovementResult::BAD_RESPONSE; //AI gave bogus direction - it will lose by default.	
Sam Moore's avatar
Sam Moore committed
138
139
140
141
142
	}

	int multiplier = 1;
	if (s.peek() != EOF)
		s >> multiplier;
143
144
145
146
147
148
	MovementResult moveResult = Board::theBoard.MovePiece(x, y, dir, multiplier, colour);

	s.clear(); 	s.str("");

	//I stored the ranks in the wrong order; rank 1 is the marshal, 2 is the general etc...
	//So I am reversing them in the output... great work
Sam Moore's avatar
Sam Moore committed
149
	s << (Piece::BOMB - moveResult.attackerRank) << " " << (Piece::BOMB - moveResult.defenderRank);	
150
	switch (moveResult.type)
Sam Moore's avatar
Sam Moore committed
151
	{
152
		case MovementResult::OK:
Sam Moore's avatar
Sam Moore committed
153
154
			buffer += " OK";
			break;
155
		case MovementResult::VICTORY:
Sam Moore's avatar
Sam Moore committed
156
157
			buffer += " FLAG";
			break;
158
159
160
161
		case MovementResult::KILLS:
			buffer += " KILLS ";
			buffer += s.str();

Sam Moore's avatar
Sam Moore committed
162
			break;
163
164
165
		case MovementResult::DIES:
			buffer += " DIES ";
			buffer += s.str();
Sam Moore's avatar
Sam Moore committed
166
			break;
167
168
169
		case MovementResult::BOTH_DIE:
			buffer += " BOTHDIE ";
			buffer += s.str();
Sam Moore's avatar
Sam Moore committed
170
171
172
173
174
175
176
			break;	
		default:
			buffer += " ILLEGAL";
			break;			
		
	}

Sam Moore's avatar
Sam Moore committed
177
178
179
	//if (!Board::LegalResult(moveResult))
	//	return MovementResult::OK; //HACK - Legal results returned!
	//else
Sam Moore's avatar
Sam Moore committed
180
181
182
183
		return moveResult; 	

}