From a976a43742a5308ad27cacb3ab5f7e70ad811bf3 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 11:41:14 +0100 Subject: [PATCH 1/7] Added GameManager and check if GameState is correct after start function call --- .../java/de/tims/fleetstorm/GameManager.java | 19 +++ .../de/tims/fleetstorm/GameManagerTest.java | 21 +++ .../matchfield/MatchfieldCreationTest.java | 132 +++++++++--------- 3 files changed, 106 insertions(+), 66 deletions(-) create mode 100644 src/main/java/de/tims/fleetstorm/GameManager.java create mode 100644 src/test/java/de/tims/fleetstorm/GameManagerTest.java diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java new file mode 100644 index 0000000..8f682cc --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/GameManager.java @@ -0,0 +1,19 @@ +package de.tims.fleetstorm; + +public class GameManager { + + private int gameState; + + 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; + } + + public int getGameState() { + return gameState; + } + +} diff --git a/src/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/GameManagerTest.java new file mode 100644 index 0000000..d90e85f --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/GameManagerTest.java @@ -0,0 +1,21 @@ +package de.tims.fleetstorm; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class GameManagerTest { + + GameManager gameManager = new GameManager(); + + @Test + void testIfGameStateIsPreparationAfterStart() { + gameManager.start(); + int expectedState = GameManager.PREPARATION; + + int calculatedState = gameManager.getGameState(); + + assertEquals(expectedState, calculatedState); + } + +} diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 168dade..8eacdae 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -1,66 +1,66 @@ -package de.tims.fleetstorm.matchfield; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertNotNull; - -import java.util.stream.Stream; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -class MatchfieldCreationTest { - - @Test - void testMatchfieldCreateNotEmpty() { - Matchfield matchfield = new Matchfield(0); - int[][] calcResult = matchfield.createMatchfield(); - assertNotNull(calcResult); - } - - @ParameterizedTest(name = "matchfield creation has correct size") - @MethodSource("testMatchfieldSize") - void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) { - Matchfield matchfield = new Matchfield(size); - matchfield.createMatchfield(); - - int calcResult = matchfield.getSize(); - assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); - } - - static Stream testMatchfieldSize() { - return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225)); - } - - @ParameterizedTest(name = "matchfield get field is correct") - @MethodSource("testMatchfieldGetFieldState") - void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) { - Matchfield matchfield = new Matchfield(10); - matchfield.createMatchfield(); - - int calcResult = matchfield.getState(x, y); - assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); - } - - static Stream testMatchfieldGetFieldState() { - return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY)); - } - - @ParameterizedTest(name = "matchfield change field is correct") - @MethodSource("testMatchfieldSetStateIsCorrect") - void testMatchfieldGetCorrectState(String testName, int x, int y, int state, int expectedResult) { - Matchfield matchfield = new Matchfield(10); - matchfield.createMatchfield(); - - matchfield.setState(x, y, state); - - int calcResult = matchfield.getState(x, y); - assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); - } - - static Stream testMatchfieldSetStateIsCorrect() { - return Stream.of( - Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP)); - } -} +package de.tims.fleetstorm.matchfield; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertNotNull; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class MatchfieldCreationTest { + + @Test + void testMatchfieldCreateNotEmpty() { + Matchfield matchfield = new Matchfield(0); + int[][] calcResult = matchfield.createMatchfield(); + assertNotNull(calcResult); + } + + @ParameterizedTest(name = "matchfield creation has correct size") + @MethodSource("testMatchfieldSize") + void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) { + Matchfield matchfield = new Matchfield(size); + matchfield.createMatchfield(); + + int calcResult = matchfield.getSize(); + assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream testMatchfieldSize() { + return Stream.of(Arguments.of("field size 10x10", 10, 100), Arguments.of("field size 15x15", 15, 225)); + } + + @ParameterizedTest(name = "matchfield get field is correct") + @MethodSource("testMatchfieldGetFieldState") + void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + int calcResult = matchfield.getState(x, y); + assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream testMatchfieldGetFieldState() { + return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY)); + } + + @ParameterizedTest(name = "matchfield change field is correct") + @MethodSource("testMatchfieldSetStateIsCorrect") + void testMatchfieldGetCorrectState(String testName, int x, int y, int state, int expectedResult) { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + matchfield.setState(x, y, state); + + int calcResult = matchfield.getState(x, y); + assertThat(calcResult).describedAs(testName).isEqualTo(expectedResult); + } + + static Stream testMatchfieldSetStateIsCorrect() { + return Stream.of( + Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP)); + } +} From 7f53ab77af189a816b9c51da24f2fa8e209faa7a Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 11:55:09 +0100 Subject: [PATCH 2/7] Added nextMove() function to check wheter the player or AI has move --- .../java/de/tims/fleetstorm/GameManager.java | 15 ++++++++++++ .../de/tims/fleetstorm/GameManagerTest.java | 24 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/GameManager.java b/src/main/java/de/tims/fleetstorm/GameManager.java index 8f682cc..9a8a59d 100644 --- a/src/main/java/de/tims/fleetstorm/GameManager.java +++ b/src/main/java/de/tims/fleetstorm/GameManager.java @@ -1,8 +1,13 @@ 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; @@ -10,10 +15,20 @@ public class GameManager { 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/test/java/de/tims/fleetstorm/GameManagerTest.java b/src/test/java/de/tims/fleetstorm/GameManagerTest.java index d90e85f..5c329c7 100644 --- a/src/test/java/de/tims/fleetstorm/GameManagerTest.java +++ b/src/test/java/de/tims/fleetstorm/GameManagerTest.java @@ -1,16 +1,23 @@ package de.tims.fleetstorm; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class GameManagerTest { GameManager gameManager = new GameManager(); + @BeforeEach + void setup() { + gameManager.start(); + } + @Test void testIfGameStateIsPreparationAfterStart() { - gameManager.start(); int expectedState = GameManager.PREPARATION; int calculatedState = gameManager.getGameState(); @@ -18,4 +25,19 @@ class GameManagerTest { assertEquals(expectedState, calculatedState); } + @Test + void testNextMoveIsNotPlayer() { + + // 20 tries => every even move is a player move + for (int i = 0; i < 20; i++) { + gameManager.nextMove(); + boolean calculatedResult = gameManager.isPlayerMove(); + if (i % 2 == 0) { + assertFalse(calculatedResult); + } else { + assertTrue(calculatedResult); + } + } + } + } From 1ca75e2abd1b0b2ae91d76a5d13fb0a42d81645e Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 12 Jan 2022 13:18:57 +0100 Subject: [PATCH 3/7] Created gui with minimal elements --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/de/tims/fleetstorm/gui/GUI.java diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java new file mode 100644 index 0000000..70d3333 --- /dev/null +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -0,0 +1,63 @@ +package de.tims.fleetstorm.gui; + +import java.awt.Color; +import java.awt.Font; +import java.util.ArrayList; + +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.MatteBorder; + +public class GUI extends JPanel { + + private ArrayList fields; + + public GUI() { + this.fields = new ArrayList<>(); + setSize(640, 480); + + JPanel fieldWrapper = new JPanel(); + fieldWrapper.setBounds(10, 11, 362, 458); + fieldWrapper.setLayout(null); + + JPanel infoWrapper = new JPanel(); + infoWrapper.setBounds(382, 11, 248, 458); + + JLabel hasMoveLabel = new JLabel("Am Zug:"); + hasMoveLabel.setFont(new Font("Tahoma", Font.BOLD, 11)); + infoWrapper.add(hasMoveLabel); + + JLabel hasMove = new JLabel("Spieler"); + infoWrapper.add(hasMove); + setLayout(null); + add(fieldWrapper); + add(infoWrapper); + + int gap = 30; + int width = 28; + + for (int x = 0; x < 10; x++) { + for (int y = 0; y < 10; y++) { + JPanel field = new JPanel(); + field.setBounds(gap + x * width, gap + y * width, width, width); + field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); + fields.add(field); + fieldWrapper.add(field); + } + } + + } + + /** + * This function is only for testing + */ + public static void main(String[] args) { + JFrame frame = new JFrame("Test GUI"); + GUI gui = new GUI(); + frame.setContentPane(gui); + frame.setSize(640, 480); + frame.setResizable(true); + frame.setVisible(true); + } +} From def0f3a1b9ba9a4ab97593a5a1d48dd007e7833f Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 11:43:58 +0100 Subject: [PATCH 4/7] Added game info sidebar --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 129 +++++++++++++++++- 1 file changed, 122 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java index 70d3333..10f7d8a 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -2,6 +2,7 @@ package de.tims.fleetstorm.gui; import java.awt.Color; import java.awt.Font; +import java.awt.GridLayout; import java.util.ArrayList; import javax.swing.JFrame; @@ -23,17 +24,131 @@ public class GUI extends JPanel { JPanel infoWrapper = new JPanel(); infoWrapper.setBounds(382, 11, 248, 458); - - JLabel hasMoveLabel = new JLabel("Am Zug:"); - hasMoveLabel.setFont(new Font("Tahoma", Font.BOLD, 11)); - infoWrapper.add(hasMoveLabel); - - JLabel hasMove = new JLabel("Spieler"); - infoWrapper.add(hasMove); + infoWrapper.setLayout(null); setLayout(null); add(fieldWrapper); add(infoWrapper); + JPanel yourFleet = new JPanel(); + yourFleet.setBounds(10, 110, 228, 93); + infoWrapper.add(yourFleet); + yourFleet.setLayout(null); + + JPanel panel_1 = new JPanel(); + panel_1.setBounds(0, 0, 228, 27); + yourFleet.add(panel_1); + panel_1.setLayout(null); + + JLabel ownFleetHeading = new JLabel("Deine Flotte:"); + ownFleetHeading.setBounds(0, 0, 228, 17); + panel_1.add(ownFleetHeading); + ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + + JPanel panel_2 = new JPanel(); + panel_2.setBounds(0, 26, 228, 67); + yourFleet.add(panel_2); + panel_2.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel own2ShipLabel = new JLabel("1x2 Schiff"); + panel_2.add(own2ShipLabel); + + JLabel own2Ship = new JLabel("OK"); + panel_2.add(own2Ship); + + JLabel own3ShipLabel = new JLabel("1x3 Schiff"); + panel_2.add(own3ShipLabel); + + JLabel own3Ship = new JLabel("OK"); + panel_2.add(own3Ship); + + JLabel own4ShipLabel = new JLabel("1x4 Schiff"); + panel_2.add(own4ShipLabel); + + JLabel own4Ship = new JLabel("OK"); + panel_2.add(own4Ship); + + JLabel own5ShipLabel = new JLabel("1x5 Schiff"); + panel_2.add(own5ShipLabel); + + JLabel own5Ship = new JLabel("OK"); + panel_2.add(own5Ship); + + JPanel enemyFleet = new JPanel(); + enemyFleet.setLayout(null); + enemyFleet.setBounds(10, 237, 228, 93); + infoWrapper.add(enemyFleet); + + JPanel panel_1_1 = new JPanel(); + panel_1_1.setLayout(null); + panel_1_1.setBounds(0, 0, 228, 27); + enemyFleet.add(panel_1_1); + + JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); + enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + enemyFleetHeading.setBounds(0, 0, 228, 17); + panel_1_1.add(enemyFleetHeading); + + JPanel panel_2_1 = new JPanel(); + panel_2_1.setBounds(0, 26, 228, 67); + enemyFleet.add(panel_2_1); + panel_2_1.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); + panel_2_1.add(enemy2ShipLabel); + + JLabel enemy2Ship = new JLabel("OK"); + panel_2_1.add(enemy2Ship); + + JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); + panel_2_1.add(enemy3ShipLabel); + + JLabel enemy3Ship = new JLabel("OK"); + panel_2_1.add(enemy3Ship); + + JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); + panel_2_1.add(enemy4ShipLabel); + + JLabel enemy4Ship = new JLabel("OK"); + panel_2_1.add(enemy4Ship); + + JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); + panel_2_1.add(enemy5ShipLabel); + + JLabel enemy5Ship = new JLabel("OK"); + panel_2_1.add(enemy5Ship); + + JPanel infos = new JPanel(); + infos.setLayout(null); + infos.setBounds(10, 11, 228, 68); + infoWrapper.add(infos); + + JPanel panel_1_2 = new JPanel(); + panel_1_2.setLayout(null); + panel_1_2.setBounds(0, 0, 228, 27); + infos.add(panel_1_2); + + JLabel gameInfoHeading = new JLabel("Spielinfos"); + gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); + gameInfoHeading.setBounds(0, 0, 228, 17); + panel_1_2.add(gameInfoHeading); + + JPanel panel_2_2 = new JPanel(); + panel_2_2.setBounds(0, 26, 228, 31); + infos.add(panel_2_2); + panel_2_2.setLayout(new GridLayout(0, 2, 0, 3)); + + JLabel matchfieldLabel = new JLabel("Spielfeld"); + panel_2_2.add(matchfieldLabel); + + JLabel matchfieldValue = new JLabel("Eigenes"); + panel_2_2.add(matchfieldValue); + + JLabel moveLabel = new JLabel("Am Zug"); + panel_2_2.add(moveLabel); + + JLabel moveValue = new JLabel("Du"); + panel_2_2.add(moveValue); + int gap = 30; int width = 28; From 5398f948fb81541bd0ca85d7c932c762c349438e Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 11:55:05 +0100 Subject: [PATCH 5/7] Refactored GUI and generated Getters --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 183 ++++++++++++------ 1 file changed, 119 insertions(+), 64 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java index 10f7d8a..5bd0a99 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -13,8 +13,18 @@ import javax.swing.border.MatteBorder; public class GUI extends JPanel { private ArrayList fields; - - public GUI() { + private JLabel matchfieldValue; + private JLabel moveValue; + private JLabel own2Ship; + private JLabel own3Ship; + private JLabel own4Ship; + private JLabel own5Ship; + private JLabel enemy2Ship; + private JLabel enemy3Ship; + private JLabel enemy4Ship; + private JLabel enemy5Ship; + + public GUI(int gapToFrameBorder, int fieldWidth, int spaceBetween) { this.fields = new ArrayList<>(); setSize(640, 480); @@ -34,128 +44,129 @@ public class GUI extends JPanel { infoWrapper.add(yourFleet); yourFleet.setLayout(null); - JPanel panel_1 = new JPanel(); - panel_1.setBounds(0, 0, 228, 27); - yourFleet.add(panel_1); - panel_1.setLayout(null); + JPanel ownFleetHeadingPanel = new JPanel(); + ownFleetHeadingPanel.setBounds(0, 0, 228, 27); + yourFleet.add(ownFleetHeadingPanel); + ownFleetHeadingPanel.setLayout(null); JLabel ownFleetHeading = new JLabel("Deine Flotte:"); ownFleetHeading.setBounds(0, 0, 228, 17); - panel_1.add(ownFleetHeading); + ownFleetHeadingPanel.add(ownFleetHeading); ownFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); - JPanel panel_2 = new JPanel(); - panel_2.setBounds(0, 26, 228, 67); - yourFleet.add(panel_2); - panel_2.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel ownFleetContentPanel = new JPanel(); + ownFleetContentPanel.setBounds(0, 26, 228, 67); + yourFleet.add(ownFleetContentPanel); + ownFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); JLabel own2ShipLabel = new JLabel("1x2 Schiff"); - panel_2.add(own2ShipLabel); + ownFleetContentPanel.add(own2ShipLabel); - JLabel own2Ship = new JLabel("OK"); - panel_2.add(own2Ship); + own2Ship = new JLabel("OK"); + ownFleetContentPanel.add(own2Ship); JLabel own3ShipLabel = new JLabel("1x3 Schiff"); - panel_2.add(own3ShipLabel); + ownFleetContentPanel.add(own3ShipLabel); - JLabel own3Ship = new JLabel("OK"); - panel_2.add(own3Ship); + own3Ship = new JLabel("OK"); + ownFleetContentPanel.add(own3Ship); JLabel own4ShipLabel = new JLabel("1x4 Schiff"); - panel_2.add(own4ShipLabel); + ownFleetContentPanel.add(own4ShipLabel); - JLabel own4Ship = new JLabel("OK"); - panel_2.add(own4Ship); + own4Ship = new JLabel("OK"); + ownFleetContentPanel.add(own4Ship); JLabel own5ShipLabel = new JLabel("1x5 Schiff"); - panel_2.add(own5ShipLabel); + ownFleetContentPanel.add(own5ShipLabel); - JLabel own5Ship = new JLabel("OK"); - panel_2.add(own5Ship); + own5Ship = new JLabel("OK"); + ownFleetContentPanel.add(own5Ship); JPanel enemyFleet = new JPanel(); enemyFleet.setLayout(null); enemyFleet.setBounds(10, 237, 228, 93); infoWrapper.add(enemyFleet); - JPanel panel_1_1 = new JPanel(); - panel_1_1.setLayout(null); - panel_1_1.setBounds(0, 0, 228, 27); - enemyFleet.add(panel_1_1); + JPanel enemyFleetHeadingPanel = new JPanel(); + enemyFleetHeadingPanel.setLayout(null); + enemyFleetHeadingPanel.setBounds(0, 0, 228, 27); + enemyFleet.add(enemyFleetHeadingPanel); JLabel enemyFleetHeading = new JLabel("Gegnerische Flotte:"); enemyFleetHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); enemyFleetHeading.setBounds(0, 0, 228, 17); - panel_1_1.add(enemyFleetHeading); + enemyFleetHeadingPanel.add(enemyFleetHeading); - JPanel panel_2_1 = new JPanel(); - panel_2_1.setBounds(0, 26, 228, 67); - enemyFleet.add(panel_2_1); - panel_2_1.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel enemyFleetContentPanel = new JPanel(); + enemyFleetContentPanel.setBounds(0, 26, 228, 67); + enemyFleet.add(enemyFleetContentPanel); + enemyFleetContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); JLabel enemy2ShipLabel = new JLabel("1x2 Schiff"); - panel_2_1.add(enemy2ShipLabel); + enemyFleetContentPanel.add(enemy2ShipLabel); - JLabel enemy2Ship = new JLabel("OK"); - panel_2_1.add(enemy2Ship); + enemy2Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy2Ship); JLabel enemy3ShipLabel = new JLabel("1x3 Schiff"); - panel_2_1.add(enemy3ShipLabel); + enemyFleetContentPanel.add(enemy3ShipLabel); - JLabel enemy3Ship = new JLabel("OK"); - panel_2_1.add(enemy3Ship); + enemy3Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy3Ship); JLabel enemy4ShipLabel = new JLabel("1x4 Schiff"); - panel_2_1.add(enemy4ShipLabel); + enemyFleetContentPanel.add(enemy4ShipLabel); - JLabel enemy4Ship = new JLabel("OK"); - panel_2_1.add(enemy4Ship); + enemy4Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy4Ship); JLabel enemy5ShipLabel = new JLabel("1x5 Schiff"); - panel_2_1.add(enemy5ShipLabel); + enemyFleetContentPanel.add(enemy5ShipLabel); - JLabel enemy5Ship = new JLabel("OK"); - panel_2_1.add(enemy5Ship); + enemy5Ship = new JLabel("OK"); + enemyFleetContentPanel.add(enemy5Ship); JPanel infos = new JPanel(); infos.setLayout(null); infos.setBounds(10, 11, 228, 68); infoWrapper.add(infos); - JPanel panel_1_2 = new JPanel(); - panel_1_2.setLayout(null); - panel_1_2.setBounds(0, 0, 228, 27); - infos.add(panel_1_2); + JPanel gameInfoHeadingPanel = new JPanel(); + gameInfoHeadingPanel.setLayout(null); + gameInfoHeadingPanel.setBounds(0, 0, 228, 27); + infos.add(gameInfoHeadingPanel); JLabel gameInfoHeading = new JLabel("Spielinfos"); gameInfoHeading.setFont(new Font("Tahoma", Font.BOLD, 14)); gameInfoHeading.setBounds(0, 0, 228, 17); - panel_1_2.add(gameInfoHeading); + gameInfoHeadingPanel.add(gameInfoHeading); - JPanel panel_2_2 = new JPanel(); - panel_2_2.setBounds(0, 26, 228, 31); - infos.add(panel_2_2); - panel_2_2.setLayout(new GridLayout(0, 2, 0, 3)); + JPanel gameInfoContentPanel = new JPanel(); + gameInfoContentPanel.setBounds(0, 26, 228, 31); + infos.add(gameInfoContentPanel); + gameInfoContentPanel.setLayout(new GridLayout(0, 2, 0, 3)); JLabel matchfieldLabel = new JLabel("Spielfeld"); - panel_2_2.add(matchfieldLabel); + gameInfoContentPanel.add(matchfieldLabel); - JLabel matchfieldValue = new JLabel("Eigenes"); - panel_2_2.add(matchfieldValue); + matchfieldValue = new JLabel("Eigenes"); + gameInfoContentPanel.add(matchfieldValue); JLabel moveLabel = new JLabel("Am Zug"); - panel_2_2.add(moveLabel); - - JLabel moveValue = new JLabel("Du"); - panel_2_2.add(moveValue); + gameInfoContentPanel.add(moveLabel); - int gap = 30; - int width = 28; + moveValue = new JLabel("Du"); + gameInfoContentPanel.add(moveValue); for (int x = 0; x < 10; x++) { for (int y = 0; y < 10; y++) { JPanel field = new JPanel(); - field.setBounds(gap + x * width, gap + y * width, width, width); + + int xPos = gapToFrameBorder + x * fieldWidth + (x * spaceBetween); + int yPos = gapToFrameBorder + y * fieldWidth + (y * spaceBetween); + + field.setBounds(xPos, yPos, fieldWidth, fieldWidth); field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); fields.add(field); fieldWrapper.add(field); @@ -164,12 +175,56 @@ public class GUI extends JPanel { } + public ArrayList getFields() { + return fields; + } + + public JLabel getMatchfieldValue() { + return matchfieldValue; + } + + public JLabel getMoveValue() { + return moveValue; + } + + public JLabel getOwn2Ship() { + return own2Ship; + } + + public JLabel getOwn3Ship() { + return own3Ship; + } + + public JLabel getOwn4Ship() { + return own4Ship; + } + + public JLabel getOwn5Ship() { + return own5Ship; + } + + public JLabel getEnemy2Ship() { + return enemy2Ship; + } + + public JLabel getEnemy3Ship() { + return enemy3Ship; + } + + public JLabel getEnemy4Ship() { + return enemy4Ship; + } + + public JLabel getEnemy5Ship() { + return enemy5Ship; + } + /** * This function is only for testing */ public static void main(String[] args) { JFrame frame = new JFrame("Test GUI"); - GUI gui = new GUI(); + GUI gui = new GUI(15, 28, 1); frame.setContentPane(gui); frame.setSize(640, 480); frame.setResizable(true); From 2acebc2cad78c4ec8570c111464b821adeb0d613 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 12:05:01 +0100 Subject: [PATCH 6/7] Added mouse listener to register click on matchfield --- src/main/java/de/tims/fleetstorm/gui/GUI.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/gui/GUI.java b/src/main/java/de/tims/fleetstorm/gui/GUI.java index 5bd0a99..0a4083c 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GUI.java +++ b/src/main/java/de/tims/fleetstorm/gui/GUI.java @@ -3,6 +3,8 @@ package de.tims.fleetstorm.gui; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.util.ArrayList; import javax.swing.JFrame; @@ -170,6 +172,15 @@ public class GUI extends JPanel { field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0))); fields.add(field); fieldWrapper.add(field); + + field.addMouseListener(new MouseAdapter() { + + public void mouseClicked(MouseEvent e) { + field.setBackground(Color.RED); + field.revalidate(); + field.repaint(); + } + }); } } From 4b71ac31e4bee39164114599943bfc773f5fb52a Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Fri, 14 Jan 2022 12:13:29 +0100 Subject: [PATCH 7/7] 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 {