diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 726666d..bfc29fc 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -76,7 +76,7 @@ public class Matchfield { /** * direction 0 => x axis (to the right) direction 1 => y axis (to the bottom) */ - public void setShip(Coordinate coordinate, int length, int direction) { + public boolean setShip(Coordinate coordinate, int length, int direction) { for (int i = 0; i < length; i++) { this.getField(coordinate).setState(Coordinate.SHIP); @@ -84,7 +84,12 @@ public class Matchfield { coordinate = this.getRight(coordinate); if (direction == 1) coordinate = this.getAbove(coordinate); + + if (coordinate == null) + return false; } + + return true; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java index 72b4359..e969003 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.util.stream.Stream; @@ -44,4 +45,21 @@ class MatchfieldPositioningTest { Arguments.of("set ship from 0:2, length 4, direction 1", 10, new Coordinate(0, 2), 1, 4, coordinatesWithShip02_4_1)); } + + @ParameterizedTest(name = "ship positioning on invalid or valid coordinates") + @MethodSource("testShipPositioningFailed") + void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, int matchfieldSize, Coordinate coordinate, + int direction, int length) { + Matchfield matchfield = new Matchfield(matchfieldSize); + matchfield.createMatchfield(); + + boolean calculatedResult = matchfield.setShip(coordinate, length, direction); + assertFalse(calculatedResult); + } + + static Stream testShipPositioningFailed() { + + return Stream.of( + Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2)); + } }