From f8a8179650b936b54acd815a6969dcea52751aa7 Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 14:02:20 +0100 Subject: [PATCH] Refactoring: Implements Coordinate into Matchfield --- src/main/java/de/tims/fleetstorm/ai/Logic.java | 5 +++-- .../tims/fleetstorm/matchfield/Matchfield.java | 16 ++++++---------- .../java/de/tims/fleetstorm/ai/LogicTest.java | 4 +++- .../matchfield/MatchfieldCreationTest.java | 6 +++--- 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/ai/Logic.java b/src/main/java/de/tims/fleetstorm/ai/Logic.java index d918ed5..a76512c 100644 --- a/src/main/java/de/tims/fleetstorm/ai/Logic.java +++ b/src/main/java/de/tims/fleetstorm/ai/Logic.java @@ -3,6 +3,7 @@ package de.tims.fleetstorm.ai; import java.util.ArrayList; import java.util.Random; +import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; public class Logic { @@ -11,14 +12,14 @@ public class Logic { ArrayList possibleFields = new ArrayList(); for (int x = 0; x < Math.sqrt(matchfield.getSize()); x++) { for (int y = 0; y < Math.sqrt(matchfield.getSize()); y++) { - if (matchfield.getState(x, y) == Matchfield.EMPTY || matchfield.getState(x, y) == Matchfield.SHIP) { + if (matchfield.getState(x, y) == Coordinate.EMPTY || matchfield.getState(x, y) == Coordinate.SHIP) { possibleFields.add(new int[] { x, y }); } } } Random randy = new Random(); - return possibleFields.get(randy.nextInt(possibleFields.size() - 1)); + return possibleFields.get(randy.nextInt(possibleFields.size())); } } diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 45912eb..7a30858 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -2,22 +2,18 @@ package de.tims.fleetstorm.matchfield; public class Matchfield { - private int[][] matchfield; + private Coordinate[][] matchfield; private 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]; + this.matchfield = new Coordinate[this.size][this.size]; } - public int[][] createMatchfield() { + public Coordinate[][] createMatchfield() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { - this.matchfield[i][j] = Matchfield.EMPTY; + this.matchfield[i][j] = new Coordinate(i, j); } } @@ -29,11 +25,11 @@ public class Matchfield { } public int getState(int x, int y) { - return this.matchfield[x][y]; + return this.matchfield[x][y].getState(); } public void setState(int x, int y, int state) { - this.matchfield[x][y] = state; + this.matchfield[x][y].setState(state); } } diff --git a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java index d8ebcf5..4fbb52c 100644 --- a/src/test/java/de/tims/fleetstorm/ai/LogicTest.java +++ b/src/test/java/de/tims/fleetstorm/ai/LogicTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import org.junit.jupiter.api.Test; +import de.tims.fleetstorm.matchfield.Coordinate; import de.tims.fleetstorm.matchfield.Matchfield; class LogicTest { @@ -14,6 +15,7 @@ class LogicTest { @Test void testFieldIsNotNull() { + matchfield.createMatchfield(); int[] calcResult = logic.chooseField(matchfield); assertNotNull(calcResult); } @@ -30,6 +32,6 @@ class LogicTest { int[] choosenField = logic.chooseField(matchfield); int calcState = matchfield.getState(choosenField[0], choosenField[1]); - assertNotEquals(calcState, Matchfield.SHOT); + assertNotEquals(calcState, Coordinate.SHOT); } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index d885970..ce47268 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -15,7 +15,7 @@ class MatchfieldCreationTest { @Test void testMatchfieldCreateNotEmpty() { Matchfield matchfield = new Matchfield(0); - int[][] calcResult = matchfield.createMatchfield(); + Coordinate[][] calcResult = matchfield.createMatchfield(); assertNotNull(calcResult); } @@ -44,7 +44,7 @@ class MatchfieldCreationTest { } static Stream testMatchfieldGetFieldState() { - return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Matchfield.EMPTY)); + return Stream.of(Arguments.of("field x:0 y:0 has empty state", 0, 0, Coordinate.EMPTY)); } @ParameterizedTest(name = "matchfield change field is correct") @@ -61,6 +61,6 @@ class MatchfieldCreationTest { static Stream testMatchfieldSetStateIsCorrect() { return Stream.of( - Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Matchfield.SHIP, Matchfield.SHIP)); + Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP)); } }