From c1cbf6a07774e9bf7e2a242eb4ecc62594dce617 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 12:13:29 +0100 Subject: [PATCH] United GameManager with GUI class (Refactoring) --- .../java/de/tims/fleetstorm/GameManager.java | 34 ----------------- .../fleetstorm/gui/{GUI.java => Logic.java} | 37 +++++++++++++++++-- .../LogicTest.java} | 18 +++++---- 3 files changed, 44 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/de/tims/fleetstorm/GameManager.java rename src/main/java/de/tims/fleetstorm/gui/{GUI.java => Logic.java} (89%) rename src/test/java/de/tims/fleetstorm/{GameManagerTest.java => gui/LogicTest.java} (66%) diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java deleted file mode 100644 index 9a8a59d..0000000 --- a/src/main/java/de/tims/fleetstorm/GameManager.java +++ /dev/null @@ -1,34 +0,0 @@ -package de.tims.fleetstorm; - -import de.tims.fleetstorm.matchfield.Matchfield; - -public class GameManager { - - private int gameState; - private Matchfield matchfield; - private int matchfieldSize = 10; - private boolean playerMove = true; - - public static final int PREPARATION = 1; - public static final int RUNNING = 2; - public static final int OVER = 3; - - public void start() { - this.gameState = GameManager.PREPARATION; - - this.matchfield = new Matchfield(matchfieldSize); - } - - public void nextMove() { - this.playerMove = !this.playerMove; - } - - public int getGameState() { - return gameState; - } - - public boolean isPlayerMove() { - return playerMove; - } - -} diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/Logic.java similarity index 89% rename from src/main/java/de/tims/fleetstorm/gui/GUI.java rename to src/main/java/de/tims/fleetstorm/gui/Logic.java index 0a4083c..a014d0b 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/Logic.java @@ -12,8 +12,21 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.MatteBorder; -public class GUI extends JPanel { +import de.tims.fleetstorm.matchfield.Matchfield; +public class Logic extends JPanel { + + // GameManager stuff + private int gameState; + private Matchfield matchfield; + private int matchfieldSize = 10; + private boolean playerMove = true; + + public static final int PREPARATION = 1; + public static final int RUNNING = 2; + public static final int OVER = 3; + + // GUI stuff private ArrayList fields; private JLabel matchfieldValue; private JLabel moveValue; @@ -26,7 +39,7 @@ public class GUI extends JPanel { private JLabel enemy4Ship; private JLabel enemy5Ship; - public GUI(int gapToFrameBorder, int fieldWidth, int spaceBetween) { + public Logic(int gapToFrameBorder, int fieldWidth, int spaceBetween) { this.fields = new ArrayList<>(); setSize(640, 480); @@ -235,10 +248,28 @@ public class GUI extends JPanel { */ public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - GUI gui = new GUI(15, 28, 1); + Logic gui = new Logic(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); frame.setVisible(true); } + + public void start() { + this.gameState = Logic.PREPARATION; + + this.matchfield = new Matchfield(matchfieldSize); + } + + public void nextMove() { + this.playerMove = !this.playerMove; + } + + public int getGameState() { + return gameState; + } + + public boolean isPlayerMove() { + return playerMove; + } } diff --git a/src/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java similarity index 66% rename from src/test/java/de/tims/fleetstorm/GameManagerTest.java rename to src/test/java/de/tims/fleetstorm/gui/LogicTest.java index 5c329c7..5d5e47c 100644 --- a/src/test/java/de/tims/fleetstorm/GameManagerTest.java +++ b/src/test/java/de/tims/fleetstorm/gui/LogicTest.java @@ -1,4 +1,4 @@ -package de.tims.fleetstorm; +package de.tims.fleetstorm.gui; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -7,20 +7,22 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -class GameManagerTest { +import de.tims.fleetstorm.gui.Logic; - GameManager gameManager = new GameManager(); +class LogicTest { + + Logic guiLogic = new Logic(0, 0, 0); @BeforeEach void setup() { - gameManager.start(); + guiLogic.start(); } @Test void testIfGameStateIsPreparationAfterStart() { - int expectedState = GameManager.PREPARATION; + int expectedState = Logic.PREPARATION; - int calculatedState = gameManager.getGameState(); + int calculatedState = guiLogic.getGameState(); assertEquals(expectedState, calculatedState); } @@ -30,8 +32,8 @@ class GameManagerTest { // 20 tries => every even move is a player move for (int i = 0; i < 20; i++) { - gameManager.nextMove(); - boolean calculatedResult = gameManager.isPlayerMove(); + guiLogic.nextMove(); + boolean calculatedResult = guiLogic.isPlayerMove(); if (i % 2 == 0) { assertFalse(calculatedResult); } else {