From 8c49de97677c1e14901dd99c51abc43fb5fa3333 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:44:51 +0100 Subject: [PATCH] New test case for shoot(): Check if field was already shot --- .../de/tims/fleetstorm/matchfield/Coordinate.java | 6 ++++-- .../tims/fleetstorm/matchfield/CoordinateTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java b/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java index 7ce2692..c3d3394 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Coordinate.java +++ b/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); } - public void shoot() { + public boolean shoot() { if (this.state == Coordinate.SHIP) { this.state = Coordinate.HIT; - return; + return true; } this.state = Coordinate.SHOT; + + return true; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java index 0aefe99..fd4b73e 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/CoordinateTest.java @@ -1,6 +1,7 @@ package de.tims.fleetstorm.matchfield; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.stream.Stream; @@ -64,4 +65,15 @@ class CoordinateTest { 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()); + } + }