Beispiel #1
0
 def test_apply_action_collide_with_ship(self):
     ship1 = Ship('1', Location(150, 100), 0, 8, 6)
     ship2 = Ship('2', Location(155, 100), 0, 8, 6)
     environment = Environment(300, 200, [ship1, ship2])
     action = Action(ship1.uid, 0, True)
     with self.assertRaises(Exception):
         environment.apply_action(action)
Beispiel #2
0
 def test_init(self):
     ship1 = Ship('1', Location(1, 2), 90, 8, 6)
     ship2 = Ship('2', Location(1, 2), 90, 12, 4)
     environment = Environment(300, 200, [ship1, ship2])
     self.assertEqual(300, environment.width)
     self.assertEqual(200, environment.height)
     self.assertEqual(2, len(environment.ships))
     self.assertEqual(ship1, environment.ships[0])
     self.assertEqual(ship2, environment.ships[1])
Beispiel #3
0
 def test_to_dict(self):
     ship = Ship('uid', Location(1, 2), 90, 8, 12)
     self.assertEqual({
         'uid': 'uid',
         'location': {
             'x': 1,
             'y': 2
         },
         'orientation': 90,
         'size': 8,
         'speed': 12
     }, ship.to_dict())
Beispiel #4
0
 def test_apply_action(self):
     ship = Ship('uid', Location(150, 100), 90, 8, 6)
     environment = Environment(300, 200, [ship])
     action = Action(ship.uid, 0, True)
     environment.apply_action(action)
     self.assertEqual(Location(150, 94),
                      ship.location)  # moved up 6 (the ship's speed)
Beispiel #5
0
 def test_init(self):
     ship = Ship('uid', Location(1, 2), 90, 8, 12)
     self.assertEqual('uid', ship.uid)
     self.assertEqual(Location(1, 2), ship.location)
     self.assertEqual(90, ship.orientation)
     self.assertEqual(8, ship.size)
     self.assertEqual(12, ship.speed)
Beispiel #6
0
    def __init__(self, width, height):
        super().__init__(width, height)
 
        arcade.set_background_color(arcade.color.BLACK)
 
        self.ship = Ship(100,100)
        #self.ship_sprite = arcade.Sprite('images/ship.png')
        self.ship_sprite = ModelSprite('images/ship.png',model=self.ship)
Beispiel #7
0
    def addShip(self, coordinates, player: int):
        """
        Adds a ship to the game for the specified player at the given coordinates.
        The rest of the board is unmodified
        If all the ships are places, subsequent calls to this function replace the ships that already exist
        :param coordinates: The coordinates of the ship squares in the form C1L1C2L2C3L3
        :param player: The player that adds the ship
        :raises CoordinateInvalid: If the given string do not represent coordinates or
        the coordinates are out of the board or
        the ship intersects with another ship
        In case an exception is thrown, the board remains unmodified
        """
        if len(coordinates) != 6:
            raise CoordinateInvalid()

        coordinate1 = coordinates[0:2]
        coordinate2 = coordinates[2:4]
        coordinate3 = coordinates[4:6]

        shipCoordinates = [coordinate1, coordinate2, coordinate3]

        Validator.validateCoordinate(coordinate1)
        Validator.validateCoordinate(coordinate2)
        Validator.validateCoordinate(coordinate3)
        Validator.validateShip(coordinate1, coordinate2, coordinate3)

        board = self.boardRepository.getBoard(player, 1)

        bonusShip = None
        if self.shipRepository.length(player) == 2:
            # remove ship to be replaced
            bonusShip = self.shipRepository.getShip(
                self.shipRepository.shipIndex[player], player)
            for coordinate in bonusShip.coordinates:
                board.markAtCoordinate(coordinate, ".")

        for coordinate in shipCoordinates:
            if board.getMark(coordinate) != ".":
                if bonusShip is not None:
                    #  put the bonus ship back
                    for bonusShipCoordinates in bonusShip.coordinates:
                        board.markAtCoordinate(bonusShipCoordinates, "+")

                raise CoordinateInvalid()

        for coordinate in shipCoordinates:
            board.markAtCoordinate(coordinate, "+")

        ship = Ship(coordinate1, coordinate2, coordinate3)

        if self.shipRepository.length(player) != 2:
            self.shipRepository.addShip(ship, player)
        else:
            self.shipRepository.putShip(ship,
                                        self.shipRepository.shipIndex[player],
                                        player)
Beispiel #8
0
def test_ship(data_num):
    ship_list = []
    for i in range(data_num):
        ran_lon_lat = random.randint(0, len(lon_lat_list) - 1)
        ship_list.append(
            Ship(ship_id=random.randint(10000, 99999),
                 cur_pos=(lon_lat_list[ran_lon_lat][0],
                          lon_lat_list[ran_lon_lat][1]),
                 weight=random.randint(2000, 120000)))
    return (ship_list)
Beispiel #9
0
 def test_to_dict(self):
     ship1 = Ship('1', Location(1, 2), 90, 8, 6)
     ship2 = Ship('2', Location(1, 2), 90, 12, 4)
     environment = Environment(300, 200, [ship1, ship2])
     expected = {
         'width': 300,
         'height': 200,
         'ships': [ship1.to_dict(), ship2.to_dict()]
     }
     self.assertEqual(expected, environment.to_dict())
Beispiel #10
0
def run_game():
    # Initialize display
    pygame.init()
    pygame.display.set_caption(APPLICATION_NAME)
    screen = pygame.display.set_mode(
        (game_settings.screen_width, game_settings.screen_height))
    ship = Ship(screen)

    elements = [ship]

    # Main game loop
    while True:
        game_functions.check_events(ship, elements)
        game_functions.update_positions(elements)
        game_functions.refresh_screen(screen, game_settings.background_color,
                                      elements)
Beispiel #11
0
 def test_apply_action_collide_with_right_wall(self):
     ship = Ship('uid', Location(295, 100), 0, 8, 6)
     environment = Environment(300, 200, [ship])
     action = Action(ship.uid, 0, True)
     with self.assertRaises(Exception):
         environment.apply_action(action)
Beispiel #12
0
 def test_from_dict(self):
     ship1 = Ship('1', Location(1, 2), 90, 8, 6)
     ship2 = Ship('2', Location(1, 2), 90, 12, 4)
     environment = Environment(300, 200, [ship1, ship2])
     self.assertEqual(environment,
                      Environment.from_dict(environment.to_dict()))
Beispiel #13
0
 def test_repr(self):
     ship = Ship('uid', Location(1, 2), 90, 8, 12)
     self.assertEqual('{"uid": "uid", "location": {"x": 1, "y": 2}, "orientation": 90, "size": 8, "speed": 12}',
                      str(ship))
Beispiel #14
0
 def test_from_dict(self):
     ship = Ship('uid', Location(1, 2), 90, 8, 12)
     self.assertEqual(ship, Ship.from_dict(ship.to_dict()))