Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpp/.ccls
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
clang
-std=c++20
10 changes: 7 additions & 3 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
EXE=run
EXEPVP=runpvp
CXX=g++
FLAGS=-std=c++20 -O2 -static -fdiagnostics-color=always

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

$(EXEPVP).o: $(EXEPVP).cpp player_code.h
$(CXX) -c -o $(EXEPVP).o $(FLAGS) $(EXEPVP).cpp

main.o: main.cpp player_code.h
$(CXX) $(FLAGS) -c -o main.o main.cpp

$(EXE): $(EXE).o player_code.o main.o
g++ -fdiagnostics-color=always -static -o $(EXE) $(EXE).o player_code.o main.o
$(EXE): $(EXE).o $(EXEPVP).o player_code.o main.o
g++ -fdiagnostics-color=always -static -o $(EXE) $(EXE).o $(EXEPVP).o player_code.o main.o

clean_objects:
rm player_code.o $(EXE).o main.o

clean:
rm $(EXE) $(EXE).o

.PHONY: all clean
.PHONY: all clean
2 changes: 1 addition & 1 deletion cpp/compiler.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN make all

RUN rm run.o

CMD [ "make", "all" ]
CMD [ "make", "all" ]
126 changes: 101 additions & 25 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#include "player_code.h"

void init_constants() {
std::ostringstream all_logs;

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

void init_constants() {
// All the attacker types
std::cin >> Constants::NO_OF_ATTACKER_TYPES;

std::unordered_map<size_t, Attributes> attacker_type_to_attributes;
for (size_t i = 1; i <= Constants::NO_OF_ATTACKER_TYPES; i++) {
unsigned hp, range, attack_power, speed, price, is_aerial;
std::cin >> hp >> range >> attack_power >> speed >> price >> is_aerial;
unsigned hp, range, attack_power, speed, price, is_aerial , weight, num_ability_turns, ability_activation_cost;
std::cin >> hp >> range >> attack_power >> speed >> price >> is_aerial >> weight >> num_ability_turns >> ability_activation_cost;
attacker_type_to_attributes.insert(
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial)));
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial,weight, num_ability_turns,ability_activation_cost)));
}
Constants::ATTACKER_TYPE_ATTRIBUTES = attacker_type_to_attributes;

Expand All @@ -24,7 +25,7 @@ void init_constants() {
unsigned hp, range, attack_power, speed, price, is_aerial;
std::cin >> hp >> range >> attack_power >> speed >> price >> is_aerial;
defender_type_to_attributes.insert(
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial)));
std::make_pair(i, Attributes(hp, range, attack_power, speed, price, is_aerial,0,0,0)));
}
Constants::DEFENDER_TYPE_ATTRIBUTES = defender_type_to_attributes;
}
Expand All @@ -36,19 +37,19 @@ Map get_initial_map() {
return map;
}

