From 04359bd78e18233f18bc6490d3af6030c1f0a97d Mon Sep 17 00:00:00 2001 From: Max Wenzel Date: Wed, 12 Jan 2022 15:55:59 +0100 Subject: [PATCH] Fix: Above = Right and Below = Left --- .../fleetstorm/matchfield/Matchfield.java | 23 +++++++++--------- .../matchfield/MatchfieldCreationTest.java | 24 +++++++++---------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 9c86328..4c2b5b2 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -32,26 +32,27 @@ public class Matchfield { this.matchfield[x][y].setState(state); } - public Coordinate getAbove(Coordinate center) { + public Coordinate getRight(Coordinate center) { + if (center.getX() == matchfield.length - 1) { + return null; + } return this.matchfield[center.getX() + 1][center.getY()]; } + public Coordinate getLeft(Coordinate center) { + if (center.getX() == 0) { + return null; + } + return this.matchfield[center.getX() - 1][center.getY()]; + } + public Coordinate getField(Coordinate coordinate) { return matchfield[coordinate.getX()][coordinate.getY()]; } public Coordinate getField(int x, int y) { - if (x == 0) { - return null; - } - return this.matchfield[x][y]; - } - public Coordinate getBelow(Coordinate center) { - if (center.getX() == 0) { - return null; - } - return this.matchfield[center.getX() - 1][center.getY()]; + return this.matchfield[x][y]; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java index 1936f86..e99406d 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java @@ -64,35 +64,35 @@ class MatchfieldCreationTest { Arguments.of("field x:0 y:0 has state SHIP after setState()", 0, 0, Coordinate.SHIP, Coordinate.SHIP)); } - @ParameterizedTest(name = "Get the Coordinate above") - @MethodSource("getCoordinateAbove") + @ParameterizedTest(name = "Get the Coordinate right") + @MethodSource("getCoordinateRight") void testGetAbove(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { - Coordinate result = matchfield.getAbove(center); + Coordinate result = matchfield.getRight(center); assertThat(result).describedAs(testName).isEqualTo(expectedResult); } - static Stream getCoordinateAbove() { + static Stream getCoordinateRight() { Matchfield matchfield = new Matchfield(10); return Stream.of( - Arguments.of("above from (5/5) - should be (6,5)", matchfield, new Coordinate(5, 5), + Arguments.of("right from (5/5) - should be (6,5)", matchfield, new Coordinate(5, 5), matchfield.getField(6, 5)), - Arguments.of("above from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); + Arguments.of("right from (9/5) - should be null", matchfield, new Coordinate(9, 5), null)); } - @ParameterizedTest(name = "Get the Coordinate below") - @MethodSource("getCoordinateBelow") + @ParameterizedTest(name = "Get the Coordinate left") + @MethodSource("getCoordinateLeft") void testGetBelow(String testName, Matchfield matchfield, Coordinate center, Coordinate expectedResult) { - Coordinate result = matchfield.getBelow(center); + Coordinate result = matchfield.getLeft(center); assertThat(result).describedAs(testName).isEqualTo(expectedResult); } - static Stream getCoordinateBelow() { + static Stream getCoordinateLeft() { Matchfield matchfield = new Matchfield(10); return Stream.of( - Arguments.of("bewlow from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), + Arguments.of("left from (5/5) - should be (4,5)", matchfield, new Coordinate(5, 5), matchfield.getField(4, 5)), - Arguments.of("bewlow from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); + Arguments.of("left from (0/5) - should be null", matchfield, new Coordinate(0, 5), null)); } }