Browse Source

Fix: Above = Right and Below = Left

fleetstorm
Max Wenzel 2 years ago
committed by Lorenz Hohmann
parent
commit
2603cb9f83
  1. 23
      src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java
  2. 24
      src/test/java/de/tims/fleetstorm/matchfield/MatchfieldCreationTest.java

23
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];
}
}

24
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<Arguments> getCoordinateAbove() {
static Stream<Arguments> 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<Arguments> getCoordinateBelow() {
static Stream<Arguments> 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));
}
}
Loading…
Cancel
Save