From 871f5fb860d9e1be92961956fd3394fdd6d8bed5 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 12:35:21 +0100 Subject: [PATCH] United AI with GameLogic --- .../gui/{Logic.java => GameLogic.java} | 17 +++++++++++++---- .../java/de/tims/fleetstorm/gui/LogicTest.java | 6 +++--- 2 files changed, 16 insertions(+), 7 deletions(-) rename src/main/java/de/tims/fleetstorm/gui/{Logic.java => GameLogic.java} (92%) diff --git a/src/main/java/de/tims/fleetstorm/gui/Logic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java similarity index 92% rename from src/main/java/de/tims/fleetstorm/gui/Logic.java rename to src/main/java/de/tims/fleetstorm/gui/GameLogic.java index a014d0b..d00b7a9 100644 --- a/src/main/java/de/tims/fleetstorm/gui/Logic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -12,15 +12,18 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.MatteBorder; +import de.tims.fleetstorm.ai.Logic; import de.tims.fleetstorm.matchfield.Matchfield; -public class Logic extends JPanel { +public class GameLogic extends JPanel { // GameManager stuff private int gameState; private Matchfield matchfield; + private Matchfield enemyMatchfield; private int matchfieldSize = 10; private boolean playerMove = true; + private Logic aiLogic; public static final int PREPARATION = 1; public static final int RUNNING = 2; @@ -39,7 +42,7 @@ public class Logic extends JPanel { private JLabel enemy4Ship; private JLabel enemy5Ship; - public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { + public GameLogic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { this.fields = new ArrayList<>(); setSize(640, 480); @@ -248,7 +251,7 @@ public class Logic extends JPanel { */ public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - Logic gui = new Logic(15, 28, 1); + GameLogic gui = new GameLogic(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); @@ -256,9 +259,15 @@ public class Logic extends JPanel { } public void start() { - this.gameState = Logic.PREPARATION; + this.gameState = GameLogic.PREPARATION; + // create player matchfield this.matchfield = new Matchfield(matchfieldSize); + + // create enemy matchfield and initialize AI (with player's matchfield) + this.enemyMatchfield = new Matchfield(matchfieldSize); + this.aiLogic = new Logic(); + this.aiLogic.setMatchfield(this.matchfield); } public void nextMove() { diff --git a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 5d5e47c..ee73c74 100644 --- a/src/test/java/de/tims/fleetstorm/gui/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -7,11 +7,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import de.tims.fleetstorm.gui.Logic; +import de.tims.fleetstorm.gui.GameLogic; class LogicTest { - Logic guiLogic = new Logic(0, 0, 0); + GameLogic guiLogic = new GameLogic(0, 0, 0); @BeforeEach void setup() { @@ -20,7 +20,7 @@ class LogicTest { @Test void testIfGameStateIsPreparationAfterStart() { - int expectedState = Logic.PREPARATION; + int expectedState = GameLogic.PREPARATION; int calculatedState = guiLogic.getGameState();