-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer_code.cpp
202 lines (169 loc) · 6.68 KB
/
player_code.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "player_code.h"
#include <algorithm>
#include <cmath>
#include <compare>
#include <iostream>
#include <set>
#include <sstream>
#include <unordered_map>
#include <vector>
Attributes::Attributes(unsigned hp, unsigned range, unsigned attack_power,
unsigned speed, unsigned price, unsigned is_aerial, unsigned weight, unsigned num_ability_turns, unsigned ability_activation_cost)
: hp(hp), range(range), attack_power(attack_power), speed(speed),
price(price), is_aerial(is_aerial), weight(weight), num_ability_turns(num_ability_turns), ability_activation_cost(ability_activation_cost) {}
Position::Position(int x, int y) : _x(x), _y(y) {}
int Position::get_x() const { return _x; }
int Position::get_y() const { return _y; }
double Position::distance_to(Position other) const {
auto delta_x = other.get_x() - this->get_x();
auto delta_y = other.get_y() - this->get_y();
return sqrt((double)(delta_x * delta_x + delta_y * delta_y));
}
bool is_valid_spawn_position(int x, int y) {
if (x < 0 || y < 0 || x >= static_cast<int>(Constants::MAP_NO_OF_COLS) ||
y >= static_cast<int>(Constants::MAP_NO_OF_ROWS)) {
return false;
}
return x == 0 || y == 0 ||
(x == (static_cast<int>(Constants::MAP_NO_OF_COLS) - 1)) ||
(y == (static_cast<int>(Constants::MAP_NO_OF_ROWS) - 1));
}
bool is_valid_spawn_position(Position pos) {
return is_valid_spawn_position(pos.get_x(), pos.get_y());
}
std::vector<Position> get_all_valid_spawn_positions() {
std::vector<Position> all_valid_positions;
// x is 0
for (int j = 0; j < static_cast<int>(Constants::MAP_NO_OF_ROWS); j++) {
all_valid_positions.push_back({0, j});
}
// y is 0
for (int i = 1; i < static_cast<int>(Constants::MAP_NO_OF_COLS); i++) {
all_valid_positions.push_back({i, 0});
}
// x is MAP_NO_OF_ROWS-1
for (int j = 1; j < static_cast<int>(Constants::MAP_NO_OF_ROWS); j++) {
all_valid_positions.push_back(
{static_cast<int>(Constants::MAP_NO_OF_COLS) - 1, j});
}
// y is MAP_NO_OF_COLS - 1
for (int i = 1; (i + 1) < static_cast<int>(Constants::MAP_NO_OF_COLS); i++) {
all_valid_positions.push_back(
{i, static_cast<int>(Constants::MAP_NO_OF_ROWS) - 1});
}
sort(all_valid_positions.begin(), all_valid_positions.end());
return all_valid_positions;
}
Actor::Actor(size_t id, size_t hp, size_t type, Position pos)
: _id(id), _hp(hp), _type(type), _position(pos) {}
size_t Actor::get_id() const { return _id; }
size_t Actor::get_hp() const { return _hp; }
size_t Actor::get_type() const { return _type; }
Position Actor::get_position() const { return _position; }
Attacker::Attacker(size_t id, size_t hp, size_t type, Position pos, size_t is_ability_active)
: Actor(id, hp, type, pos), is_ability_active(is_ability_active) {}
Defender::Defender(size_t id, size_t hp, size_t type, Position pos)
: Actor(id, hp, type, pos) {}
State::State(std::vector<Attacker> attackers, std::vector<Defender> defenders,
size_t no_of_coins_left, size_t turn_no)
: _turn_no(turn_no), _no_of_coins_left(no_of_coins_left),
_attackers(std::move(attackers)), _defenders(std::move(defenders)) {}
const std::vector<Attacker> &State::get_attackers() const {
return this->_attackers;
}
const std::vector<Defender> &State::get_defenders() const {
return this->_defenders;
}
size_t State::get_turn_no() const { return this->_turn_no; }
size_t State::get_coins_left() const { return this->_no_of_coins_left; }
PvPState::PvPState(std::vector<Attacker> attackers, std::vector<Attacker> opponent_attackers,
size_t no_of_coins_left, size_t turn_no)
: _turn_no(turn_no), _no_of_coins_left(no_of_coins_left),
_attackers(std::move(attackers)) {}
const std::vector<Attacker> &PvPState::get_attackers() const {
return this->_attackers;
}
const std::vector<Attacker> &PvPState::get_opponent_attackers() const {
return this->_opponent_attackers;
}
size_t PvPState::get_turn_no() const { return this->_turn_no; }
size_t PvPState::get_coins_left() const { return this->_no_of_coins_left; }
Game::Game() {}
void Game::spawn_attacker(size_t id, Position pos) {
this->_spawn_postions.push_back({id, pos});
this->_already_spawned_positions.insert(pos);
}
bool Game::already_spawned_at_position(Position pos) {
return this->_already_spawned_positions.contains(pos);
}
void Game::set_target(size_t attacker_id, size_t defender_id) {
this->_player_set_targets.insert({attacker_id, defender_id});
}
void Game::set_target(const Attacker &attacker, const Defender &defender) {
this->_player_set_targets.insert({attacker.get_id(), defender.get_id()});
}
void Game::set_target(const Attacker &attacker, const Attacker &opponent) {
this->_player_set_targets.insert({attacker.get_id(), opponent.get_id()});
}
void Game::activate_ability(size_t attacker_id) {
this->_ability_activations.push_back(attacker_id);
this-> already_activated_attacker_ids.insert(attacker_id);
}
std::ostringstream &Game::logr() { return this->_logr; }
const std::unordered_map<size_t, size_t> &Game::get_player_set_targets() const {
return this->_player_set_targets;
}
const std::vector<std::pair<size_t, Position>> &
Game::get_spawn_positions() const {
return this->_spawn_postions;
}
const std::set<Position> &Game::get_already_spawned_positions() const {
return this->_already_spawned_positions;
}
const std::vector<size_t> &Game::get_ability_activations() const {
return this->_ability_activations;
}
Map::Map(std::vector<std::vector<int>> map_as_grid)
: _grid(move(map_as_grid)) {}
Map Map::get(std::istream &stream) {
static int no_of_times_called = 0;
no_of_times_called++;
if (no_of_times_called > 1) {
throw std::runtime_error(
"Player tried to call an internal function. Not allowed");
}
size_t m = 0;
size_t n = 0;
stream >> m;
stream >> n;
Map::no_of_rows = m;
Map::no_of_cols = n;
std::vector<std::vector<int>> grid(m, std::vector<int>(n, 0));
for (auto &row : grid) {
for (auto &cell : row) {
stream >> cell;
}
}
return {move(grid)};
}
std::vector<Defender> Map::spawn_defenders() const {
static int no_of_times_called = 0;
no_of_times_called++;
if (no_of_times_called > 1) {
throw std::runtime_error(
"Player tried to call an internal function. Not allowed");
}
std::vector<Defender> defenders;
size_t id = 0;
for (size_t x = 0; x < this->no_of_cols; x++) {
for (size_t y = 0; y < this->no_of_rows; y++) {
if (this->_grid[y][x] > 0) {
auto &attributes_for_defender =
Constants::DEFENDER_TYPE_ATTRIBUTES.at(this->_grid[y][x]);
defenders.emplace_back(id++, attributes_for_defender.hp,
this->_grid[y][x], Position(x, y));
}
}
}
return defenders;
}