-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapStrategy.java
More file actions
149 lines (121 loc) · 4.52 KB
/
HeapStrategy.java
File metadata and controls
149 lines (121 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import hlt.Command;
import hlt.Constants;
import hlt.Direction;
import hlt.EntityId;
import hlt.Game;
import hlt.GameMap;
import hlt.Log;
import hlt.MapCell;
import hlt.Player;
import hlt.Position;
import hlt.Ship;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
public class HeapStrategy {
private static final int MIN_SHIPS = 10;
private static final int MAX_SHIPS = 25;
private static final int LAST_TURN_CREATE_SHIPS = 200;
private static MaxHeap MAX_HEAP;
private static Game game;
private static Random random;
private Map<EntityId, Position> shipCell = new HashMap<>();
public HeapStrategy(Game game, Random random) {
HeapStrategy.game = game;
HeapStrategy.random = random;
generateHeap(game.gameMap);
}
public static void generateHeap(GameMap map) {
int heapSize = map.width * map.height;
MAX_HEAP = new MaxHeap(heapSize, map.cells[0][0]);
for (int y = 0; y < 32; ++y) {
for (int x = 0; x < 32; ++x) {
MapCell currentCell = map.cells[y][x];
MAX_HEAP.insert(currentCell);
}
}
}
private static boolean haveEnoughHalite(Game game) {
return game.me.halite >= Constants.SHIP_COST;
}
private boolean removeDeadShips(Iterator<EntityId> shipIterator) {
boolean removed = false;
while(shipIterator.hasNext()){
EntityId shipId = shipIterator.next();
if(!game.me.ships.containsKey(shipId)){
shipIterator.remove();;
Log.log("removing collided ship "+ shipId);
removed = true;
}
}
return removed;
}
public ArrayList<Command> getCommand() {
final ArrayList<Command> commandQueue = new ArrayList<>();
final Player me = game.me;
final GameMap gameMap = game.gameMap;
removeDeadShips(me.ships.keySet().iterator());
for (final Ship ship : me.ships.values()) {
if (ship.isFull()) {
shipCell.put(ship.id, me.shipyard.position);
}
if (gameMap.at(ship).halite < Constants.MAX_HALITE / 10 || ship.isFull()) {
if (ship.position.equals(me.shipyard.position)) {
shipCell.put(ship.id, MAX_HEAP.extractMax().position);
}
Position shipIntendedPosition = shipCell.get(ship.id);
if (ship.position.equals(shipIntendedPosition)) {
shipIntendedPosition = me.shipyard.position;
shipCell.put(ship.id, shipIntendedPosition);
}
Direction direction = navigate(ship, shipIntendedPosition);
commandQueue.add(ship.move(direction));
} else {
commandQueue.add(ship.stayStill());
}
}
if (shouldSpawn()) {
commandQueue.add(game.me.shipyard.spawn());
}
return commandQueue;
}
private static boolean shouldSpawn() {
if (!haveEnoughHalite(game)) {
return false;
}
if (game.me.ships.values().size() > MAX_SHIPS) {
return false;
}
if (game.gameMap.at(game.me.shipyard.position).isOccupied()) {
return false;
}
if (game.turnNumber % 10 == 0 || game.turnNumber == 1) {
if (game.me.ships.size() < MIN_SHIPS) {
return true;
}
return game.turnNumber < LAST_TURN_CREATE_SHIPS;
}
return false;
}
public Direction navigate(Ship ship, Position targetPosition){
for (Direction direction : game.gameMap.getUnsafeMoves(ship.position, targetPosition)) {
Position targetPos = ship.position.directionalOffset(direction);
if (!game.gameMap.at(targetPos).isOccupied()) {
game.gameMap.at(targetPos).markUnsafe(ship);
return direction;
}
}
ArrayList<Direction> directionOptions = new ArrayList<>(Direction.ALL_CARDINALS);
while(directionOptions.size() > 0){
Direction direction = directionOptions.remove(random.nextInt(directionOptions.size()));
final Position targetPos = ship.position.directionalOffset(direction);
if (!game.gameMap.at(targetPos).isOccupied()) {
game.gameMap.at(targetPos).markUnsafe(ship);
return direction;
}
}
return Direction.STILL;
}
}