Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public boolean isOver() {
}

public boolean isShipSunk(final Ship ship) {
return getEnemyGrid().isShipSunk(ship);
return getEnemyGrid().getSunkShipAt(ship);
}

private boolean areAllEnemyShipsSunk() {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/core/GameDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,10 +43,12 @@ private void displayWinner(Game game) {
}

private void reportIfShipSunk(Game game, Coord playerInputCoord) {
Optional<Ship> 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() {
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/core/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -153,16 +153,8 @@ public boolean isShipSunk(final Ship ship) {
return true;
}

public Optional<Ship> isShipSunk(Coord coordinate) {
Optional<Ship> 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<Ship> getSunkShipAt(Coord coordinate) {
return shipList.getShipAt(coordinate, this).filter(ship -> ship.all(this::isShipHit));
}

/** changes status of given cell to shoot */
Expand All @@ -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
*
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/core/Ship.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,13 @@ public boolean any(Predicate<Coord> predicate) {
}
return false;
}

public boolean all(Predicate<Coord> predicate) {
for (Coord coord : getCoordList()) {
if (!predicate.test(coord)) {
return false;
}
}
return true;
}
}
35 changes: 33 additions & 2 deletions src/test/java/core/GridTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Ship> 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<Ship> 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<Ship> result = testGrid.getSunkShipAt(ship.getCoordList().get(0));
assertTrue(result.isPresent());
assertEquals("BattleShip", result.get().getName());
}
}
Loading