|
| 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 |
1 | 9 | 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 | + |
2 | 23 | public Game run(State state) { |
| 24 | + |
| 25 | + // Always start by instantiating a Game class object |
3 | 26 | 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())); |
6 | 105 | } |
| 106 | + |
| 107 | + // always return the game object |
7 | 108 | return game; |
8 | 109 | } |
9 | 110 | } |
0 commit comments