示例#1
0
 def add_ship(self, start_coordinates, end_coordinates):
     validate_ship_coordinates(
         self.own_field, self.own_ships, start_coordinates, end_coordinates)
     ship = Ship(start_coordinates, end_coordinates)
     for coordinates in ship.get_all_coordinates():
         self.own_field[coordinates] = Cell(True, coordinates)
     # FIXME: is this really necessary?!
     self.own_ships.add(ship)
示例#2
0
 def add_ship_randomly(self, length=None):
     '''
     length: the length of the ship which will be added; if not given, a
     random value in the intervall [2; 5] is used
     '''
     logger.debug('calling Player.add_ship_randomly')
     start_x = randrange(DEFAULT_FIELD_WIDTH)
     start_y = randrange(DEFAULT_FIELD_HEIGHT)
     start_coordinates = Coordinates(start_x, start_y)
     if length is None:
         length = randrange(
             DEFAULT_MIN_SHIP_LENGTH, DEFAULT_MAX_SHIP_LENGTH + 1)
     is_horintal = random_choice((True, False))
     if is_horintal:
         new_x_coordinate = start_coordinates.x + length
         end_coordinates = Coordinates(
             new_x_coordinate,
             start_coordinates.y)
     else:
         new_y_coordinates = start_coordinates.y + length
         end_coordinates = Coordinates(
             start_coordinates.x,
             new_y_coordinates)
     try:
         validate_ship_coordinates(
             self.own_field, self.own_ships,
             start_coordinates, end_coordinates)
     except OutOfFieldError:
         logger.debug('out of field')
         # `length` was *added* which caused the ship to overlap a border of
         # the field
         # ->
         # `length` must be *subtracted* from the corresponding
         # coordinate of the start point
         if is_horintal:
             end_coordinates_x = start_coordinates.x - length
             end_coordinates = Coordinates(
                 end_coordinates_x,
                 end_coordinates.y)
         else:
             end_coordinates_y = start_coordinates.y - length
             end_coordinates = Coordinates(
                 end_coordinates.x,
                 end_coordinates_y)
     logger.debug(repr((start_coordinates, end_coordinates)))
     self.add_ship(start_coordinates, end_coordinates)
示例#3
0
 def test_length_too_low(self, own_field, own_ships):
     co = Coordinates(4, 7)
     with pytest.raises(ShipTooSmallError):
         validate_ship_coordinates(own_field, own_ships, co, co)
示例#4
0
 def test_nonstraight(self, own_field, own_ships, nonstraight_ship):
     start = nonstraight_ship.start_coordinates
     end = nonstraight_ship.end_coordinates
     with pytest.raises(ShipNotStraightError):
         validate_ship_coordinates(own_field, own_ships, start, end)
示例#5
0
 def test_already_added(self, own_field, own_ships):
     with pytest.raises(ShipAlreadyAdded):
         validate_ship_coordinates(
             own_field, own_ships, Coordinates(0, 0), Coordinates(3, 4))