-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyBot.java
153 lines (138 loc) · 4.37 KB
/
MyBot.java
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
150
151
152
153
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.Set;
/**
* Starter bot implementation.
*/
public class MyBot extends Bot {
/**
* Main method executed by the game engine for starting the bot.
*
* @param args command line arguments
*
* @throws IOException if an I/O error occurs
*/
public static void main(String[] args) throws IOException {
new MyBot().readSystemInput();
}
public Set<Tile> unseen = new HashSet<Tile>();
@Override
public void setup(int loadTime, int turnTime, int rows, int cols,
int turns, int viewRadius2, int attackRadius2, int spawnRadius2) {
super.setup(loadTime, turnTime, rows, cols, turns, viewRadius2, attackRadius2, spawnRadius2);
unseen = new HashSet<Tile>();
for(int col = 0; col < cols; ++col) {
for(int row = 0; row < rows; ++row) {
unseen.add(new Tile(row,col));
}
}
}
/**
* For every ant check every direction in fixed order (N, E, S, W) and move it if the tile is
* passable.
*/
@Override
public void doTurn() {
Ants ants = getAnts();
Set<Tile> toMove = new HashSet<Tile>();
Random rnd = new Random();
for(int col = 0; col < ants.getCols(); ++col) {
for(int row = 0; row < ants.getRows(); ++row) {
Tile t = new Tile(row, col);
if(ants.isVisible(t)) unseen.remove(t);
}
}
for (Tile myAnt : ants.getMyAnts()) {
Set<Tile> targets = new HashSet<Tile>(ants.getFoodTiles());
targets.addAll(ants.getEnemyAnts());
targets.addAll(ants.getEnemyHills());
targets.addAll(unseen);
Tile nearest = getNearest(myAnt, targets, ants);
if(nearest == null /*|| ants.getDistance(myAnt, nearest) > ants.getViewRadius2() * 2 */) {
List<Aim> dirs = new ArrayList<Aim>(Arrays.asList(Aim.values()));
Collections.shuffle(dirs);
for(Aim direction : dirs) {
Tile to = ants.getTile(myAnt, direction);
if (ants.getIlk(myAnt, direction).isPassable() && !toMove.contains(to) && !ants.getMyAnts().contains(to)) {
ants.issueOrder(myAnt, direction);
toMove.add(to);
break;
}
}
} else {
Tile to = towards(myAnt, nearest, ants);
if(!toMove.contains(to) && !ants.getMyAnts().contains(to)) {
toMove.add(to);
ants.issueOrder(myAnt, tileToAim(myAnt, to, ants));
} else {
toMove.add(myAnt);
}
}
}
}
private Tile getNearest(Tile myAnt, Set<Tile> foodTiles, Ants ants) {
int min = Integer.MAX_VALUE;
Tile nearest = null;
for(Tile t : foodTiles) {
int d = ants.getDistance(myAnt, t);
if(d < min) {
min = d;
nearest = t;
}
}
return nearest;
}
public static class Node {
public Node(Tile from, List<Tile> path2) {
t = from;
path = path2;
}
public Tile t;
public List<Tile> path = new ArrayList<Tile>();
}
public static List<Tile> getPath(Tile from, Tile to, Ants world) {
Queue<Node> tbd = new ArrayDeque<MyBot.Node>();
List<Tile> path = new ArrayList<Tile>();
Set<Tile> visited = new HashSet<Tile>();
path.add(from);
visited.add(from);
tbd.add(new Node(from, path));
while(!tbd.isEmpty()) {
Node n = tbd.remove();
if(n.t.equals(to)) return n.path;
for(Tile nt : getNeighbours(n.t, world)) {
if(!visited.contains(nt) && world.getIlk(nt).isPassable()) {
List<Tile> p = new ArrayList<Tile>(n.path);
p.add(nt);
tbd.add(new Node(nt, p));
visited.add(nt);
}
}
}
return Collections.emptyList();
}
private static Set<Tile> getNeighbours(Tile t, Ants world) {
Set<Tile> result = new HashSet<Tile>();
for(Aim d : Aim.values()) {
result.add(world.getTile(t, d));
}
return result;
}
public static Aim tileToAim(Tile from, Tile next, Ants world) {
return world.getDirections(from, next).get(0);
}
public static Tile towards(Tile from, Tile to, Ants world) {
List<Tile> path = getPath(from, to, world);
// TODO : Jotain parempaa!
if(path.size() < 2) return getNeighbours(from, world).iterator().next();
Tile next = path.get(1);
return next;
}
}