From fadf708556e8858251b6be27f707fbc958ca3eb1 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:38:25 +0100 Subject: [PATCH] Added shoot() function to Coordinate class with first test case --- .../fleetstorm/matchfield/Coordinate.java | 4 ++++ .../fleetstorm/matchfield/CoordinateTest.java | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index a11c666..f31b14c 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java @@ -44,4 +44,8 @@ public class Coordinate { System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state); } + public void shoot() { + this.setState(Coordinate.HIT); + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 3de5fc8..9705b32 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -44,4 +44,23 @@ class CoordinateTest { Arguments.of("State = EMPTY", 0, Coordinate.EMPTY, Coordinate.EMPTY)); } + @ParameterizedTest(name = "test if shoot function sets correct field states") + @MethodSource("ShootFunctionFieldStates") + void testShootFunctionSetsCorrectFieldStates(String testName, Coordinate coordinateToTest, int expectedState) { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + + // set dummy ship + matchfield.setShip(new Coordinate(0, 0), 5, 0); + + Coordinate realCoordinate = matchfield.getField(coordinateToTest); + realCoordinate.shoot(); + int calculatedResult = realCoordinate.getState(); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedState); + } + + static Stream ShootFunctionFieldStates() { + return Stream.of(Arguments.of("Field State is HIT", new Coordinate(0, 0), Coordinate.HIT)); + } + }