-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBetter.java
372 lines (320 loc) · 14.5 KB
/
Better.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package planetwars.strategies;
import planetwars.core.*;
import planetwars.publicapi.*;
import java.util.*;
import java.lang.Math.*;
public class Better implements IStrategy{
private HashMap<Double, Long> planetMoves;
private int homeID;
private IVisiblePlanet initial;
private List<Integer> ownedPlanets;
private List<Integer> attackPlanets;
private HashMap<Integer, IVisiblePlanet> idToPlanet;
private HashMap<Integer, Integer> idToHelp;
private List<Integer> help;
private List<Integer> neutral;
private int move=0;
@Override
public void takeTurn(List<IPlanet> planets, IPlanetOperations planetOperations, Queue<IEvent> eventsToExecute) {
boolean sent = false;
updatePlanets(planets);
planetMap(planets);
updateHelp(planets);
long num=0;
// updateHelpMap(planets);
/*
System.out.println("OWNED: " + ownedPlanets.size());
System.out.println("ATTACK: " + attackPlanets.size());
System.out.println("HELP: " + help.size());
System.out.println("NEUTRAL: " + neutral.size());
System.out.println("ALL: " + idToPlanet.size());
*/
for (Integer i : ownedPlanets) {
List<IVisiblePlanet> ed = findEdges(planets, idToPlanet.get(i));
long pop = idToPlanet.get(i).getPopulation();
int len = ed.size();
for (IVisiblePlanet plan : ed) {
if (attackPlanets.contains(plan.getId())) {
long nPop = getNetPop(plan, 4);
long me = getNetPop(idToPlanet.get(i), 4);
if (nPop < 0 && -idToPlanet.get(i).getPopulation() < nPop - 5 && me > 0) {
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i), plan, Math.abs(nPop) *2));
pop += nPop;
sent = true;
} else if ((pop > Math.abs(nPop) + 15) && nPop < 0 && me > 0) {
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i), plan, Math.abs(nPop)));
pop += nPop;
sent = true;
}
}
if (help.contains(i) && plan.getOwner() == Owner.SELF) {
if (plan.getPopulation() >= (plan.getSize() / 5)) {
long netR = pop;
long netS = getNetPop(plan, 5);
long toSend = netS - plan.getPopulation();
eventsToExecute.add(planetOperations.transferPeople(plan, idToPlanet.get(i), 5 * toSend));
pop -= (pop / 4);
sent = true;
System.out.println("HELPED");
}
}
if (neutral.contains(plan.getId()) && idToPlanet.get(i).getPopulation() > 4) {
if (plan.getHabitability() > idToPlanet.get(i).getHabitability()) {
num += 2;
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i), plan, 2));
} else if (idToPlanet.get(i).getPopulation() > 3) {
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i), plan, 1));
num += 1;
}
}
/* if (friendlyNeighbors(planets, idToPlanet.get(i)) && !sent) {
List<IVisiblePlanet> lst = findEdges(planets, idToPlanet.get(i));
for (IVisiblePlanet planet : lst) {
if (idToHelp.containsKey(planet.getId()) && idToHelp.get(planet.getId()) < idToHelp.get(i)) {
System.out.println("Here");
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(planet.getId()),
idToPlanet.get(i), 9));
}
}
Set<IEdge> edges = idToPlanet.get(i).getEdges();
for(IEdge e : edges) {
if(idToHelp.get(i) < idToHelp.get(e.getDestinationPlanetId())) {
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i),
idToPlanet.get(e.getDestinationPlanetId()), pop-1));
System.out.println("Here");
}
}
} */
if (nextAbove(idToPlanet.get(i))) {
long toSend = (pop / len) - 1;
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i), plan, toSend));
}
}
if (friendlyNeighbors(planets, idToPlanet.get(i)) && !sent && pop > 15) {
for (IVisiblePlanet pl : ed) {
eventsToExecute.add(planetOperations.transferPeople(idToPlanet.get(i), pl, (pop - num)));
}
}
}
}
@Override
public String getName() {
return "Strat";
}
@Override
public boolean compete() {
return false;
}
public List<IVisiblePlanet> findEdges(List<IPlanet> all, IVisiblePlanet planetOwn) {
Set<IEdge> edges = planetOwn.getEdges();
List<IVisiblePlanet> ret = new ArrayList<>();
for(IEdge edge : edges) {
ret.add(idToPlanet.get(edge.getDestinationPlanetId()));
}
return ret;
}
public void updatePlanets(List<IPlanet> planets) {
ownedPlanets = new ArrayList<>();
attackPlanets = new ArrayList<>();
neutral = new ArrayList<>();
for(IPlanet planet : planets) {
if (planet instanceof IVisiblePlanet && ((IVisiblePlanet) planet).getOwner() == Owner.SELF) {
ownedPlanets.add(planet.getId());
} else if(planet instanceof IVisiblePlanet && ((IVisiblePlanet) planet).getOwner() == Owner.OPPONENT) {
attackPlanets.add(planet.getId());
} else if(planet instanceof IVisiblePlanet) {
neutral.add(planet.getId());
}
}
}
public void updateHelp(List<IPlanet> all) {
help = new ArrayList<>();
for(Integer i : ownedPlanets) {
List<IVisiblePlanet> lst = findEdges(all,idToPlanet.get(i));
for(IVisiblePlanet plan : lst) {
if(attackPlanets.contains(plan.getId()) && !help.contains(plan.getId())) {
help.add(idToPlanet.get(i).getId());
}
}
}
}
public void updateHelpMap(List<IPlanet> all) { // HashMap stored as <Planet ID, Help #>
HashMap<Integer, Integer> idToHelp = new HashMap<>();
if(help.size() == 0) {
return;
} else {
for (Integer hlp : help) {
idToHelp.put(hlp, 0);
} // Grabs ID's in help list already
System.out.println("COMPARING: " + idToHelp.size() +" & " + ownedPlanets.size());
for (Integer i : idToHelp.keySet()) {
List<IVisiblePlanet> edge = findEdges(all, idToPlanet.get(i));
for (IVisiblePlanet plan : edge) { // some planet sharing an edge
if (ownedPlanets.contains(plan.getId()) && !idToHelp.containsKey(plan.getId())) { // new ID
idToHelp.put(plan.getId(), idToHelp.get(idToPlanet.get(i).getId()) + 1);
} else if (idToHelp.containsKey(plan.getId()) && idToHelp.get(i) > idToHelp.get(plan.getId())) { // ID we've seen
idToHelp.replace(i, idToHelp.get(plan.getId()) + 1);
System.out.println("HERE SOMEHOW");
}
}
}
System.out.println("---------------------------------");
/*for(Integer i : idToHelp.keySet()){
System.out.println("ID: " + i+ " HELP_LEVEL: " + idToHelp.get(i));
}*/
}
}
private void updateHelpMapWrapper(List<IPlanet> all) {
int count = ownedPlanets.size();
int o =0;
while(o<count) {
for (Integer i : idToHelp.keySet()) {
List<IVisiblePlanet> edge = findEdges(all, idToPlanet.get(i));
for (IVisiblePlanet plan : edge) { // some planet sharing an edge
if (ownedPlanets.contains(plan.getId()) && !idToHelp.containsKey(plan.getId())) { // new ID
o++;
idToHelp.put(plan.getId(), idToHelp.get(idToPlanet.get(i).getId()) + 1);
} else if (idToHelp.containsKey(plan.getId()) && idToHelp.get(i) > idToHelp.get(plan.getId())) { // ID we've seen
idToHelp.replace(i, idToHelp.get(plan.getId()) + 1);
System.out.println("HERE SOMEHOW");
o++;
}
}
}
}
}
public void planetMap(List<IPlanet> planets){
idToPlanet = new HashMap<>();
for(IPlanet p: planets){
if(p instanceof IVisiblePlanet) {
idToPlanet.put(p.getId(), (IVisiblePlanet) p);
}
}
}
public IVisiblePlanet getHome(List<IPlanet> all) {
for(IPlanet planet : all) {
if(planet instanceof IVisiblePlanet) {
if(((IVisiblePlanet) planet).getOwner() == Owner.SELF){
System.out.println("HAVE HOME");
return (IVisiblePlanet) planet;
}
}
}
return null;
}
public boolean friendlyNeighbors(List<IPlanet> planets, IVisiblePlanet plan){
List<IVisiblePlanet> lst = findEdges(planets,plan);
for(IVisiblePlanet planet : lst) {
if(planet.getOwner() != Owner.SELF){
return false;
}
}
return true;
}
public long numFighting(List<IPlanet> planets, IVisiblePlanet planet) {
long ret = 0;
List<IVisiblePlanet> lst = findEdges(planets,planet);
for(IVisiblePlanet plan : lst) {
if(planet.getOwner() == Owner.OPPONENT){
ret++;
}
}
return ret;
}
public boolean nextAbove(IVisiblePlanet planet) {
double pop = planet.getPopulation();
double h = planet.getHabitability();
double m = planet.getSize();
pop = pop * (1 + (h / 100));
return pop >= m;
}
public double aboveMax(IVisiblePlanet planet) {
double pop = planet.getPopulation();
double h = planet.getHabitability();
double m = planet.getSize();
pop = pop * (1 + (h / 100));
double ret = pop - m;
return ret;
}
public long getNetPop(IVisiblePlanet planet, int numTurns) {
List<IShuttle> shuttle = planet.getIncomingShuttles();
int turns;
int temp = 0;
long pop;
long ret = 0;
int ind=0;
if(planet.getOwner() == Owner.SELF) {
turns = 100;
pop = planet.getPopulation();
if(shuttle.size() != 0) {
while (shuttle.size() > 0) {
for (int i = 0; i < shuttle.size(); i++) {
if (shuttle.get(i).getTurnsToArrival() < turns) {
turns = shuttle.get(i).getTurnsToArrival();
ind = i;
}
}
// We now have the minimum number of turns to the next arrival, need to see who owns it to proceed
if (shuttle.get(ind).getOwner() == Owner.SELF && shuttle.get(ind).getTurnsToArrival() <= numTurns) {
int h = planet.getHabitability();
int max = (int) planet.getSize();
long incoming = shuttle.get(ind).getNumberPeople();
for (int i = 0; i < turns - temp; i++) { // temp is a counter of how many steps have already been taken
pop = pop * (1 + (h / 100));
if (pop >= max) {
pop = pop - ((pop - max) * (1/10));
}
}
pop += incoming;
// subtract number of turns
temp += turns; // updates the counter
} else if(shuttle.get(ind).getTurnsToArrival() <= numTurns){
int h = planet.getHabitability();
int max = (int) planet.getSize();
long incoming = shuttle.get(ind).getNumberPeople();
for (int i = 0; i < turns - temp; i++) { // temp is a counter of how many steps have already been taken
pop = pop * (1 + (h / 100));
if (pop >= max) {
pop = pop - ((pop - max) * (1/10));
}
}
pop -= incoming;
temp += (turns - temp);
}
shuttle.remove(ind);
turns = 100;
}
}
ret = pop;
} else if (planet.getOwner() == Owner.OPPONENT) {
turns = 100;
pop = -planet.getPopulation();
while(shuttle.size() > 0) {
for (int i = 0; i < shuttle.size(); i++) {
if (shuttle.get(i).getTurnsToArrival() < turns) {
turns = shuttle.get(i).getTurnsToArrival();
ind = i;
}
} // grabs index of minimum turns
int h = planet.getHabitability();
int max = (int) planet.getSize();
long incoming = shuttle.get(ind).getNumberPeople();
for (int i = 0; i < turns - temp; i++) { // temp is a counter of how many steps have already been taken
pop = pop * (1 + (h / 100));
if (pop >= max) {
pop = pop - ((pop - max) * (1/10));
}
} // updated values of the population on the planet with growth/decay
if (shuttle.get(ind).getOwner() == Owner.OPPONENT && shuttle.get(ind).getTurnsToArrival() <= numTurns) {
pop -= shuttle.get(ind).getNumberPeople();
} else if (shuttle.get(ind).getTurnsToArrival() <= numTurns) { // Owner is we
pop += incoming;
}
temp += (turns - temp);
shuttle.remove(ind);
}
ret = pop;
}
return ret;
}
}