diff --git a/src/main/java/core/Game.java b/src/main/java/core/Game.java index 035058c..0de7cf7 100644 --- a/src/main/java/core/Game.java +++ b/src/main/java/core/Game.java @@ -77,7 +77,7 @@ public boolean isOver() { } public boolean isShipSunk(final Ship ship) { - return getEnemyGrid().isShipSunk(ship); + return getEnemyGrid().getSunkShipAt(ship); } private boolean areAllEnemyShipsSunk() { diff --git a/src/main/java/core/GameDriver.java b/src/main/java/core/GameDriver.java index 79f042e..3f29605 100644 --- a/src/main/java/core/GameDriver.java +++ b/src/main/java/core/GameDriver.java @@ -3,7 +3,6 @@ import core.state.Action; import java.util.HashMap; import java.util.Map; -import java.util.Optional; public class GameDriver implements Driver { private final Presenter presenter; @@ -44,10 +43,12 @@ private void displayWinner(Game game) { } private void reportIfShipSunk(Game game, Coord playerInputCoord) { - Optional currShip = game.getEnemyGrid().isShipSunk(playerInputCoord); - if (currShip.isPresent()) { - presenter.displayMessage("You sunk your opponent's " + currShip.get().getName() + "!"); - } + game.getEnemyGrid() + .getSunkShipAt(playerInputCoord) + .ifPresent( + ship -> + presenter.displayMessage( + "You sunk your opponent's " + ship.getName() + "!")); } private void stopGame() { diff --git a/src/main/java/core/Grid.java b/src/main/java/core/Grid.java index baed86f..aebfcc1 100644 --- a/src/main/java/core/Grid.java +++ b/src/main/java/core/Grid.java @@ -144,7 +144,7 @@ public ShipList getShipListObject() { * @param ship is a ship in list of ships * @return boolean value for ship status */ - public boolean isShipSunk(final Ship ship) { + public boolean getSunkShipAt(final Ship ship) { for (final Coord coord : ship.getCoordList()) { if (!this.getStatus(coord).equals(CellStatus.SHIP_HIT)) { return false; @@ -153,16 +153,8 @@ public boolean isShipSunk(final Ship ship) { return true; } - public Optional isShipSunk(Coord coordinate) { - Optional optionalShip = shipList.getShipAt(coordinate, this); - if (optionalShip.isEmpty()) return Optional.empty(); - - Ship ship = optionalShip.get(); - if (ship.any(c -> !this.getStatus(c).equals(CellStatus.SHIP_HIT))) { - return Optional.empty(); - } - - return optionalShip; + public Optional getSunkShipAt(Coord coordinate) { + return shipList.getShipAt(coordinate, this).filter(ship -> ship.all(this::isShipHit)); } /** changes status of given cell to shoot */ @@ -178,6 +170,10 @@ public long getId() { return id; } + public boolean isShipHit(Coord coordinate) { + return (getStatus(coordinate).equals(CellStatus.SHIP_HIT)); + } + /** * Checks if the two grids are the same * diff --git a/src/main/java/core/Ship.java b/src/main/java/core/Ship.java index 01b574e..f16d2d1 100644 --- a/src/main/java/core/Ship.java +++ b/src/main/java/core/Ship.java @@ -128,4 +128,13 @@ public boolean any(Predicate predicate) { } return false; } + + public boolean all(Predicate predicate) { + for (Coord coord : getCoordList()) { + if (!predicate.test(coord)) { + return false; + } + } + return true; + } } diff --git a/src/test/java/core/GridTest.java b/src/test/java/core/GridTest.java index 147e2d0..923415a 100644 --- a/src/test/java/core/GridTest.java +++ b/src/test/java/core/GridTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.*; import java.util.List; +import java.util.Optional; import org.junit.jupiter.api.Test; class GridTest { @@ -117,8 +118,38 @@ public void isShipSunkReturnsTrueIfAllCellsMarkedAsHit() { Coord c3 = new Coord(3, 2); testGrid.shoot(c1); testGrid.shoot(c2); - assertFalse(testGrid.isShipSunk(ship)); + assertFalse(testGrid.getSunkShipAt(ship)); testGrid.shoot(c3); - assertTrue(testGrid.isShipSunk(ship)); + assertTrue(testGrid.getSunkShipAt(ship)); + } + + @Test + public void getSunkShipAtReturnsEmptyWhenNoShipAtCoordinate() { + Optional result = testGrid.getSunkShipAt(new Coord(1, 1)); // Empty cell + assertTrue(result.isEmpty(), "Should return empty when no ship is at the coordinate"); + } + + @Test + public void getSunkShipAtReturnsEmptyWhenShipNotFullyHit() { + Ship ship = new Ship(new Coord(1, 2), 3, VERTICAL, "BattleShip"); + Coord c1 = new Coord(1, 2); + Coord c2 = new Coord(2, 2); + Coord c3 = new Coord(3, 2); + testGrid.shoot(c1); + testGrid.shoot(c2); + Optional result = testGrid.getSunkShipAt(c1); + assertTrue(result.isEmpty(), "Should return empty when ship is not fully hit"); + } + + @Test + public void getSunkShipAtReturnsShipWhenFullyHit() { + Ship ship = new Ship(new Coord(4, 4), 2, HORIZONTAL, "BattleShip"); + testGrid.addShip(ship); + for (Coord coord : ship.getCoordList()) { + testGrid.shoot(coord); + } + Optional result = testGrid.getSunkShipAt(ship.getCoordList().get(0)); + assertTrue(result.isPresent()); + assertEquals("BattleShip", result.get().getName()); } }