Пример #1
0
  @Test(expected = GameException.class)
  public void testIllegalPlacementThrowsException() {
    Ship ship = Ship.createHorizontal(new Position(9, 9), 1);
    Ship otherShip = Ship.createHorizontal(new Position(9, 9), 1);

    board.placeShip(ship);
    board.placeShip(otherShip);
  }
Пример #2
0
  @Test
  public void testShipIsNotPlaceableIfOtherShipIsOnSpot() {
    board.placeShip(Ship.createHorizontal(new Position(0, 0), 5));

    Ship ship = Ship.createHorizontal(new Position(4, 0), 2);

    assertThat(board.shipIsPlaceable(ship), is(false));
  }
Пример #3
0
  @Test
  public void testIsNotPlaceableIfOtherShipBordersInYDirection() {
    board.placeShip(Ship.createHorizontal(new Position(0, 1), 5));

    Ship ship = Ship.createHorizontal(new Position(0, 0), 1);

    assertThat(board.shipIsPlaceable(ship), is(false));
  }
Пример #4
0
 public boolean allShipsSunk() {
   for (Ship ship : ships) {
     if (!ship.sunk()) {
       return false;
     }
   }
   return true;
 }
Пример #5
0
  @Test
  public void testAllShipsSunkIsFalseIfSomeShipsPartiallyRemain() {
    Ship ship = Ship.createHorizontal(new Position(0, 0), 2);
    Ship otherShip = Ship.createVertical(new Position(3, 3), 3);

    board.placeShip(ship);
    board.placeShip(otherShip);
    board.shootAt(new Position(0, 0));
    board.shootAt(new Position(1, 0));
    board.shootAt(new Position(3, 3));

    assertThat(board.allShipsSunk(), is(false));
  }
Пример #6
0
  public boolean shipIsPlaceable(Ship ship) {
    for (Position position : ship.getShipPositions()) {
      if (!positionIsOnBoard(position) || positionIsTakenByShip(position)) {
        return false;
      }
    }

    for (Position position : ship.getBoarderPositions()) {
      if (positionIsOnBoard(position) && positionIsTakenByShip(position)) {
        return false;
      }
    }

    return true;
  }
Пример #7
0
  public void shootAt(Position position) {
    if (!positionIsShootableAt(position)) {
      throw GameException.CreateAlreadyShotAt();
    }

    shots2DMap[position.getY()][position.getX()] = true;
    Ship ship = getShipAtPosition(position);

    shots.add(position);

    if (ship != null) {
      ship.hit();
      messageQueue.addHitMessage(position, ship);
    } else {
      messageQueue.addMissMessage(position);
    }
  }
Пример #8
0
  @Test
  public void testAllShipsSunkIsTrueIfAllShipsWereFullyHit() {
    Ship ship = Ship.createHorizontal(new Position(0, 0), 2);

    board.placeShip(ship);
    board.shootAt(new Position(0, 0));
    board.shootAt(new Position(1, 0));

    assertThat(board.allShipsSunk(), is(true));
  }
Пример #9
0
  public void placeShip(Ship ship) {
    if (!shipIsPlaceable(ship)) {
      throw GameException.CreateShipNotPlaceable();
    }

    for (Position position : ship.getShipPositions()) {
      setShipAtPosition(ship, position);
    }
    ships.add(ship);
  }
Пример #10
0
 private BoardPosition processPosition(Position position) {
   if (board.positionWasShotAt(position)) {
     if (board.positionIsTakenByShip(position)) {
       Ship ship = board.getShipAtPosition(position);
       if (ship.sunk()) {
         return BoardPosition.createShipSunk();
       }
       return BoardPosition.createShipHit();
     }
     return BoardPosition.createWaterHit();
   }
   if (mask) {
     return BoardPosition.createUnknown();
   }
   if (board.positionIsTakenByShip(position)) {
     return BoardPosition.createShip();
   }
   return BoardPosition.createWater();
 }
Пример #11
0
  @Test
  public void testIsPlaceableInCorner() {
    Ship ship = Ship.createHorizontal(new Position(9, 9), 1);

    assertThat(board.shipIsPlaceable(ship), is(true));
  }
Пример #12
0
  @Test
  public void testIsNotPlaceableIfShipIsOutOfBoard() {
    Ship ship = Ship.createHorizontal(new Position(9, 9), 2);

    assertThat(board.shipIsPlaceable(ship), is(false));
  }
Пример #13
0
  @Test
  public void testShipIsPlaceableNextToEdge() {
    Ship ship = Ship.createHorizontal(new Position(0, 9), 5);

    assertThat(board.shipIsPlaceable(ship), is(true));
  }
Пример #14
0
  @Test
  public void testShipIsPlaceableForEmptyBoard() {
    Ship ship = Ship.createHorizontal(new Position(2, 2), 5);

    assertThat(board.shipIsPlaceable(ship), is(true));
  }