-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwood_2_league.java
333 lines (277 loc) · 8.72 KB
/
wood_2_league.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
import java.util.*;
import java.io.*;
import java.math.*;
class Player {
private static List<Spot> listOfSpots = new ArrayList<>();
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int numSites = in.nextInt();
for (int i = 0; i < numSites; i++) {
int siteId = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int radius = in.nextInt();
Spot spot = new Spot(siteId, x, y, radius);
listOfSpots.add(spot);
}
listOfSpots.sort(Comparator.comparing(spot -> spot.getId()));
int counter = 0;
// game loop
while (true) {
List<CurrentSpot> listOfCurrentSpots = new ArrayList<>();
int gold = in.nextInt();
int touchedSite = in.nextInt(); // -1 if none
for (int i = 0; i < numSites; i++) {
int siteId = in.nextInt();
int ignore1 = in.nextInt(); // used in future leagues
int ignore2 = in.nextInt(); // used in future leagues
int structureType = in.nextInt(); // -1 = No structure, 1 = Tower, 2 = Barracks
int owner = in.nextInt(); // -1 = No structure, 0 = Friendly, 1 = Enemy
int param1 = in.nextInt();
int param2 = in.nextInt();
for(Spot spot : listOfSpots){
if(spot.getId() == siteId){
CurrentSpot currentSpot = new CurrentSpot(siteId, ignore1, ignore2,
structureType, owner, param1, param2, spot.getX(), spot.getY(), spot.getRadius());
listOfCurrentSpots.add(currentSpot);
}
}
}
List <Unit> listOfUnits = new ArrayList<>();
int numUnits = in.nextInt();
for (int i = 0; i < numUnits; i++) {
int x = in.nextInt();
int y = in.nextInt();
int owner = in.nextInt();
int unitType = in.nextInt(); // -1 = QUEEN, 0 = KNIGHT, 1 = ARCHER, 2 = GIANT
int health = in.nextInt();
Unit unit = new Unit(x, y, owner, unitType, health);
listOfUnits.add(unit);
}
Queen myQueen = findQueen(listOfUnits, listOfCurrentSpots);
int closestSpot = myQueen.getClosestSpot();
String build = myQueen.building();
String toTrain = myQueen.idOfSpotToTrain(gold);
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
// First line: A valid queen action
// Second line: A set of training instructions
if(closestSpot != -1){
System.out.println("BUILD "+ closestSpot + " " + build);
} else {
System.out.println("WAIT");
}
System.out.println("TRAIN " + toTrain);
}
}
public static boolean isFriendly(int owner){
return owner == 0;
}
public static boolean isQueen(int unitType){
return unitType == -1;
}
public static Queen findQueen(List<Unit> listOfUnits, List<CurrentSpot> listOfCurrentSpots){
for(Unit unit : listOfUnits){
if(isFriendly(unit.getOwner()) && isQueen(unit.getUnitType())){
return new Queen(listOfCurrentSpots, listOfUnits, unit.getX(), unit.getY(), unit.getHealth());
}
}
return new Queen(listOfCurrentSpots, listOfUnits, listOfUnits.get(0).getX(), listOfUnits.get(0).getY(), listOfUnits.get(0).getHealth());
}
}
class Spot {
private int id;
private int x;
private int y;
private int radius;
public Spot(int id, int x, int y, int radius) {
this.id = id;
this.x = x;
this.y = y;
this.radius = radius;
}
public int getId() {
return id;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getRadius() {
return radius;
}
}
class CurrentSpot {
private int spotId;
private int ignore1;
private int ignore2;
private int structureType;
private int owner;
private int param1;
private int param2;
private int x;
private int y;
private int radius;
public CurrentSpot(int spotId, int ignore1, int ignore2, int structureType, int owner,
int param1, int param2, int x, int y, int radius) {
this.spotId = spotId;
this.ignore1 = ignore1;
this.ignore2 = ignore2;
this.structureType = structureType;
this.owner = owner;
this.param1 = param1;
this.param2 = param2;
this.x = x;
this.y = y;
this.radius = radius;
}
public int getSpotId() {
return spotId;
}
public int getIgnore1() {
return ignore1;
}
public int getIgnore2() {
return ignore2;
}
public int getStructureType() {
return structureType;
}
public int getOwner() {
return owner;
}
public int getParam1() {
return param1;
}
public int getParam2() {
return param2;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public int getRadius(){
return radius;
}
}
class Unit {
private int x;
private int y;
private int owner;
private int unitType;
private int health;
public Unit(int x, int y, int owner, int unitType, int health) {
this.x = x;
this.y = y;
this.owner = owner;
this.unitType = unitType;
this.health = health;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getOwner() {
return owner;
}
public int getUnitType() {
return unitType;
}
public int getHealth() {
return health;
}
}
class Queen {
private List<CurrentSpot> listOfCurrentSpots;
private List<Unit> listOfUnits;
private List<CurrentSpot> listOfFreeSpots = new ArrayList<>();
private List<CurrentSpot> listOfEnemySpots = new ArrayList<>();
private int x;
private int y;
private int health;
public Queen(List<CurrentSpot> listOfCurrentSpots, List<Unit> listOfUnits, int x, int y, int health) {
this.listOfCurrentSpots = listOfCurrentSpots;
this.listOfUnits = listOfUnits;
this.x = x;
this.y = y;
this.health = health;
}
public List<CurrentSpot> getListOfCurrentSpots() {
return listOfCurrentSpots;
}
public List<Unit> getListOfUnits() {
return listOfUnits;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getHealth() {
return health;
}
public double getDistance(int x1, int y1, int x2, int y2) {
int firstDifference = x2 - x1;
int secondDifference = y2 - y1;
return Math.sqrt(Math.pow(firstDifference, 2) + Math.pow(secondDifference, 2));
}
public List<CurrentSpot> getFreeSpots(){
for(CurrentSpot spot : listOfCurrentSpots){
if(spot.getOwner() == -1){
listOfFreeSpots.add(spot);
}
}
return listOfFreeSpots;
}
public List<CurrentSpot> getEnemySpots(){
for(CurrentSpot spot : listOfCurrentSpots){
if(spot.getOwner() == 1){
listOfEnemySpots.add(spot);
}
}
return listOfEnemySpots;
}
public int getClosestSpot() {
listOfFreeSpots = getFreeSpots();
listOfEnemySpots = getEnemySpots();
int result = -1;
double difference = 999999999;
for (CurrentSpot spot : listOfFreeSpots) {
double count = getDistance(this.x, this.y, spot.getX(), spot.getY());
if (count < difference) {
result = spot.getSpotId();
difference = count;
}
}
return result;
}
public String idOfSpotToTrain(int gold){
String idToTrain = String.valueOf(0);
for(CurrentSpot spot : listOfCurrentSpots){
if(Player.isFriendly(spot.getOwner()) && spot.getStructureType() == 2){
idToTrain = String.valueOf(spot.getSpotId());
}
}
return idToTrain;
}
public String building() {
int counter = 0;
for(CurrentSpot spot : listOfCurrentSpots){
if(Player.isFriendly(spot.getOwner()) && spot.getStructureType() == 2){
counter ++;
}
}
if (counter > 1) {
return "TOWER";
} else {
return "BARRACKS-KNIGHT";
}
}
}