From 6de05cb1340a3ee612de9b03e49b614f930b4b0d Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 13:32:51 +0100 Subject: [PATCH] Implemented setShip() in Matchfield with first test case for 2x1 ship in direction 0 --- .../fleetstorm/matchfield/Matchfield.java | 8 ++++ .../matchfield/MatchfieldPositioningTest.java | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 99c5a8d..1d5bdb1 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -73,4 +73,12 @@ public class Matchfield { return matchfield[center.getX()][center.getY() - 1]; } + /** + * direction 0 => x axis (to the right) direction 1 => y axis (to the bottom) + */ + public void setShip(Coordinate coordinate, int length, int direction) { + this.setState(coordinate, Coordinate.SHIP); + this.setState(this.getRight(coordinate), Coordinate.SHIP); + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java new file mode 100644 index 0000000..22d6723 --- /dev/null +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java @@ -0,0 +1,38 @@ + +package de.tims.fleetstorm.matchfield; + +import static org.junit.Assert.assertTrue; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +class MatchfieldPositioningTest { + + @ParameterizedTest(name = "ship was set on the correct positions") + @MethodSource("testShipPositioning") + void testMatchfieldShipSetHasCorrectPositions(String testName, int matchfieldSize, Coordinate coordinate, + int direction, int length, Coordinate[] coordinatesWithShip) { + Matchfield matchfield = new Matchfield(matchfieldSize); + matchfield.createMatchfield(); + + matchfield.setShip(coordinate, length, direction); + + int shipSetCounter = 0; + for (Coordinate toCheckCoordinate : coordinatesWithShip) { + if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) + shipSetCounter++; + } + + assertTrue(shipSetCounter == length); + } + + static Stream testShipPositioning() { + Coordinate[] coordinatesWithShip00_2_0 = new Coordinate[] { new Coordinate(0, 0), new Coordinate(1, 0) }; + + return Stream.of(Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, + coordinatesWithShip00_2_0)); + } +}