Skip to content

Commit e0ef38d

Browse files
feat: PvP (#5)
* feat: pvp for cpp with fs logs * feat:pvp * fix: clear logs * fix(cpp): global logger * feat: global logger * wip: add weights for troops * feat: add weights * fix: python state typecast * feat: abilities * fix: helper fn for abilities * fix: constructor issues * fix: lsp stdc++20 --------- Co-authored-by: Bhoopesh <[email protected]>
1 parent 16fbce4 commit e0ef38d

22 files changed

+780
-95
lines changed

cpp/.ccls

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
clang
2+
-std=c++20

cpp/Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
EXE=run
2+
EXEPVP=runpvp
23
CXX=g++
34
FLAGS=-std=c++20 -O2 -static -fdiagnostics-color=always
45

@@ -10,16 +11,19 @@ player_code.o: player_code.h player_code.cpp
1011
$(EXE).o: $(EXE).cpp player_code.h
1112
$(CXX) -c -o $(EXE).o $(FLAGS) $(EXE).cpp
1213

14+
$(EXEPVP).o: $(EXEPVP).cpp player_code.h
15+
$(CXX) -c -o $(EXEPVP).o $(FLAGS) $(EXEPVP).cpp
16+
1317
main.o: main.cpp player_code.h
1418
$(CXX) $(FLAGS) -c -o main.o main.cpp
1519

16-
$(EXE): $(EXE).o player_code.o main.o
17-
g++ -fdiagnostics-color=always -static -o $(EXE) $(EXE).o player_code.o main.o
20+
$(EXE): $(EXE).o $(EXEPVP).o player_code.o main.o
21+
g++ -fdiagnostics-color=always -static -o $(EXE) $(EXE).o $(EXEPVP).o player_code.o main.o
1822

1923
clean_objects:
2024
rm player_code.o $(EXE).o main.o
2125

2226
clean:
2327
rm $(EXE) $(EXE).o
2428

25-
.PHONY: all clean
29+
.PHONY: all clean

cpp/compiler.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ RUN make all
88

99
RUN rm run.o
1010

11-
CMD [ "make", "all" ]
11+
CMD [ "make", "all" ]

cpp/main.cpp

Lines changed: 101 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#include "player_code.h"
22

3-
void init_constants() {
3+
std::ostringstream all_logs;
44

5-
std::cin >> Constants::NO_OF_TURNS >> Constants::MAX_NO_OF_COINS;
5+
//make global object for holding all ids of attackers whose abilities activated
66

7+
void init_constants() {
78
// All the attacker types
89
std::cin >> Constants::NO_OF_ATTACKER_TYPES;
910

1011
std::unordered_map<size_t, Attributes> attacker_type_to_attributes;
1112
for (size_t i = 1; i <= Constants::NO_OF_ATTACKER_TYPES; i++) {
12-
unsigned hp, range, attack_power, speed, price, is_aerial;
13-
std::cin >> hp >> range >> attack_power >> speed >> price >> is_aerial;
13+
unsigned hp, range, attack_power, speed, price, is_aerial , weight, num_ability_turns, ability_activation_cost;
14+
std::cin >> hp >> range >> attack_power >> speed >> price >> is_aerial >> weight >> num_ability_turns >> ability_activation_cost;
1415
attacker_type_to_attributes.insert(
15-
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial)));
16+
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial,weight, num_ability_turns,ability_activation_cost)));
1617
}
1718
Constants::ATTACKER_TYPE_ATTRIBUTES = attacker_type_to_attributes;
1819

@@ -24,7 +25,7 @@ void init_constants() {
2425
unsigned hp, range, attack_power, speed, price, is_aerial;
2526
std::cin >> hp >> range >> attack_power >> speed >> price >> is_aerial;
2627
defender_type_to_attributes.insert(
27-
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial)));
28+
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial,0,0,0)));
2829
}
2930
Constants::DEFENDER_TYPE_ATTRIBUTES = defender_type_to_attributes;
3031
}
@@ -36,19 +37,19 @@ Map get_initial_map() {
3637
return map;
3738
}
3839

