Skip to content

Commit de06d99

Browse files
committed
fix: helper fn for abilities
1 parent 948f577 commit de06d99

File tree

11 files changed

+103
-47
lines changed

11 files changed

+103
-47
lines changed

.vscode/c_cpp_properties.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Linux-GCC",
5+
"includePath": ["${workspaceFolder}/**"],
6+
"defines": [],
7+
"compilerPath": "/usr/bin/g++",
8+
"cStandard": "c17",
9+
"cppStandard": "c++20",
10+
"intelliSenseMode": "gcc-x64",
11+
"browse": {
12+
"path": ["${workspaceFolder}"],
13+
"limitSymbolsToIncludedHeaders": true,
14+
"databaseFilename": ""
15+
}
16+
}
17+
],
18+
"version": 4
19+
}
20+

cpp/player_code.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ void Game::set_target(const Attacker &attacker, const Attacker &opponent) {
130130

131131
void Game::activate_ability(size_t attacker_id) {
132132
this->_ability_activations.push_back(attacker_id);
133+
this-> already_activated_attacker_ids.insert(attacker_id);
133134
}
134135

135136
std::ostringstream &Game::logr() { return this->_logr; }

cpp/player_code.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class Game {
129129
const std::vector<std::pair<size_t, Position>> &get_spawn_positions() const;
130130
const std::set<Position> &get_already_spawned_positions() const;
131131
const std::vector<size_t> &get_ability_activations() const;
132+
static std::set<size_t> already_activated_attacker_ids;
132133
private:
133134
std::unordered_map<size_t, size_t> _player_set_targets;
134135
std::vector<std::pair<size_t, Position>> _spawn_postions;

cpp/run.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ Game run(const State &state) {
8585
if (!attackers.empty() && !defenders.empty()) {
8686
// check if they are empty beforehand to be safe from unexpected errors
8787
game.set_target(attackers.front(), defenders.front());
88+
//lets say i want to activate the ability of the first attacker
89+
//check if ability wasnt activated before to avoid getting penalized
90+
if (!Game::already_activated_attacker_ids.contains(attackers.front().get_id())){
91+
game.activate_ability(attackers.front().get_id());
92+
}
8893
}
8994

9095
// Lets log all the spawned positions for this turn

cpp/runpvp.cpp

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Game run(const PvPState &state) {
1414

1515
game.logr() << "TURN " << state.get_turn_no() << " LOGS:";
1616

17-
// Get all the attackers and defenders in the game and store it
17+
// Get all the attackers and opponent in the game and store it
1818
const std::vector<Attacker> &attackers = state.get_attackers();
1919
const std::vector<Attacker> &opponent_attackers = state.get_opponent_attackers();
2020

@@ -31,55 +31,60 @@ Game run(const PvPState &state) {
3131
// static variable in c++
3232

3333
static int last_spawned = 0;
34-
for (size_t type_id = 1; type_id <= Constants::NO_OF_ATTACKER_TYPES;
35-
type_id++) {
36-
// Spawn the attacker of type_id at position
37-
// all_valid_spawn_positions[last_spawned]
38-
39-
// There are two cases when you might be panalized
40-
// - Spawning at invalid position
41-
// - Spawning at position where you have already spawned one attacker
42-
// in the same turn
43-
//
44-
// We have provided helpers to check just that
45-
46-
// game class will keep track of all your spawned positions for you and
47-
// provides a helper method called already_spawned_at_position(Position)
48-
// to check if you already spawned in the position
49-
50-
// Mostly a good practice to check with these two helpers before spawning,
51-
// to save up on accidental penalties
52-
if (is_valid_spawn_position(all_valid_spawn_positions[last_spawned]) &&
53-
!game.already_spawned_at_position(
54-
all_valid_spawn_positions[last_spawned])) {
55-
// If lets say you had run out of coins left, the game will just ignore
56-
// the spawn
57-
game.spawn_attacker(type_id, all_valid_spawn_positions[last_spawned]);
58-
59-
// This has the starting attributes for the attacker we are about to
60-
// spawn
61-
// For full information about the Attributes class refer the
62-
// documentation
63-
// This can be used for strategizing
64-
Attributes attackers_attributes =
65-
Constants::ATTACKER_TYPE_ATTRIBUTES.at(type_id);
66-
67-
// You can use the logger we provide to show log messages in the
68-
// rendered game
69-
game.logr() << "To to be spawned at Position("
70-
<< all_valid_spawn_positions[last_spawned].get_x() << ","
71-
<< all_valid_spawn_positions[last_spawned].get_y() << ")"
72-
<< '\n';
73-
(last_spawned += 1) %= all_valid_spawn_positions.size();
74-
}
34+
for (size_t type_id = 1; type_id <= Constants::NO_OF_ATTACKER_TYPES;
35+
type_id++) {
36+
// Spawn the attacker of type_id at position
37+
// all_valid_spawn_positions[last_spawned]
38+
39+
// There are two cases when you might be panalized
40+
// - Spawning at invalid position
41+
// - Spawning at position where you have already spawned one attacker
42+
// in the same turn
43+
//
44+
// We have provided helpers to check just that
45+
46+
// game class will keep track of all your spawned positions for you and
47+
// provides a helper method called already_spawned_at_position(Position)
48+
// to check if you already spawned in the position
49+
50+
// Mostly a good practice to check with these two helpers before spawning,
51+
// to save up on accidental penalties
52+
if (is_valid_spawn_position(all_valid_spawn_positions[last_spawned]) &&
53+
!game.already_spawned_at_position(
54+
all_valid_spawn_positions[last_spawned])) {
55+
// If lets say you had run out of coins left, the game will just ignore
56+
// the spawn
57+
game.spawn_attacker(type_id, all_valid_spawn_positions[last_spawned]);
58+
59+
// This has the starting attributes for the attacker we are about to
60+
// spawn
61+
// For full information about the Attributes class refer the
62+
// documentation
63+
// This can be used for strategizing
64+
Attributes attackers_attributes =
65+
Constants::ATTACKER_TYPE_ATTRIBUTES.at(type_id);
66+
67+
// You can use the logger we provide to show log messages in the
68+
// rendered game
69+
game.logr() << "To to be spawned at Position("
70+
<< all_valid_spawn_positions[last_spawned].get_x() << ","
71+
<< all_valid_spawn_positions[last_spawned].get_y() << ")"
72+
<< '\n';
73+
(last_spawned += 1) %= all_valid_spawn_positions.size();
7574
}
75+
}
7676

7777
// Now lets say you always want to set the target for the attackers[0] to
7878
// defenders[0]
7979
// To do that you do
8080
if (!attackers.empty() && !opponent_attackers.empty()) {
8181
// check if they are empty beforehand to be safe from unexpected errors
8282
game.set_target(attackers.front(), opponent_attackers.front());
83+
//lets say i want to activate the ability of the first attacker
84+
//check if ability wasnt activated before to avoid getting penalized
85+
if (!Game::already_activated_attacker_ids.contains(attackers.front().get_id())){
86+
game.activate_ability(attackers.front().get_id());
87+
}
8388
}
8489

8590
// Lets log all the spawned positions for this turn

java/Game.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class Game {
1111
private final Set<Position> _alreadySpawnedPositions;
1212
private final StringBuilder _logr;
1313
private final List<Integer> _ability_activations;
14+
public static Set<Integer> already_activated_attacker_ids;
1415

1516
public Game() {
1617
_playerSetTargets = new HashMap<>();
@@ -27,6 +28,7 @@ public void spawnAttacker(int id, Position pos) {
2728

2829
public void activateAbility(int attacker_id) {
2930
_ability_activations.add(attacker_id);
31+
already_activated_attacker_ids.add(attacker_id);
3032
}
3133

3234
public List<SpawnDetail> getSpawnPositions() {
@@ -53,6 +55,10 @@ public void setTarget(Attacker attacker, Defender defender) {
5355
setTarget(attacker.getId(), defender.getId());
5456
}
5557

58+
public void setTarget(Attacker attacker, Attacker opponent_attacker) {
59+
setTarget(attacker.getId(), opponent_attacker.getId());
60+
}
61+
5662
public void log(String s) {
5763
_logr.append(s + "\n");
5864
}

java/Run.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public Game run(State state) {
9595
if (!attackers.isEmpty() && !defenders.isEmpty()) {
9696
// check if they are empty beforehand to be safe from unexpected errors
9797
game.setTarget(attackers.get(0).getId(), defenders.get(0).getId());
98+
//lets say i want to activate the ability of the first attacker
99+
//check if ability wasnt activated before to avoid getting penalized
100+
if (!Game.already_activated_attacker_ids.contains(attackers.get(0).getId())) {
101+
game.activateAbility(attackers.get(0).getId());
102+
}
98103
}
99104

100105
// Lets log all the spawned positions for this turn

java/RunPvP.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Game run(PvPState state) {
2828
int remainingCoins = state.getCoinsLeft();
2929
game.log("TURN " + state.getTurnNo() + " LOGS:");
3030

31-
// Get all the attackers and defenders in the game and store it
31+
// Get all the attackers and opponent in the game and store it
3232
List<Attacker> attackers = state.getAttackers();
3333
List<Attacker> opponentAttackers = state.getOpponentAttackers();
3434

@@ -91,6 +91,11 @@ public Game run(PvPState state) {
9191
if (!attackers.isEmpty() && !opponentAttackers.isEmpty()) {
9292
// check if they are empty beforehand to be safe from unexpected errors
9393
game.setTarget(attackers.get(0).getId(), opponentAttackers.get(0).getId());
94+
//lets say i want to activate the ability of the first attacker
95+
//check if ability wasnt activated before to avoid getting penalized
96+
if (!Game.already_activated_attacker_ids.contains(attackers.get(0).getId())) {
97+
game.activateAbility(attackers.get(0).getId());
98+
}
9499
}
95100

96101
// Lets log all the spawned positions for this turn

python/player_code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class PvPState:
7070
turn_no: int
7171

7272
class Game:
73+
already_activated_attacker_ids: set[int]
7374
def __init__(self):
7475
self._log = ""
7576
self.player_set_targets: dict[int, int] = {}
@@ -92,6 +93,7 @@ def set_target(self, attacker_id: int, defender_id: int):
9293
def activate_ability(self, attacker_id: int):
9394
assert (type(attacker_id)== int)
9495
self.ability_activations.append(attacker_id)
96+
Game.already_activated_attacker_ids.add(attacker_id)
9597

9698
def log(self, line: str):
9799
self._log += line + "\n"

python/run.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ def run(state: State) -> Game:
8989
#To do that you do
9090
if len(attackers)!=0 and len(defenders)!=0:
9191
game.set_target(attackers[0].id,defenders[0].id)
92-
game.activate_ability(attackers[0].id)
93-
94-
92+
#lets say i want to activate the ability of the first attacker
93+
#check if ability wasnt activated before to avoid getting penalized
94+
if attackers[0].id not in Game.already_activated_attacker_ids:
95+
game.activate_ability(attackers[0].id)
96+
9597
#Lets log all the spawned positions for this turn
9698
for type_id, pos in game.spawn_positions:
9799
game.log("Type {} at Position ({},{})".

0 commit comments

Comments
 (0)