From 67aa58c6742f9ff040c72bb70c80991bf6fdb13f Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 14:11:57 +0100 Subject: [PATCH] Fixed that AI has next move when ship was hit --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 6 ++++-- src/main/java/de/tims/fleetstorm/gui/GameLogic.java | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index a20c9e0..5c4be84 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -93,8 +93,10 @@ public class Logic { } public void isShipOnYAxis() { - if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot).getState() == Coordinate.HIT) - || (matchfield.getBelow(lastShot).getState() == Coordinate.HIT))) { + if ((clearedAbove && clearedBelow) && ((matchfield.getAbove(lastShot) == null + || (matchfield.getAbove(lastShot).getState() == Coordinate.HIT)) + || (matchfield.getBelow(lastShot) == null + || (matchfield.getBelow(lastShot).getState() == Coordinate.HIT)))) { clearedLeft = true; clearedRight = true; sinkShip(); diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 414fd36..0bf44d3 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -216,12 +216,16 @@ public class GameLogic extends JPanel { // waiting for mouse click event.... } else { - Coordinate aiChosenField = this.aiLogic.chooseField(); + Coordinate aiChosenField = null; + while (aiChosenField == null) { + aiChosenField = this.aiLogic.chooseField(); + } + aiChosenField.print(); aiChosenField.shoot(); gui.updateFields(); - boolean aiHasNextMove = aiChosenField.getState() == Coordinate.HIT; + boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT; this.nextMove(aiHasNextMove); }