Browse Source

New test case for shoot(): Check if field was already shot

Lorenz Hohmann 3 years ago
parent
commit
13ff6d73d3
  1. 6
      src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java
  2. 12
      src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java

6
src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java

@ -44,13 +44,15 @@ public class Coordinate {
System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state); System.out.println("X = " + this.x + ", Y = " + this.y + ", State = " + this.state);
} }
public void shoot() {
public boolean shoot() {
if (this.state == Coordinate.SHIP) { if (this.state == Coordinate.SHIP) {
this.state = Coordinate.HIT; this.state = Coordinate.HIT;
return;
return true;
} }
this.state = Coordinate.SHOT; this.state = Coordinate.SHOT;
return true;
} }
} }

12
src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java

@ -1,6 +1,7 @@
package de.tims.fleetstorm.matchfield; package de.tims.fleetstorm.matchfield;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -64,4 +65,15 @@ class CoordinateTest {
Arguments.of("Field State is SHOT", new Coordinate(0, 1), Coordinate.SHOT)); Arguments.of("Field State is SHOT", new Coordinate(0, 1), Coordinate.SHOT));
} }
@Test
void testIfShootCheckWhenFieldIsAlreadyShot() {
Matchfield matchfield = new Matchfield(10);
matchfield.createMatchfield();
// set dummy ship
matchfield.setShip(new Coordinate(3, 3), 4, 1);
assertTrue(matchfield.getField(0, 0).shoot());
}
} }
Loading…
Cancel
Save