From 27e7a96ba73467d5c48fd89d85e4b713d7fd1bc9 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Mon, 17 Jan 2022 15:02:25 +0100 Subject: [PATCH] Added second test case for areAllShipsHit() - all ships hit test --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 12 +++++++++++- .../fleetstorm/matchfield/MatchfieldShipTest.java | 8 ++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index a3e4abe..cca91c9 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -97,7 +97,17 @@ public class Matchfield { } public boolean areAllShipsHit() { - return false; + int shipCounter = 0; + + for (int i = 0; i < size; i++) { + for (int j = 0; j < size; j++) { + int fieldState = this.matchfield[i][j].getState(); + if (fieldState == Coordinate.SHIP) + shipCounter++; + } + } + + return shipCounter == 0; } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 5052bf3..0d08687 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -92,5 +92,13 @@ class MatchfieldShipTest { boolean calulatedResult = matchfield.areAllShipsHit(); assertFalse(calulatedResult); + + 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); + calulatedResult = matchfield.areAllShipsHit(); + assertTrue(calulatedResult); } }