Skip to content

Commit

Permalink
Merge pull request #58 from Lily-Kate/main
Browse files Browse the repository at this point in the history
Added method addShip() to Ship Class, and necessary changes to helper…
  • Loading branch information
skiadas authored Nov 19, 2024
2 parents b81a0cb + d15e7d9 commit 7180df7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 45 deletions.
45 changes: 25 additions & 20 deletions src/main/java/core/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class Grid {

private Cell[][] grid;

private List<Ship> shipList;

private final List<Ship> shipList = new ArrayList<>();
private List<Cell> chosenCells;

public Grid(int rows, int cols, List<Ship> shipList) {
Expand All @@ -29,23 +28,28 @@ public Grid(int rows, int cols, List<Ship> shipList) {
}
}

this.shipList = shipList;
markShipCells();
this.shipList.addAll(shipList);

markAllShipCells();
}

public Grid(int rows, int cols) {
this(rows, cols, new ArrayList<>());
}

private void markShipCells() {
private void markAllShipCells() {
for (Ship ship : shipList) {
for (Coord coord : ship.getCoordList()) {
get(coord).setAsShip();
}
markShipCells(ship);
}
}

public Cell get(Coord coordinate) {
private void markShipCells(Ship ship) {
for (Coord coord : ship.getCoordList()) {
getCell(coord).setAsShip();
}
}

public Cell getCell(Coord coordinate) {
int row = coordinate.row - 1;
int col = coordinate.col - 1;
return grid[row][col];
Expand All @@ -60,18 +64,14 @@ public int numCols() {
}

public static List<Ship> defaultShipsFor5x5() {
Coord c1 = new Coord(1, 2);
Coord c2 = new Coord(5, 1);
Coord c3 = new Coord(1, 5);
Ship ship1 = new Ship(c1, 3, VERTICAL, "Submarine");
Ship ship2 = new Ship(c2, 5, HORIZONTAL, "Carrier");
Ship ship3 = new Ship(c3, 3, VERTICAL, "Destroyer");
Ship ship1 = new Ship(new Coord(1, 2), 3, VERTICAL, "Submarine");
Ship ship2 = new Ship(new Coord(5, 1), 5, HORIZONTAL, "Carrier");
Ship ship3 = new Ship(new Coord(1, 5), 3, VERTICAL, "Destroyer");
return List.of(ship1, ship2, ship3);
}

public static Grid defaultGrid() {
Grid g = new Grid(5, 5, defaultShipsFor5x5());
return g;
return new Grid(5, 5, defaultShipsFor5x5());
}

public boolean isValid(Coord coordinate) {
Expand All @@ -80,11 +80,16 @@ public boolean isValid(Coord coordinate) {
return row >= 0 && row < rows && col >= 0 && col < cols;
}

public void addShip(Ship ship) {
shipList.add(ship);
markShipCells(ship);
}

public boolean allShipsAreSunk() {
for (Ship ship : shipList) {
List<Coord> coords = ship.getCoordList();
for (Coord coord : coords) {
if (!this.get(coord).cellIsHit()) {
if (!this.getCell(coord).cellIsHit()) {
return false;
}
}
Expand All @@ -98,7 +103,7 @@ public List<Ship> getShipList() {

public boolean isShipSunk(Ship ship) {
for (Coord coord : ship.getCoordList()) {
if (!(get(coord).cellIsHit())) {
if (!(getCell(coord).cellIsHit())) {
return false;
}
}
Expand All @@ -114,7 +119,7 @@ public boolean isShipOnGrid(Ship ship) {
}

public void shoot(Coord coordinate) {
Cell target = get(coordinate);
Cell target = getCell(coordinate);
if (!target.hasBeenShot()) {
target.setAsShot();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ui/TextPresenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void displayGrid(Grid g) {
for (int row = 1; row <= numOfRows; row++) {
output.print(" " + (letter[row]) + " ");
for (int col = 1; col <= numOfCols; col++) {
Cell cell = g.get(new Coord(row, col));
Cell cell = g.getCell(new Coord(row, col));
setCellAs(cell);
}
output.print("\n\n");
Expand Down
62 changes: 44 additions & 18 deletions src/test/java/core/GridTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package core;

import static core.Ship.Direction.HORIZONTAL;
import static core.Ship.Direction.VERTICAL;
import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -20,19 +21,19 @@ public void aNewGridHasProvidedDimensions() {
public void aNewGridHasNoneNullCells() {
for (int i = 1; i <= testGrid.numRows(); i++) {
for (int j = 1; j <= testGrid.numCols(); j++) {
assertNotEquals(null, testGrid.get(new Coord(i, j)));
assertNotEquals(null, testGrid.getCell(new Coord(i, j)));
}
}
}

@Test
public void aNewGridCorrectlyMarksShips() {
assertTrue(testGrid.get(new Coord(1, 2)).hasShip());
assertTrue(testGrid.get(new Coord(2, 2)).hasShip());
assertTrue(testGrid.get(new Coord(3, 2)).hasShip());
assertTrue(testGrid.get(new Coord(5, 2)).hasShip());
assertFalse(testGrid.get(new Coord(4, 2)).hasShip());
assertFalse(testGrid.get(new Coord(1, 1)).hasShip());
assertTrue(testGrid.getCell(new Coord(1, 2)).hasShip());
assertTrue(testGrid.getCell(new Coord(2, 2)).hasShip());
assertTrue(testGrid.getCell(new Coord(3, 2)).hasShip());
assertTrue(testGrid.getCell(new Coord(5, 2)).hasShip());
assertFalse(testGrid.getCell(new Coord(4, 2)).hasShip());
assertFalse(testGrid.getCell(new Coord(1, 1)).hasShip());
}

@Test
Expand All @@ -47,9 +48,34 @@ public void isTheCoordinateNotWithinGrid() {
assertEquals(false, result);
}

@Test
public void addShipUpdatesShipListCorrectly() {
Ship ship1 = new Ship(new Coord(1, 2), 3, VERTICAL, "Submarine");
Ship ship2 = new Ship(new Coord(5, 1), 5, HORIZONTAL, "Carrier");
Ship ship3 = new Ship(new Coord(1, 5), 3, VERTICAL, "Destroyer");
Ship addedShip = new Ship(new Coord(4, 5), 1, VERTICAL, "Ship");
testGrid.addShip(addedShip);
assertTrue(assertEqualShips(ship1, testGrid.getShipList().get(0)));
assertTrue(assertEqualShips(ship2, testGrid.getShipList().get(1)));
assertTrue(assertEqualShips(ship3, testGrid.getShipList().get(2)));
assertTrue(assertEqualShips(addedShip, testGrid.getShipList().get(3)));
}

public static boolean assertEqualShips(Ship expected, Ship actual) {
if (expected.getSize() != actual.getSize()) {
return false;
}
for (int i = 0; i < expected.getSize(); i++) {
if (!expected.getCoordList().get(i).isEqual(actual.getCoordList().get(i))) {
return false;
}
}
return true;
}

@Test
public void whenShootingAndNotAlreadyShotChangeCellToShot() {
Cell shootCell = testGrid.get(new Coord(2, 2));
Cell shootCell = testGrid.getCell(new Coord(2, 2));
testGrid.shoot(new Coord(2, 2));
assertTrue(shootCell.hasBeenShot());
}
Expand All @@ -62,9 +88,9 @@ public void allShipsHaveNotBeenShot() {

@Test
public void allShipsAreParticallyShot() {
Cell cell1 = testGrid.get(new Coord(5, 1));
Cell cell1 = testGrid.getCell(new Coord(5, 1));
cell1.setAsHit();
Cell cell2 = testGrid.get(new Coord(1, 5));
Cell cell2 = testGrid.getCell(new Coord(1, 5));
cell2.setAsHit();
boolean result = testGrid.allShipsAreSunk();
assertFalse(result);
Expand All @@ -76,8 +102,8 @@ public void allShipsAreShot() {
for (Ship ship : shipList) {
List<Coord> coords = ship.getCoordList();
for (Coord coord : coords) {
testGrid.get(coord).setAsShot();
testGrid.get(coord).setAsHit();
testGrid.getCell(coord).setAsShot();
testGrid.getCell(coord).setAsHit();
}
}
boolean result = testGrid.allShipsAreSunk();
Expand All @@ -103,13 +129,13 @@ public void isShipSunkReturnsTrueIfAllCellsMarkedAsHit() {
Coord c1 = new Coord(1, 2);
Coord c2 = new Coord(2, 2);
Coord c3 = new Coord(3, 2);
testGrid.get(c1).setAsShip();
testGrid.get(c2).setAsShip();
testGrid.get(c3).setAsShip();
testGrid.get(c1).setAsShot();
testGrid.get(c2).setAsShot();
testGrid.getCell(c1).setAsShip();
testGrid.getCell(c2).setAsShip();
testGrid.getCell(c3).setAsShip();
testGrid.getCell(c1).setAsShot();
testGrid.getCell(c2).setAsShot();
assertFalse(testGrid.isShipSunk(ship));
testGrid.get(c3).setAsShot();
testGrid.getCell(c3).setAsShot();
assertTrue(testGrid.isShipSunk(ship));
}
}
12 changes: 6 additions & 6 deletions src/test/java/ui/TextPresenterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void whenDisplayGridIsCalled_CreatesRectangularGridThatHas1hit() {
TestIOProvider ioProvider = TestIOProvider.withInput("");
Grid grid = new Grid(2, 3);
TextPresenter presenter = new TextPresenter(ioProvider);
Cell cell = grid.get(new Coord(1, 1));
Cell cell = grid.getCell(new Coord(1, 1));
cell.setAsHit();
presenter.displayGrid(grid);
String expected =
Expand All @@ -163,11 +163,11 @@ void whenDisplayGridIsCalled_CreatesRectangularGridThatHas2hitsAMissAndAShip() {
Grid g = new Grid(5, 7);
TextPresenter presenter = new TextPresenter(ioProvider);

g.get(new Coord(1, 1)).setAsHit();
g.get(new Coord(4, 5)).setAsHit();
g.get(new Coord(5, 7)).setAsHit();
g.get(new Coord(2, 6)).setAsMiss();
g.get(new Coord(3, 2)).setAsShip();
g.getCell(new Coord(1, 1)).setAsHit();
g.getCell(new Coord(4, 5)).setAsHit();
g.getCell(new Coord(5, 7)).setAsHit();
g.getCell(new Coord(2, 6)).setAsMiss();
g.getCell(new Coord(3, 2)).setAsShip();

presenter.displayGrid(g);
String expected =
Expand Down

0 comments on commit 7180df7

Please sign in to comment.