From f6d4e8a182a9b2b3585cf460a922c4c024e88896 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 16 Dec 2021 15:42:15 +0100 Subject: [PATCH] matchfield: refactored Matchfield class structure --- .../{MatchfieldCreation.java => Matchfield.java} | 13 ++++++++----- .../matchfield/MatchfieldCreationTest.java | 13 +++++++------ 2 files changed, 15 insertions(+), 11 deletions(-) rename src/main/java/de/tims/fleetstorm/matchfield/{MatchfieldCreation.java => Matchfield.java} (72%) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java similarity index 72% rename from src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java rename to src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 2f604a6..45912eb 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/MatchfieldCreation.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -1,20 +1,23 @@ package de.tims.fleetstorm.matchfield; -public class MatchfieldCreation { +public class Matchfield { private int[][] matchfield; private int size; - public MatchfieldCreation(int size) { + public static final int EMPTY = 0; + public static final int SHIP = 1; + public static final int SHOT = 2; + + public Matchfield(int size) { this.size = size; + this.matchfield = new int[this.size][this.size]; } public int[][] createMatchfield() { - this.matchfield = new int[this.size][this.size]; - for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - this.matchfield[i][j] = 1; + this.matchfield[i][j] = Matchfield.EMPTY; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index ee790cb..168dade 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -14,7 +14,7 @@ class MatchfieldCreationTest { @Test void testMatchfieldCreateNotEmpty() { - MatchfieldCreation matchfield = new MatchfieldCreation(0); + Matchfield matchfield = new Matchfield(0); int[][] calcResult = matchfield.createMatchfield(); assertNotNull(calcResult); } @@ -22,7 +22,7 @@ class MatchfieldCreationTest { @ParameterizedTest(name = "matchfield creation has correct size") @MethodSource("testMatchfieldSize") void testMatchfieldCreationHasCorrectSize(String testName, int size, int expectedResult) { - MatchfieldCreation matchfield = new MatchfieldCreation(size); + Matchfield matchfield = new Matchfield(size); matchfield.createMatchfield(); int calcResult = matchfield.getSize(); @@ -36,7 +36,7 @@ class MatchfieldCreationTest { @ParameterizedTest(name = "matchfield get field is correct") @MethodSource("testMatchfieldGetFieldState") void testMatchfieldGetCorrectState(String testName, int x, int y, int expectedResult) { - MatchfieldCreation matchfield = new MatchfieldCreation(10); + Matchfield matchfield = new Matchfield(10); matchfield.createMatchfield(); int calcResult = matchfield.getState(x, y); @@ -44,13 +44,13 @@ class MatchfieldCreationTest { } static Stream testMatchfieldGetFieldState() { - return Stream.of(Arguments.of("field x:0 y:0 has initial state", 0, 0, 1)); + 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) { - MatchfieldCreation matchfield = new MatchfieldCreation(10); + Matchfield matchfield = new Matchfield(10); matchfield.createMatchfield(); matchfield.setState(x, y, state); @@ -60,6 +60,7 @@ class MatchfieldCreationTest { } static Stream testMatchfieldSetStateIsCorrect() { - return Stream.of(Arguments.of("field x:0 y:0 has state 2 after setState()", 0, 0, 2, 2)); + return Stream.of( + Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP)); } }