-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.hh
107 lines (76 loc) · 1.87 KB
/
Action.hh
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
#ifndef Action_hh
#define Action_hh
#include "Structs.hh"
struct Command;
/**
* Class that stores the commands requested by a player in a round.
*/
class Action {
public:
/**
* The following functions add a command to the action (= list of commands).
* They fail if a command is already present for the commanded unit.
*/
/**
* Commands unit with identifier id to move following direction dir.
*/
void move(int id, Dir dir);
//////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
/**
* Empty constructor.
*/
Action () : q(0) { }
private:
friend class Game;
friend class SecGame;
friend class Board;
/**
* Maximum number of commands allowed for a player during one round.
*/
static const int MAX_COMMANDS = 1000;
/**
* Number of commands tried so far.
*/
int q;
/**
* Set of units that have already performed a command.
*/
set<int> u;
/**
* List of commands to be performed during this round.
*/
vector<Command> v;
/**
* Read/write commands to/from a stream.
*/
Action (istream& is);
static void print (const vector<Command>& commands, ostream& os);
void execute(const Command& m);
};
/**
* Class for commands.
*/
struct Command {
int id; // Identifier of the commanded unit.
int c_type; // Type of command.
int dir; // Direction of the command
/**
* Constructor with all defining fields.
*/
Command (int id, int c_type, int dir) :
id(id), c_type(c_type), dir(dir) { }
};
inline void Action::move(int id, Dir dir) {
execute(Command(id, Move, int(dir)));
}
inline void Action::execute(const Command& m) {
++q;
_my_assert(q <= MAX_COMMANDS, "Too many commands.");
if (u.find(m.id) != u.end()) {
cerr << "warning: command already requested for unit " << m.id << endl;
return;
}
u.insert(m.id);
v.push_back(m);
}
#endif