From 929f2b918b51428d06c64a8ee635beda752b1d48 Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 19:22:33 -0700 Subject: [PATCH 1/7] Added Ship#all method mimicing the Ship#any method --- src/main/java/core/Ship.java | 9 +++++++++ 1 file changed, 9 insertions(+) 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; + } } From e1690f8d89496db93fa01246f3970d27d1c619bc Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 19:29:29 -0700 Subject: [PATCH 2/7] Adjusted isShipSunk to use the new all method --- src/main/java/core/Grid.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/core/Grid.java b/src/main/java/core/Grid.java index c8e1ab1..7ec70c1 100644 --- a/src/main/java/core/Grid.java +++ b/src/main/java/core/Grid.java @@ -161,7 +161,7 @@ public Optional isShipSunk(Coord coordinate) { if (optionalShip.isEmpty()) return Optional.empty(); Ship ship = optionalShip.get(); - if (ship.any(c -> !this.getStatus(c).equals(CellStatus.SHIP_HIT))) { + if (!ship.all(c -> !this.getStatus(c).equals(CellStatus.SHIP_HIT))) { return Optional.empty(); } From 19a9d82509f2a956369976d92877b194a80e72ec Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 19:50:53 -0700 Subject: [PATCH 3/7] Created a new isShipHit method and adjusted isShipSunk to use new isShipHit method --- src/main/java/core/Grid.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/core/Grid.java b/src/main/java/core/Grid.java index 7ec70c1..d262d5e 100644 --- a/src/main/java/core/Grid.java +++ b/src/main/java/core/Grid.java @@ -161,7 +161,7 @@ public Optional isShipSunk(Coord coordinate) { if (optionalShip.isEmpty()) return Optional.empty(); Ship ship = optionalShip.get(); - if (!ship.all(c -> !this.getStatus(c).equals(CellStatus.SHIP_HIT))) { + if (!ship.all(this::isShipHit)) { return Optional.empty(); } @@ -180,4 +180,8 @@ public void shoot(final Coord coordinate) { public long getId() { return id; } + + public boolean isShipHit(Coord coordinate) { + return (getStatus(coordinate).equals(CellStatus.SHIP_HIT)); + } } From 5f0fe4f8360b704a54aad2e8ce649520759614b7 Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 20:26:29 -0700 Subject: [PATCH 4/7] Simplified isShipSunk using .filter --- src/main/java/core/Grid.java | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/main/java/core/Grid.java b/src/main/java/core/Grid.java index d262d5e..cc206ed 100644 --- a/src/main/java/core/Grid.java +++ b/src/main/java/core/Grid.java @@ -157,15 +157,7 @@ public boolean isShipSunk(final Ship ship) { } public Optional isShipSunk(Coord coordinate) { - Optional optionalShip = shipList.getShipAt(coordinate, this); - if (optionalShip.isEmpty()) return Optional.empty(); - - Ship ship = optionalShip.get(); - if (!ship.all(this::isShipHit)) { - return Optional.empty(); - } - - return optionalShip; + return shipList.getShipAt(coordinate, this).filter(ship -> ship.all(this::isShipHit)); } /** changes status of given cell to shoot */ From 66aee29a50bbfb73b9123e25d634ee5d15c4a508 Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 20:58:12 -0700 Subject: [PATCH 5/7] Renamed method and wrote tests. --- src/main/java/core/Game.java | 2 +- src/main/java/core/GameDriver.java | 2 +- src/main/java/core/Grid.java | 4 ++-- src/test/java/core/GridTest.java | 35 ++++++++++++++++++++++++++++-- 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/core/Game.java b/src/main/java/core/Game.java index 45836c9..fb0d82f 100644 --- a/src/main/java/core/Game.java +++ b/src/main/java/core/Game.java @@ -79,7 +79,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..4ce2a66 100644 --- a/src/main/java/core/GameDriver.java +++ b/src/main/java/core/GameDriver.java @@ -44,7 +44,7 @@ private void displayWinner(Game game) { } private void reportIfShipSunk(Game game, Coord playerInputCoord) { - Optional currShip = game.getEnemyGrid().isShipSunk(playerInputCoord); + Optional currShip = game.getEnemyGrid().getSunkShipAt(playerInputCoord); if (currShip.isPresent()) { presenter.displayMessage("You sunk your opponent's " + currShip.get().getName() + "!"); } diff --git a/src/main/java/core/Grid.java b/src/main/java/core/Grid.java index cc206ed..21bce11 100644 --- a/src/main/java/core/Grid.java +++ b/src/main/java/core/Grid.java @@ -147,7 +147,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; @@ -156,7 +156,7 @@ public boolean isShipSunk(final Ship ship) { return true; } - public Optional isShipSunk(Coord coordinate) { + public Optional getSunkShipAt(Coord coordinate) { return shipList.getShipAt(coordinate, this).filter(ship -> ship.all(this::isShipHit)); } 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()); } } From d3be60388bdd9d1896ed1a432a8447e36a184cdd Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 20:59:47 -0700 Subject: [PATCH 6/7] Adjusted reportIfShipSunk in GameDriver --- src/main/java/core/GameDriver.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/core/GameDriver.java b/src/main/java/core/GameDriver.java index 4ce2a66..5a144b2 100644 --- a/src/main/java/core/GameDriver.java +++ b/src/main/java/core/GameDriver.java @@ -44,12 +44,14 @@ private void displayWinner(Game game) { } private void reportIfShipSunk(Game game, Coord playerInputCoord) { - Optional currShip = game.getEnemyGrid().getSunkShipAt(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() { presenter.displayMessage("Game is stopping..."); } From 90c0ebe93b068058e795300203d535e18e99c8c8 Mon Sep 17 00:00:00 2001 From: Jonah Smith Date: Thu, 10 Apr 2025 21:02:05 -0700 Subject: [PATCH 7/7] Adjusted reportIfShipSunk in GameDriver --- src/main/java/core/GameDriver.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/core/GameDriver.java b/src/main/java/core/GameDriver.java index 5a144b2..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; @@ -46,12 +45,12 @@ private void displayWinner(Game game) { private void reportIfShipSunk(Game game, Coord playerInputCoord) { game.getEnemyGrid() .getSunkShipAt(playerInputCoord) - .ifPresent(ship -> presenter.displayMessage( - "You sunk your opponent's " + ship.getName() + "!" - )); + .ifPresent( + ship -> + presenter.displayMessage( + "You sunk your opponent's " + ship.getName() + "!")); } - private void stopGame() { presenter.displayMessage("Game is stopping..."); }