|
@ -3,9 +3,8 @@ package de.tims.fleetstorm.matchfield; |
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
import static org.junit.jupiter.api.Assertions.*; |
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
|
import java.util.stream.Stream; |
|
|
import java.util.stream.Stream; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
import org.junit.jupiter.params.ParameterizedTest; |
|
|
import org.junit.jupiter.params.provider.Arguments; |
|
|
import org.junit.jupiter.params.provider.Arguments; |
|
@ -13,19 +12,27 @@ import org.junit.jupiter.params.provider.MethodSource; |
|
|
|
|
|
|
|
|
class MatchfieldShipTest { |
|
|
class MatchfieldShipTest { |
|
|
|
|
|
|
|
|
|
|
|
private Matchfield matchfield; |
|
|
|
|
|
private int matchfieldSize = 10; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
|
|
void setup() { |
|
|
|
|
|
matchfield = new Matchfield(matchfieldSize); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "ship was set on the correct positions") |
|
|
@ParameterizedTest(name = "ship was set on the correct positions") |
|
|
@MethodSource("testShipPositioning") |
|
|
@MethodSource("testShipPositioning") |
|
|
void testMatchfieldShipSetHasCorrectPositions(String testName, int matchfieldSize, Coordinate coordinate, |
|
|
|
|
|
int direction, int length, Coordinate[] coordinatesWithShip) { |
|
|
|
|
|
Matchfield matchfield = new Matchfield(matchfieldSize); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
void testMatchfieldShipSetHasCorrectPositions(String testName, Coordinate coordinate, int direction, int length, |
|
|
|
|
|
Coordinate[] coordinatesWithShip) { |
|
|
|
|
|
|
|
|
matchfield.setShip(coordinate, length, direction); |
|
|
matchfield.setShip(coordinate, length, direction); |
|
|
|
|
|
|
|
|
int shipSetCounter = 0; |
|
|
int shipSetCounter = 0; |
|
|
for (Coordinate toCheckCoordinate : coordinatesWithShip) { |
|
|
for (Coordinate toCheckCoordinate : coordinatesWithShip) { |
|
|
if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) |
|
|
|
|
|
|
|
|
if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) { |
|
|
shipSetCounter++; |
|
|
shipSetCounter++; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
assertThat(shipSetCounter).describedAs(testName).isEqualTo(length); |
|
|
assertThat(shipSetCounter).describedAs(testName).isEqualTo(length); |
|
@ -39,42 +46,33 @@ class MatchfieldShipTest { |
|
|
new Coordinate(0, 4), new Coordinate(0, 5) }; |
|
|
new Coordinate(0, 4), new Coordinate(0, 5) }; |
|
|
|
|
|
|
|
|
return Stream.of( |
|
|
return Stream.of( |
|
|
Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, |
|
|
|
|
|
|
|
|
Arguments.of("set ship from 0:0, length 2, direction 0", new Coordinate(0, 0), 0, 2, |
|
|
coordinatesWithShip00_2_0), |
|
|
coordinatesWithShip00_2_0), |
|
|
Arguments.of("set ship from 0:1, length 3, direction 0", 10, new Coordinate(0, 1), 0, 3, |
|
|
|
|
|
|
|
|
Arguments.of("set ship from 0:1, length 3, direction 0", new Coordinate(0, 1), 0, 3, |
|
|
coordinatesWithShip01_3_0), |
|
|
coordinatesWithShip01_3_0), |
|
|
Arguments.of("set ship from 0:2, length 4, direction 1", 10, new Coordinate(0, 2), 1, 4, |
|
|
|
|
|
|
|
|
Arguments.of("set ship from 0:2, length 4, direction 1", new Coordinate(0, 2), 1, 4, |
|
|
coordinatesWithShip02_4_1)); |
|
|
coordinatesWithShip02_4_1)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "ship positioning on invalid or valid coordinates") |
|
|
@ParameterizedTest(name = "ship positioning on invalid or valid coordinates") |
|
|
@MethodSource("testShipPositioningFailed") |
|
|
@MethodSource("testShipPositioningFailed") |
|
|
void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, int matchfieldSize, Coordinate coordinate, |
|
|
|
|
|
int direction, int length, boolean expectedResult) { |
|
|
|
|
|
Matchfield matchfield = new Matchfield(matchfieldSize); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, Coordinate coordinate, int direction, |
|
|
|
|
|
int length, boolean expectedResult) { |
|
|
|
|
|
|
|
|
boolean calculatedResult = matchfield.setShip(coordinate, length, direction); |
|
|
boolean calculatedResult = matchfield.setShip(coordinate, length, direction); |
|
|
|
|
|
|
|
|
if (expectedResult) |
|
|
|
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(true); |
|
|
|
|
|
|
|
|
|
|
|
if (!expectedResult) |
|
|
|
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(false); |
|
|
|
|
|
|
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static Stream<Arguments> testShipPositioningFailed() { |
|
|
static Stream<Arguments> testShipPositioningFailed() { |
|
|
return Stream.of( |
|
|
return Stream.of( |
|
|
Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2, |
|
|
|
|
|
false), |
|
|
|
|
|
Arguments.of("valid ship position from 8:0, length 5, direction 1", 10, new Coordinate(9, 0), 1, 5, |
|
|
|
|
|
|
|
|
Arguments.of("invalid ship position from 9:0, length 2, direction 0 (x-axis)", new Coordinate(9, 0), 0, |
|
|
|
|
|
2, false), |
|
|
|
|
|
Arguments.of("valid ship position from 8:0, length 5, direction 1 (y-axis)", new Coordinate(9, 0), 1, 5, |
|
|
true)); |
|
|
true)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void testShipPositionAlreadySetWithShip() { |
|
|
void testShipPositionAlreadySetWithShip() { |
|
|
Matchfield matchfield = new Matchfield(10); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
|
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship") |
|
|
assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship") |
|
@ -88,9 +86,6 @@ class MatchfieldShipTest { |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() { |
|
|
void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() { |
|
|
Matchfield matchfield = new Matchfield(10); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
|
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
boolean calulatedResult = matchfield.areAllShipsHit(); |
|
|
boolean calulatedResult = matchfield.areAllShipsHit(); |
|
|
assertFalse(calulatedResult); |
|
|
assertFalse(calulatedResult); |
|
@ -98,15 +93,9 @@ class MatchfieldShipTest { |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() { |
|
|
void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() { |
|
|
Matchfield matchfield = new Matchfield(10); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
|
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
matchfield.getField(0, 0).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 1).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 2).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 3).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 4).setState(Coordinate.HIT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setFieldRangeHit(matchfield); |
|
|
|
|
|
|
|
|
boolean calulatedResult = matchfield.areAllShipsHit(); |
|
|
boolean calulatedResult = matchfield.areAllShipsHit(); |
|
|
assertTrue(calulatedResult); |
|
|
assertTrue(calulatedResult); |
|
@ -114,15 +103,9 @@ class MatchfieldShipTest { |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() { |
|
|
void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() { |
|
|
Matchfield matchfield = new Matchfield(10); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
|
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
matchfield.setShip(new Coordinate(0, 0), 5, 1); |
|
|
matchfield.getField(0, 0).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 1).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 2).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 3).setState(Coordinate.HIT); |
|
|
|
|
|
matchfield.getField(0, 4).setState(Coordinate.HIT); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setFieldRangeHit(matchfield); |
|
|
|
|
|
|
|
|
matchfield.setShip(new Coordinate(3, 4), 2, 0); |
|
|
matchfield.setShip(new Coordinate(3, 4), 2, 0); |
|
|
matchfield.getField(4, 4).setState(Coordinate.HIT); |
|
|
matchfield.getField(4, 4).setState(Coordinate.HIT); |
|
@ -130,4 +113,45 @@ class MatchfieldShipTest { |
|
|
boolean calulatedResult = matchfield.areAllShipsHit(); |
|
|
boolean calulatedResult = matchfield.areAllShipsHit(); |
|
|
assertFalse(calulatedResult); |
|
|
assertFalse(calulatedResult); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setFieldRangeHit(Matchfield matchfield) { |
|
|
|
|
|
for (int i = 0; i <= 4; i++) { |
|
|
|
|
|
matchfield.getField(0, i).setState(Coordinate.HIT); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest(name = "matchfield returns correct state if a position is free or not") |
|
|
|
|
|
@MethodSource("testIsFreeMethod") |
|
|
|
|
|
void testMatchfieldReturnsCorrectValueAboutFreeCoordinateSlots(String testName, Coordinate originCoordinate, |
|
|
|
|
|
int direction, int length, Coordinate[] coordinatesWithShip, boolean expectedResult) { |
|
|
|
|
|
|
|
|
|
|
|
Matchfield matchfield = new Matchfield(matchfieldSize); |
|
|
|
|
|
matchfield.createMatchfield(); |
|
|
|
|
|
|
|
|
|
|
|
for (Coordinate coordinate : coordinatesWithShip) { |
|
|
|
|
|
matchfield.getField(coordinate).setState(Coordinate.SHIP); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
boolean calculatedResult = matchfield.isFreePosition(originCoordinate, length, direction); |
|
|
|
|
|
assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static Stream<Arguments> testIsFreeMethod() { |
|
|
|
|
|
return Stream.of( |
|
|
|
|
|
Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2, |
|
|
|
|
|
new Coordinate[] { new Coordinate(0, 0) }, false), |
|
|
|
|
|
Arguments.of("no ship is within the coordinates", new Coordinate(1, 1), 0, 5, new Coordinate[] {}, |
|
|
|
|
|
true), |
|
|
|
|
|
Arguments.of("one of the coordinates is hit, others on free fields", new Coordinate(5, 5), 1, 3, |
|
|
|
|
|
new Coordinate[] { new Coordinate(5, 7), new Coordinate(5, 8), new Coordinate(5, 9) }, false)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testIfRandomShipPositioningIsWorking() { |
|
|
|
|
|
for (int shipLength = 2; shipLength <= 5; shipLength++) { |
|
|
|
|
|
boolean calculatedResult = matchfield.setShipOnRandomPosition(shipLength); |
|
|
|
|
|
assertTrue(calculatedResult); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |