-
Notifications
You must be signed in to change notification settings - Fork 0
/
Runner.cpp
66 lines (49 loc) · 1.88 KB
/
Runner.cpp
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
#include "Runner.h"
#include <vector>
#include "MyStrategy.h"
using namespace model;
using namespace std;
int main(int argc, char* argv[]) {
if (argc == 4) {
Runner runner(argv[1], argv[2], argv[3]);
runner.run();
} else {
Runner runner("127.0.0.1", "31001", "0000000000000000");
runner.run();
}
return 0;
}
Runner::Runner(const char* host, const char* port, const char* token)
: remoteProcessClient(host, atoi(port)), token(token) {
}
void Runner::run() {
remoteProcessClient.writeTokenMessage(token);
int teamSize = remoteProcessClient.readTeamSizeMessage();
remoteProcessClient.writeProtocolVersionMessage();
Game game = remoteProcessClient.readGameContextMessage();
vector<Strategy*> strategies;
for (int strategyIndex = 0; strategyIndex < teamSize; ++strategyIndex) {
Strategy* strategy = new MyStrategy;
strategies.push_back(strategy);
}
PlayerContext* playerContext;
while ((playerContext = remoteProcessClient.readPlayerContextMessage()) != NULL) {
vector<Hockeyist> playerHockeyists = playerContext->getHockeyists();
if ((int) playerHockeyists.size() != teamSize) {
break;
}
vector<Move> moves;
for (int hockeyistIndex = 0; hockeyistIndex < teamSize; ++hockeyistIndex) {
Hockeyist playerHockeyist = playerHockeyists[hockeyistIndex];
Move move;
strategies[playerHockeyist.getTeammateIndex()]
->move(playerHockeyist, playerContext->getWorld(), game, move);
moves.push_back(move);
}
remoteProcessClient.writeMovesMessage(moves);
delete playerContext;
}
for (int strategyIndex = 0; strategyIndex < teamSize; ++strategyIndex) {
delete strategies[strategyIndex];
}
}