Browse Source

Implemented setShip() in Matchfield with first test case for 2x1 ship in direction 0

fleetstorm
Lorenz Hohmann 3 years ago
parent
commit
6de05cb134
  1. 8
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  2. 38
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldPositioningTest.java

8
src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java

@ -73,4 +73,12 @@ public class Matchfield {
return matchfield[center.getX()][center.getY() - 1]; 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);
}
} }

38
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<Arguments> 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));
}
}
Loading…
Cancel
Save