39-
void output(State &state, Game &game) {
40-
41-
// Player logs are logged to cerr, so that driver will collect it
40+
void output(size_t turn_no, Game &game) {
4241
game.logr().flush();
42+
4343
if (!game.logr().view().empty()) {
44-
std::cerr << "TURN " << state.get_turn_no() << '\n';
45-
std::cerr << game.logr().view() << '\n';
46-
std::cerr << "ENDLOG" << std::endl;
44+
all_logs << "TURN " << turn_no << std::endl;
45+
all_logs << game.logr().view() << std::endl;
46+
all_logs << "ENDLOG" << std::endl;
4747
}
4848

4949
// Game details logged
5050
const auto &spawn_positions = game.get_spawn_positions();
5151
const auto &player_set_targets = game.get_player_set_targets();
52+
const auto &ability_activations = game.get_ability_activations();
5253

5354
std::cout << spawn_positions.size() << std::endl;
5455
for (const auto &entry : spawn_positions) {
@@ -60,20 +61,27 @@ void output(State &state, Game &game) {
6061
for (const auto &entry : player_set_targets) {
6162
std::cout << entry.first << " " << entry.second << std::endl;
6263
}
64+
65+
std::cout << ability_activations.size() << std::endl;
66+
for (const auto &attacker_id : ability_activations) {
67+
std::cout << attacker_id << std::endl;
68+
}
6369
}
6470

6571
void dump_logs(State &state, Game &game) {}
6672

73+
void dump_logs(PvPState& state, Game &game) {}
74+
6775
State next_state(size_t cur_turn_no) {
6876
size_t no_of_active_defenders;
6977
size_t no_of_active_attackers;
7078

7179
std::cin >> no_of_active_attackers;
7280
std::vector<Attacker> attackers;
7381
for (size_t i = 0; i < no_of_active_attackers; i++) {
74-
size_t id, hp, x, y, type;
75-
std::cin >> id >> x >> y >> type >> hp;
76-
attackers.push_back(Attacker(id, hp, type, Position(x, y)));
82+
size_t id, hp, x, y, type, is_ability_active;
83+
std::cin >> id >> x >> y >> type >> hp >> is_ability_active;
84+
attackers.push_back(Attacker(id, hp, type, Position(x, y), is_ability_active));
7785
}
7886

7987
std::cin >> no_of_active_defenders;
@@ -90,19 +98,87 @@ State next_state(size_t cur_turn_no) {
9098
return {move(attackers), move(defenders), coins_left, cur_turn_no + 1};
9199
}
92100

93-
int main() {
94-
init_constants();
95-
Map initial_map = get_initial_map();
101+
PvPState pvp_next_state(size_t cur_turn_no) {
102+
size_t no_of_active_attackers;
103+
size_t no_of_opponent_attackers;
96104

97-
State state({}, initial_map.spawn_defenders(), Constants::MAX_NO_OF_COINS, 0);
105+
std::cin >> no_of_active_attackers;
106+
std::vector<Attacker> attackers;
107+
for (size_t i = 0; i < no_of_active_attackers; i++) {
108+
size_t id, hp, x, y, type, is_ability_active;
109+
std::cin >> id >> x >> y >> type >> hp >> is_ability_active;
110+
attackers.push_back(Attacker(id, hp, type, Position(x, y), is_ability_active));
111+
}
112+
113+
std::cin >> no_of_opponent_attackers;
114+
std::vector<Attacker> opponent_attackers;
115+
for (size_t i = 0; i < no_of_opponent_attackers; i++) {
116+
size_t id, hp, x, y, type, is_ability_active;
117+
std::cin >> id >> x >> y >> type >> hp >> is_ability_active;
118+
opponent_attackers.push_back(Attacker(id, hp, type, Position(x, y), is_ability_active));
119+
}
120+
121+
return {move(attackers), move(opponent_attackers), Constants::PVP_FIXED_COINS, cur_turn_no + 1};
122+
}
123+
124+
enum class GameType {
125+
NORMAL,
126+
PVP
127+
};
98128

99-
auto game = run(state);
129+
GameType string_to_game_type(std::string type) {
130+
if (type == std::string("normal")) {
131+
return GameType::NORMAL;
132+
} else {
133+
return GameType::PVP;
134+
}
135+
}
100136

101-
output(state, game);
137+
int main(int argc, char** argv) {
138+
139+
if (argc < 2) {
140+
std::cerr << "Usage: " << argv[0] << " [game-type]\n";
141+
exit(1);
142+
}
102143

103-
for (size_t i = 0; i < Constants::NO_OF_TURNS; i++) {
104-
state = next_state(state.get_turn_no());
105-
game = run(state);
106-
output(state, game);
144+
auto gameType = string_to_game_type(std::string(argv[1]));
145+
146+
switch (gameType) {
147+
case GameType::NORMAL: {
148+
std::cin >> Constants::NO_OF_TURNS >> Constants::MAX_NO_OF_COINS;
149+
init_constants();
150+
Map initial_map = get_initial_map();
151+
State state({}, initial_map.spawn_defenders(), Constants::MAX_NO_OF_COINS, 0);
152+
153+
auto game = run(state);
154+
output(state.get_turn_no(), game);
155+
156+
for (size_t i = 0; i < Constants::NO_OF_TURNS; i++) {
157+
state = next_state(state.get_turn_no());
158+
game = run(state);
159+
output(state.get_turn_no(), game);
160+
}
161+
162+
break;
163+
}
164+
165+
case GameType::PVP: {
166+
std::cin >> Constants::NO_OF_TURNS >> Constants::PVP_FIXED_COINS;
167+
init_constants();
168+
PvPState state({},{},Constants::PVP_FIXED_COINS, 0);
169+
170+
auto game = run(state);
171+
output(state.get_turn_no(), game);
172+
173+
for (size_t i = 0; i < Constants::NO_OF_TURNS; i++) {
174+
state = pvp_next_state(state.get_turn_no());
175+
game = run(state);
176+
output(state.get_turn_no(), game);
177+
}
178+
179+
break;
180+
}
107181
}
182+
183+
std::cerr << all_logs.str() << std::endl;
108184
}

cpp/player_code.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <vector>
1111

1212
Attributes::Attributes(unsigned hp, unsigned range, unsigned attack_power,
13-
unsigned speed, unsigned price, unsigned is_aerial)
13+
unsigned speed, unsigned price, unsigned is_aerial, unsigned weight, unsigned num_ability_turns, unsigned ability_activation_cost)
1414
: hp(hp), range(range), attack_power(attack_power), speed(speed),
15-
price(price), is_aerial(is_aerial) {}
15+
price(price), is_aerial(is_aerial), weight(weight), num_ability_turns(num_ability_turns), ability_activation_cost(ability_activation_cost) {}
1616

1717
Position::Position(int x, int y) : _x(x), _y(y) {}
1818

@@ -73,8 +73,8 @@ size_t Actor::get_hp() const { return _hp; }
7373
size_t Actor::get_type() const { return _type; }
7474
Position Actor::get_position() const { return _position; }
7575

76-
Attacker::Attacker(size_t id, size_t hp, size_t type, Position pos)
77-
: Actor(id, hp, type, pos) {}
76+
Attacker::Attacker(size_t id, size_t hp, size_t type, Position pos, size_t is_ability_active)
77+
: Actor(id, hp, type, pos), is_ability_active(is_ability_active) {}
7878

7979
Defender::Defender(size_t id, size_t hp, size_t type, Position pos)
8080
: Actor(id, hp, type, pos) {}
@@ -93,6 +93,21 @@ const std::vector<Defender> &State::get_defenders() const {
9393
size_t State::get_turn_no() const { return this->_turn_no; }
9494
size_t State::get_coins_left() const { return this->_no_of_coins_left; }
9595

96+
PvPState::PvPState(std::vector<Attacker> attackers, std::vector<Attacker> opponent_attackers,
97+
size_t no_of_coins_left, size_t turn_no)
98+
: _turn_no(turn_no), _no_of_coins_left(no_of_coins_left),
99+
_attackers(std::move(attackers)) {}
100+
101+
const std::vector<Attacker> &PvPState::get_attackers() const {
102+
return this->_attackers;
103+
}
104+
105+
const std::vector<Attacker> &PvPState::get_opponent_attackers() const {
106+
return this->_opponent_attackers;
107+
}
108+
size_t PvPState::get_turn_no() const { return this->_turn_no; }
109+
size_t PvPState::get_coins_left() const { return this->_no_of_coins_left; }
110+
96111
Game::Game() {}
97112

98113
void Game::spawn_attacker(size_t id, Position pos) {
@@ -109,6 +124,15 @@ void Game::set_target(const Attacker &attacker, const Defender &defender) {
109124
this->_player_set_targets.insert({attacker.get_id(), defender.get_id()});
110125
}
111126

127+
void Game::set_target(const Attacker &attacker, const Attacker &opponent) {
128+
this->_player_set_targets.insert({attacker.get_id(), opponent.get_id()});
129+
}
130+
131+
void Game::activate_ability(size_t attacker_id) {
132+
this->_ability_activations.push_back(attacker_id);
133+
this-> already_activated_attacker_ids.insert(attacker_id);
134+
}
135+
112136
std::ostringstream &Game::logr() { return this->_logr; }
113137

114138
const std::unordered_map<size_t, size_t> &Game::get_player_set_targets() const {
@@ -122,6 +146,10 @@ const std::set<Position> &Game::get_already_spawned_positions() const {
122146
return this->_already_spawned_positions;
123147
}
124148

149+
const std::vector<size_t> &Game::get_ability_activations() const {
150+
return this->_ability_activations;
151+
}
152+
125153
Map::Map(std::vector<std::vector<int>> map_as_grid)
126154
: _grid(move(map_as_grid)) {}
127155

@@ -171,4 +199,4 @@ std::vector<Defender> Map::spawn_defenders() const {
171199
}
172200
}
173201
return defenders;
174-
}
202+
}

0 commit comments

Comments
 (0)