void output(State &state, Game &game) {

// Player logs are logged to cerr, so that driver will collect it
void output(size_t turn_no, Game &game) {
game.logr().flush();

if (!game.logr().view().empty()) {
std::cerr << "TURN " << state.get_turn_no() << '\n';
std::cerr << game.logr().view() << '\n';
std::cerr << "ENDLOG" << std::endl;
all_logs << "TURN " << turn_no << std::endl;
all_logs << game.logr().view() << std::endl;
all_logs << "ENDLOG" << std::endl;
}

// Game details logged
const auto &spawn_positions = game.get_spawn_positions();
const auto &player_set_targets = game.get_player_set_targets();
const auto &ability_activations = game.get_ability_activations();

std::cout << spawn_positions.size() << std::endl;
for (const auto &entry : spawn_positions) {
Expand All @@ -60,20 +61,27 @@ void output(State &state, Game &game) {
for (const auto &entry : player_set_targets) {
std::cout << entry.first << " " << entry.second << std::endl;
}

std::cout << ability_activations.size() << std::endl;
for (const auto &attacker_id : ability_activations) {
std::cout << attacker_id << std::endl;
}
}

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

void dump_logs(PvPState& state, Game &game) {}

State next_state(size_t cur_turn_no) {
size_t no_of_active_defenders;
size_t no_of_active_attackers;

std::cin >> no_of_active_attackers;
std::vector<Attacker> attackers;
for (size_t i = 0; i < no_of_active_attackers; i++) {
size_t id, hp, x, y, type;
std::cin >> id >> x >> y >> type >> hp;
attackers.push_back(Attacker(id, hp, type, Position(x, y)));
size_t id, hp, x, y, type, is_ability_active;
std::cin >> id >> x >> y >> type >> hp >> is_ability_active;
attackers.push_back(Attacker(id, hp, type, Position(x, y), is_ability_active));
}

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

int main() {
init_constants();
Map initial_map = get_initial_map();
PvPState pvp_next_state(size_t cur_turn_no) {
size_t no_of_active_attackers;
size_t no_of_opponent_attackers;

State state({}, initial_map.spawn_defenders(), Constants::MAX_NO_OF_COINS, 0);
std::cin >> no_of_active_attackers;
std::vector<Attacker> attackers;
for (size_t i = 0; i < no_of_active_attackers; i++) {
size_t id, hp, x, y, type, is_ability_active;
std::cin >> id >> x >> y >> type >> hp >> is_ability_active;
attackers.push_back(Attacker(id, hp, type, Position(x, y), is_ability_active));
}

std::cin >> no_of_opponent_attackers;
std::vector<Attacker> opponent_attackers;
for (size_t i = 0; i < no_of_opponent_attackers; i++) {
size_t id, hp, x, y, type, is_ability_active;
std::cin >> id >> x >> y >> type >> hp >> is_ability_active;
opponent_attackers.push_back(Attacker(id, hp, type, Position(x, y), is_ability_active));
}

return {move(attackers), move(opponent_attackers), Constants::PVP_FIXED_COINS, cur_turn_no + 1};
}

enum class GameType {
NORMAL,
PVP
};

auto game = run(state);
GameType string_to_game_type(std::string type) {
if (type == std::string("normal")) {
return GameType::NORMAL;
} else {
return GameType::PVP;
}
}

output(state, game);
int main(int argc, char** argv) {

if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " [game-type]\n";
exit(1);
}

for (size_t i = 0; i < Constants::NO_OF_TURNS; i++) {
state = next_state(state.get_turn_no());
game = run(state);
output(state, game);
auto gameType = string_to_game_type(std::string(argv[1]));

switch (gameType) {
case GameType::NORMAL: {
std::cin >> Constants::NO_OF_TURNS >> Constants::MAX_NO_OF_COINS;
init_constants();
Map initial_map = get_initial_map();
State state({}, initial_map.spawn_defenders(), Constants::MAX_NO_OF_COINS, 0);

auto game = run(state);
output(state.get_turn_no(), game);

for (size_t i = 0; i < Constants::NO_OF_TURNS; i++) {
state = next_state(state.get_turn_no());
game = run(state);
output(state.get_turn_no(), game);
}

break;
}

case GameType::PVP: {
std::cin >> Constants::NO_OF_TURNS >> Constants::PVP_FIXED_COINS;
init_constants();
PvPState state({},{},Constants::PVP_FIXED_COINS, 0);

auto game = run(state);
output(state.get_turn_no(), game);

for (size_t i = 0; i < Constants::NO_OF_TURNS; i++) {
state = pvp_next_state(state.get_turn_no());
game = run(state);
output(state.get_turn_no(), game);
}

break;
}
}

std::cerr << all_logs.str() << std::endl;
}
38 changes: 33 additions & 5 deletions cpp/player_code.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <vector>

Attributes::Attributes(unsigned hp, unsigned range, unsigned attack_power,
unsigned speed, unsigned price, unsigned is_aerial)
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) {}
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) {}

Expand Down Expand Up @@ -73,8 +73,8 @@ 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)
: Actor(id, hp, type, pos) {}
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) {}
Expand All @@ -93,6 +93,21 @@ const std::vector<Defender> &State::get_defenders() const {
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) {
Expand All @@ -109,6 +124,15 @@ 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 {
Expand All @@ -122,6 +146,10 @@ 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)) {}

Expand Down Expand Up @@ -171,4 +199,4 @@ std::vector<Defender> Map::spawn_defenders() const {
}
}
return defenders;
}
}
Loading