Skip to content

Commit 68f3cb9

Browse files
authored
Java default code and new Java SpawnDetail class (#2)
1 parent 2627998 commit 68f3cb9

File tree

6 files changed

+164
-20
lines changed

6 files changed

+164
-20
lines changed

cpp/run.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ Game run(const State &state) {
6464
// spawn
6565
// For full information about the Attributes class refer the
6666
// documentation
67+
// This can be used for strategizing
6768
Attributes attackers_attributes =
6869
Constants::ATTACKER_TYPE_ATTRIBUTES.at(type_id);
6970

7071
// You can use the logger we provide to show log messages in the
7172
// rendered game
72-
game.logr() << "(" << attackers_attributes.hp << ","
73-
<< attackers_attributes.attack_power
74-
<< ") to be spawned at Position("
73+
game.logr() << "To to be spawned at Position("
7574
<< all_valid_spawn_positions[last_spawned].get_x() << ","
7675
<< all_valid_spawn_positions[last_spawned].get_y() << ")"
7776
<< '\n';

java/Game.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1+
import java.util.ArrayList;
12
import java.util.HashMap;
23
import java.util.HashSet;
4+
import java.util.List;
35
import java.util.Map;
46
import java.util.Set;
57

68
public class Game {
79
private final Map<Integer, Integer> _playerSetTargets;
8-
private final Map<Integer, Position> _spawnPositions;
10+
private final List<SpawnDetail> _spawnPositions;
911
private final Set<Position> _alreadySpawnedPositions;
1012
private final StringBuilder _logr;
1113

1214
public Game() {
1315
_playerSetTargets = new HashMap<>();
14-
_spawnPositions = new HashMap<>();
16+
_spawnPositions = new ArrayList<>();
1517
_alreadySpawnedPositions = new HashSet<>();
1618
_logr = new StringBuilder();
1719
}
1820

1921
public void spawnAttacker(int id, Position pos) {
20-
_spawnPositions.put(id, pos);
22+
_spawnPositions.add(new SpawnDetail(id, pos));
2123
_alreadySpawnedPositions.add(pos);
2224
}
2325

24-
public Map<Integer, Position> getSpawnPositions() {
26+
public List<SpawnDetail> getSpawnPositions() {
2527
return _spawnPositions;
2628
}
2729

java/Main.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ private static State nextState(int currentTurnNo) {
1212
int noOfActiveAttackers = in.nextInt();
1313
List<Attacker> attackers = new ArrayList<>();
1414
for (int i = 0; i < noOfActiveAttackers; i++) {
15-
attackers.add(new Attacker(in.nextInt(), in.nextInt(), in.nextInt(), new Position(in.nextInt(), in.nextInt())));
15+
attackers.add(
16+
new Attacker(in.nextInt(), in.nextInt(), in.nextInt(), new Position(in.nextInt(), in.nextInt())));
1617
}
1718

1819
int noOfActiveDefenders = in.nextInt();
1920
List<Defender> defenders = new ArrayList<>();
2021
for (int i = 0; i < noOfActiveDefenders; i++) {
21-
defenders.add(new Defender(in.nextInt(), in.nextInt(), in.nextInt(), new Position(in.nextInt(), in.nextInt())));
22+
defenders.add(
23+
new Defender(in.nextInt(), in.nextInt(), in.nextInt(), new Position(in.nextInt(), in.nextInt())));
2224
}
2325

2426
int coinsLeft = in.nextInt();
@@ -41,18 +43,18 @@ private static GameMap getInitialMap() {
4143
}
4244

4345
private static void output(State state, Game game) {
44-
46+
4547
String log = game.getLog();
4648

4749
System.err.println("TURN " + state.getTurnNo());
4850
System.err.println(log);
4951
System.err.println("ENDLOG");
5052

51-
Map<Integer, Position> spawnPositions = game.getSpawnPositions();
53+
List<SpawnDetail> spawnPositions = game.getSpawnPositions();
5254

5355
System.out.println(spawnPositions.size());
54-
for (Map.Entry<Integer, Position> entry : spawnPositions.entrySet()) {
55-
System.out.println(entry.getKey() + " " + entry.getValue().getX() + " " + entry.getValue().getY());
56+
for (SpawnDetail entry : spawnPositions) {
57+
System.out.println(entry.getTypeId() + " " + entry.getPos().getX() + " " + entry.getPos().getY());
5658
}
5759

5860
Map<Integer, Integer> playerSetTargets = game.getPlayerSetTargets();
@@ -67,15 +69,17 @@ public static void main(String[] args) {
6769
Constants.MAX_NO_OF_COINS = in.nextInt();
6870

6971
Constants.NO_OF_ATTACKER_TYPES = in.nextInt();
70-
Constants.ATTACKER_TYPE_ATTRIBUTES = new HashMap<Integer,Attributes>();
72+
Constants.ATTACKER_TYPE_ATTRIBUTES = new HashMap<Integer, Attributes>();
7173
for (int i = 1; i <= Constants.NO_OF_ATTACKER_TYPES; i++) {
72-
Constants.ATTACKER_TYPE_ATTRIBUTES.put(i, new Attributes(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()));
74+
Constants.ATTACKER_TYPE_ATTRIBUTES.put(i,
75+
new Attributes(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()));
7376
}
7477

7578
Constants.NO_OF_DEFENDER_TYPES = in.nextInt();
76-
Constants.DEFENDER_TYPE_ATTRIBUTES = new HashMap<Integer,Attributes>();
79+
Constants.DEFENDER_TYPE_ATTRIBUTES = new HashMap<Integer, Attributes>();
7780
for (int i = 1; i <= Constants.NO_OF_DEFENDER_TYPES; i++) {
78-
Constants.DEFENDER_TYPE_ATTRIBUTES.put(i, new Attributes(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()));
81+
Constants.DEFENDER_TYPE_ATTRIBUTES.put(i,
82+
new Attributes(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt()));
7983
}
8084

8185
GameMap map = getInitialMap();
@@ -86,7 +90,7 @@ public static void main(String[] args) {
8690
Run run = new Run();
8791
Game game = run.run(state);
8892
output(state, game);
89-
93+
9094
for (int i = 0; i < Constants.NO_OF_TURNS; i++) {
9195
state = nextState(state.getTurnNo());
9296
game = run.run(state);

java/Run.java

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,110 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
// This initial code is well commented and serves as a small tutorial for game
5+
// APIs, for more information you can refer to the documentation
6+
7+
// This is the function player has to fill
8+
// You can define any new functions here that you want
19
public class Run {
10+
11+
// Lets say I want to spawn an attacker of each of the type in one turn
12+
// and I want to use the Helpers.getAllValidSpawnPositions list as well. In
13+
// order to keep track of the last index in the list that we spawned at, we can
14+
// use a private variable. We'll initialize it in the constructor for this
15+
// class. This class is only instantiated once so it's safe to use a private
16+
// variable.
17+
private int lastSpawned;
18+
19+
public Run() {
20+
this.lastSpawned = 0;
21+
}
22+
223
public Game run(State state) {
24+
25+
// Always start by instantiating a Game class object
326
Game game = new Game();
4-
for (var spawnPosition: Helpers.getAllValidSpawnPositions()) {
5-
game.spawnAttacker(1, spawnPosition);
27+
28+
int remainingCoins = state.getCoinsLeft();
29+
game.log("TURN " + state.getTurnNo() + " LOGS:");
30+
31+
// Get all the attackers and defenders in the game and store it
32+
List<Attacker> attackers = state.getAttackers();
33+
List<Defender> defenders = state.getDefenders();
34+
35+
// The function get_all_valid_spawn_positions() is a helper which will give us
36+
// the list of valid spawn positions in map.
37+
// If the position we're spawning is not one of these, the player will be
38+
// penalized by deducting the spawn cost but not spawning the attacker
39+
40+
// The Helpers.getAllValidSpawnPositions() function
41+
// returns a Set<Position>, if we want to index on it,
42+
// we must convert to an indexable collection
43+
List<Position> allValidSpawnPositons = new ArrayList<Position>(Helpers.getAllValidSpawnPositions());
44+
45+
// If there's no defenders left,we can stop spawning and save up on coins,
46+
// which are important for boosting game score
47+
if (!defenders.isEmpty()) {
48+
for (int typeId = 1; typeId <= Constants.NO_OF_ATTACKER_TYPES; typeId++) {
49+
// Spawn the attacker of typeId at position
50+
// allValidSpawnPositions[last_spawned]
51+
52+
// There are two cases when you might be panalized
53+
// - Spawning at invalid position
54+
// - Spawning at position where you have already spawned one attacker
55+
// in the same turn
56+
//
57+
// We have provided helpers to check just that
58+
59+
// game class will keep track of all your spawned positions for you and
60+
// provides a helper method called already_spawned_at_position(Position)
61+
// to check if you already spawned in the position
62+
63+
// Mostly a good practice to check with these two helpers before spawning,
64+
// to save up on accidental penalties
65+
//
66+
67+
if (Helpers.isValidSpawnPosition(allValidSpawnPositons.get(lastSpawned)) &&
68+
!game.alreadySpawnedAtPosition(
69+
allValidSpawnPositons.get(lastSpawned))) {
70+
71+
// If lets say you had run out of coins left, the game will just ignore
72+
// the spawn
73+
game.spawnAttacker(typeId, allValidSpawnPositons.get(lastSpawned));
74+
75+
// This has the starting attributes for the attacker we are about to
76+
// spawn
77+
// For full information about the Attributes class refer the
78+
// documentation
79+
// This information can be used for strategizing
80+
Attributes attackersAttributes = Constants.ATTACKER_TYPE_ATTRIBUTES.get(typeId);
81+
82+
Position pos = allValidSpawnPositons.get(lastSpawned);
83+
game.log(String.format("To be spawned at Position(%d,%d)\n", pos.getX(), pos.getY()));
84+
85+
lastSpawned += 1;
86+
lastSpawned %= allValidSpawnPositons.size();
87+
}
88+
89+
}
90+
}
91+
92+
// Now lets say you always want to set the target for the attackers[0] to
93+
// defenders[0]
94+
// To do that you do
95+
if (!attackers.isEmpty() && !defenders.isEmpty()) {
96+
// check if they are empty beforehand to be safe from unexpected errors
97+
game.setTarget(attackers.get(0).getId(), defenders.get(0).getId());
98+
}
99+
100+
// Lets log all the spawned positions for this turn
101+
for (var entry : game.getSpawnPositions()) {
102+
game.log(String.format("Type %d at Position(%d, %d)",
103+
entry.getTypeId(), entry.getPos().getX(),
104+
entry.getPos().getY()));
6105
}
106+
107+
// always return the game object
7108
return game;
8109
}
9110
}

java/SpawnDetail.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class SpawnDetail {
2+
private int typeId;
3+
private Position pos;
4+
5+
public SpawnDetail(int typeId, Position pos) {
6+
this.typeId = typeId;
7+
this.pos = pos;
8+
}
9+
10+
@Override
11+
public boolean equals(Object obj) {
12+
if (this == obj)
13+
return true;
14+
if (obj == null)
15+
return false;
16+
if (getClass() != obj.getClass())
17+
return false;
18+
SpawnDetail other = (SpawnDetail) obj;
19+
if (pos == null) {
20+
if (other.pos != null)
21+
return false;
22+
} else if (!pos.equals(other.pos))
23+
return false;
24+
if (typeId != other.typeId)
25+
return false;
26+
return true;
27+
}
28+
29+
public int getTypeId() {
30+
return typeId;
31+
}
32+
33+
public Position getPos() {
34+
return pos;
35+
}
36+
37+
}

python/run.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def run(state: State) -> Game:
7373
# rendered game
7474
# For full information about the AttackerType class refer the
7575
# documentation
76+
# This information can be used for strategizing
7677
attackers_attributes: AttackerType = Constants.ATTACKER_TYPE_ATTRIBUTES[type_id]
7778

7879
# You can use the logger we provide to show log messages in the

0 commit comments

Comments
 (0)