@@ -64,23 +64,15 @@ public static void main(String args[]) {
6464 final Ship myBestShip = myShips .stream ().max (Comparator .comparingInt (o -> o .rum )).orElseThrow (RuntimeException ::new );
6565 final Ship opponentBestShip = enemyShips .stream ().max (Comparator .comparingInt (o -> o .rum )).orElseThrow (RuntimeException ::new );
6666 for (final Ship myShip : myShips ) {
67- final boolean canFireNow = canFire [myShip .id ];
6867 Ship nearestEnemy = enemyShips .get (0 );
68+ final boolean canFireNow = canFire [myShip .id ];
6969 for (final Ship enemyShip : enemyShips ) {
7070 if (myShip .calculateDistance (enemyShip ) < myShip .calculateDistance (nearestEnemy )) {
7171 nearestEnemy = enemyShip ;
7272 }
7373 }
74- Ship nearestAlly = myShips .get (0 );
75- for (final Ship ship : myShips ) {
76- if (myShip .calculateDistance (ship ) < myShip .calculateDistance (nearestAlly )) {
77- nearestAlly = ship ;
78- }
79- }
8074 final CannonBall incomingCannonBall = myShip .willHitOnTime (cannonBalls );
81- if (myShip .speed == 0 && nearestAlly .calculateDistance (myShip ) < 3 && nearestAlly .speed == 0 ) {
82- System .out .println (MOVE + random .nextInt (Ship .HEIGHT ) + " " + random .nextInt (Ship .WIDTH ));
83- } else if (incomingCannonBall != null ) {
75+ if (incomingCannonBall != null ) {
8476 System .err .println ("Gonna be hit with cannons!" + incomingCannonBall .targetX + " " + incomingCannonBall .targetY );
8577 if (myShip .speed > 0 ) {
8678 System .out .println (Math .random () > 0.5 ? STARBOARD : PORT );
@@ -102,27 +94,85 @@ public static void main(String args[]) {
10294 }
10395 } else if (myShip .calculateDistance (nearestEnemy ) <= 9 ) {
10496 final Coordinate shot = myShip .shoot (nearestEnemy );
105- if (shot != null && canFireNow && isNotFriendlyFire (shot , myShips , myShip )) {
97+ if (shot . x != - 1 && canFireNow && isNotFriendlyFire (shot , myShips , myShip )) {
10698 System .out .println (FIRE + shot .x + " " + shot .y );
10799 canFire [myShip .id ] = false ;
108100 } else {
109101 if (barrels .isEmpty ()) {
110- System .out .println (MOVE + opponentBestShip .x + " " + opponentBestShip .y );
102+ if (myShip .speed < 2 && myShip .isAligned (opponentBestShip ) && myShip .calculateDistance (opponentBestShip ) > 2 ) {
103+ System .err .println (opponentBestShip + " " + myShip .closestOrientation (opponentBestShip ));
104+ System .out .println (FASTER );
105+ } else {
106+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
107+ if (myShip .isAligned (opponentBestShip )) {
108+ System .out .println (STARBOARD );
109+ } else {
110+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
111+ if (myShip .isAligned (opponentBestShip )) {
112+ System .out .println (PORT );
113+ } else {
114+ System .out .println (MOVE + opponentBestShip .x + " " + opponentBestShip .y );
115+ }
116+ }
117+ }
111118 } else {
112119 final Barrel nearest = findTheNearestBarrel (barrels , myShip );
113120 if (nearestEnemy .rum <= myShip .rum - 25 ) {
114- System .out .println (MOVE + nearestEnemy .x + " " + nearestEnemy .y );
121+ if (myShip .speed < 2 && myShip .calculateDistance (nearest ) > 2 && myShip .isAligned (nearest )) {
122+ System .err .println (nearest + " " + myShip .closestOrientation (nearest ));
123+ System .out .println (FASTER );
124+ } else {
125+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
126+ if (myShip .isAligned (nearestEnemy )) {
127+ System .out .println (STARBOARD );
128+ } else {
129+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
130+ if (myShip .isAligned (nearestEnemy )) {
131+ System .out .println (PORT );
132+ } else {
133+ System .out .println (MOVE + nearestEnemy .x + " " + nearestEnemy .y );
134+ }
135+ }
136+ }
137+ } else if (myShip .speed < 2 && myShip .calculateDistance (nearest ) > 2 && myShip .isAligned (nearest )) {
138+ System .err .println (nearest + " " + myShip .closestOrientation (nearest ));
139+ System .out .println (FASTER );
115140 } else {
116- System .out .println (MOVE + nearest .x + " " + nearest .y );
141+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
142+ if (myShip .isAligned (nearest )) {
143+ System .out .println (STARBOARD );
144+ } else {
145+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
146+ if (myShip .isAligned (nearest )) {
147+ System .out .println (PORT );
148+ } else {
149+ System .out .println (MOVE + nearest .x + " " + nearest .y );
150+ }
151+ }
117152 }
118153 }
119154 }
120155 } else if (!barrels .isEmpty ()) {
121156 final Barrel nearest = findTheNearestBarrel (barrels , myShip );
122157 if (myShip .rum < 87 || myShip .calculateDistance (nearest ) > 5 ) {
123- System .out .println (MOVE + nearest .x + " " + nearest .y );
158+ if (myShip .speed < 2 && myShip .calculateDistance (nearest ) > 2 && myShip .isAligned (nearest )) {
159+ System .err .println (nearest + " " + myShip .closestOrientation (nearest ));
160+ System .out .println (FASTER );
161+ } else {
162+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
163+ if (myShip .isAligned (nearest )) {
164+ System .out .println (STARBOARD );
165+ } else {
166+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
167+ if (myShip .isAligned (nearest )) {
168+ System .out .println (PORT );
169+ } else {
170+ System .out .println (MOVE + nearest .x + " " + nearest .y );
171+ }
172+ }
173+ }
124174 } else {
125- if (myShip .speed == 0 && myShip .calculateDistance (nearest ) > 3 ) {
175+ if (myShip .speed == 0 && myShip .calculateDistance (nearest ) > 4 ) {
126176 System .out .println (MOVE + nearest .x + " " + nearest .y );
127177 } else {
128178 System .out .println (SLOWER );
@@ -142,21 +192,67 @@ public static void main(String args[]) {
142192 } else {
143193 final Coordinate run = evade (myShips , myBestShip , opponentBestShip , myShip , nearestEnemy );
144194 if (run .isOutSideMap ()) {
145- System .out .println (MOVE + random .nextInt (Ship . HEIGHT ) + " " + random .nextInt (Ship . WIDTH ));
195+ System .out .println (MOVE + random .nextInt (21 ) + " " + random .nextInt (21 ));
146196 } else {
147- System .out .println (MOVE + run .x + " " + run .y );
197+ if (myShip .speed < 2 && myShip .calculateDistance (nearestEnemy ) > 2 && myShip .isAligned (nearestEnemy )) {
198+ System .err .println (nearestEnemy + " " + myShip .closestOrientation (nearestEnemy ));
199+ System .out .println (FASTER );
200+ } else {
201+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
202+ if (myShip .isAligned (run )) {
203+ System .out .println (STARBOARD );
204+ } else {
205+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
206+ if (myShip .isAligned (run )) {
207+ System .out .println (PORT );
208+ } else {
209+ System .out .println (MOVE + run .x + " " + run .y );
210+ }
211+ }
212+ }
148213 }
149214 }
150215 } else if (myShip .calculateDistance (nearestEnemy ) <= 10 ) {
151216 final Coordinate shot = myShip .shoot (nearestEnemy );
152- if (shot != null && canFireNow ) {
217+ if (shot . x != - 1 && canFireNow ) {
153218 System .out .println (FIRE + shot .x + " " + shot .y );
154219 canFire [myShip .id ] = false ;
155220 } else {
156- System .out .println (MOVE + opponentBestShip .x + " " + opponentBestShip .y );
221+ if (myShip .speed < 2 && myShip .calculateDistance (opponentBestShip ) > 2 && myShip .isAligned (opponentBestShip )) {
222+ System .err .println (opponentBestShip + " " + myShip .closestOrientation (opponentBestShip ));
223+ System .out .println (FASTER );
224+ } else {
225+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
226+ if (myShip .isAligned (opponentBestShip )) {
227+ System .out .println (STARBOARD );
228+ } else {
229+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
230+ if (myShip .isAligned (opponentBestShip )) {
231+ System .out .println (PORT );
232+ } else {
233+ System .out .println (MOVE + opponentBestShip .x + " " + opponentBestShip .y );
234+ }
235+ }
236+ }
157237 }
158238 } else {
159- System .out .println (MOVE + opponentBestShip .x + " " + opponentBestShip .y );
239+ //pursue
240+ if (myShip .speed < 2 && myShip .calculateDistance (opponentBestShip ) > 2 && myShip .isAligned (opponentBestShip )) {
241+ System .err .println (opponentBestShip + " " + myShip .closestOrientation (opponentBestShip ));
242+ System .out .println (FASTER );
243+ } else {
244+ myShip .rotation = (myShip .rotation + 1 ) % 6 ;
245+ if (myShip .isAligned (opponentBestShip )) {
246+ System .out .println (STARBOARD );
247+ } else {
248+ myShip .rotation = (6 + myShip .rotation - 2 ) % 6 ;
249+ if (myShip .isAligned (opponentBestShip )) {
250+ System .out .println (PORT );
251+ } else {
252+ System .out .println (MOVE + opponentBestShip .x + " " + opponentBestShip .y );
253+ }
254+ }
255+ }
160256 }
161257 }
162258 if (!canFireNow ) {
@@ -168,7 +264,7 @@ public static void main(String args[]) {
168264 }
169265
170266 private static boolean isNotFriendlyFire (final Coordinate shot , final List <Ship > myShips , final Ship attackingShip ) {
171- final List <CannonBall > cannonBall = Collections .singletonList (new CannonBall (shot .x , shot .y , attackingShip .id , CannonBall .findTimeToReach (attackingShip , shot ), random .nextInt (1000 )));
267+ List <CannonBall > cannonBall = Collections .singletonList (new CannonBall (shot .x , shot .y , attackingShip .id , CannonBall .findTimeToReach (attackingShip , shot ), random .nextInt (1000 )));
172268 for (final Ship myShip : myShips ) {
173269 if (myShip .willHitOnTime (cannonBall ) != null ) {
174270 return false ;
@@ -294,7 +390,7 @@ public Barrel(final int rum, final int x, final int y, int id) {
294390
295391class Ship extends Entity {
296392 public static final int MAX_MINE_DISTANCE = 5 ;
297- public static final int MAX_CANNON_TIME = 4 ;
393+ public static final double MAX_CANNON_TIME = 4 ;
298394 int rotation ;
299395 int speed ;
300396 int rum ;
@@ -324,7 +420,7 @@ public Coordinate shoot(final Ship ship) {
324420 return prediction ;
325421 }
326422 }
327- return null ;
423+ return new Coordinate (- 1 , - 1 ) ;
328424 }
329425 }
330426
@@ -333,10 +429,14 @@ public Mine willCollide(final List<Mine> mines) {
333429 return null ;
334430 }
335431 Coordinate current = this ;
432+ int rumOnTheWay = 0 ;
336433 for (int i = 0 ; i <= MAX_MINE_DISTANCE ; i ++) {
434+ if (current instanceof Barrel ) {
435+ rumOnTheWay += ((Barrel ) current ).rum ;
436+ }
337437 for (final Mine mine : mines ) {
338438 if (isAHit (current , mine )) {
339- return mine ;
439+ return !( rumOnTheWay > 25 && i >= MAX_MINE_DISTANCE - 1 ) ? mine : null ;
340440 }
341441 }
342442 final int [] movement = current .x % 2 == 0 ? evenMovement [rotation ] : oddMovement [rotation ];
@@ -360,10 +460,14 @@ public CannonBall willHitOnTime(final List<CannonBall> cannonBalls) {
360460 return null ;
361461 }
362462 Coordinate current = this ;
463+ int rumOnTheWay = 0 ;
363464 for (int i = 0 ; i <= MAX_CANNON_TIME ; i ++) {
465+ if (current instanceof Barrel ) {
466+ rumOnTheWay += ((Barrel ) current ).rum ;
467+ }
364468 for (final CannonBall cannonBall : cannonBalls ) {
365469 if (cannonBall .turnsToHitTarget == i + 1 && isAHit (current , new Coordinate (cannonBall .targetX , cannonBall .targetY ))) {
366- return cannonBall ;
470+ return !( rumOnTheWay > 25 && i >= MAX_CANNON_TIME - 1 ) ? cannonBall : null ;
367471 }
368472 }
369473 final int [] movement = current .x % 2 == 0 ? evenMovement [rotation ] : oddMovement [rotation ];
@@ -374,11 +478,30 @@ public CannonBall willHitOnTime(final List<CannonBall> cannonBalls) {
374478 }
375479 return null ;
376480 }
481+
482+ public int closestOrientation (final Coordinate coordinate ) {
483+ final int angle = (int ) (Math .round ((360 + Math .toDegrees (Math .atan2 ((coordinate .y - y ), coordinate .x - x ))) / 60.0 ) % 6 );
484+ System .err .println (coordinate .x + " " + coordinate .y + " " + x + " " + y + " " + angle );
485+ return angle % 6 ;
486+ }
487+
488+ public boolean isAligned (final Coordinate coordinate ) {
489+ return closestOrientation (coordinate ) == rotation ;
490+ }
491+
492+ public static void main (String args []) {
493+ Ship ship = new Ship (0 , 1 , 1 , 1 , 10 , 10 , 1 );
494+ for (int i = 0 ; i < 20 ; i ++) {
495+ for (int j = 0 ; j < 20 ; j ++) {
496+ ship .closestOrientation (new Coordinate (i , j ));
497+ }
498+ }
499+ }
377500}
378501
379502class Mine extends Entity {
380503
381504 public Mine (final int x , final int y , final int id ) {
382505 super (id , x , y );
383506 }
384- }
507+ }
0 commit comments