Exemple #1
0
 def test_fill_island(self):
     # fmt: off
     game_map = create_map_with([[1, 1, E, 1], [1, 1, E, 1], [E, E, E, E],
                                 [E, E, E, E]])
     # fmt: on
     ps = PlayerState(id=1,
                      name="dummy",
                      game_map=game_map,
                      position=Position(2, 2))
     gs = GameState(game_map, [ps])
     ps.tail = [
         ps.spawn_position,
         Position(2, 3),
         Position(3, 3),
         Position(4, 3),
         Position(4, 2)
     ]
     self.assertEqual(len(gs.fill(ps)), 3)
     # fmt: off
     self.assertEqual(
         game_map,
         create_map_with([[1, 1, E, 1], [1, 1, E, 1], [E, 1, 1, 1],
                          [E, E, E, E]]))
     # fmt: on
     self.assertEqual(ps.score, 3)
Exemple #2
0
 def test_fill_planet_to_planet_2(self):
     # fmt: off
     game_map = create_map_with([[E, E, E, P1, 1], [E, E, E, 1, P1],
                                 [E, E, E, E, E], [E, E, E, E, E],
                                 [E, E, E, E, E]])
     # fmt: on
     ps = PlayerState(id=1,
                      name="dummy",
                      game_map=game_map,
                      position=Position(4, 1))
     gs = GameState(game_map, [ps])
     ps.tail = [
         Position(4, 1),
         Position(3, 1),
         Position(2, 1),
         Position(2, 2),
         Position(2, 3),
         Position(2, 4),
         Position(3, 4),
         Position(4, 4),
         Position(5, 4),
         Position(5, 3),
         Position(5, 2),
     ]
     self.assertEqual(len(gs.fill(ps)), 12)
     # fmt: off
     self.assertEqual(
         game_map,
         create_map_with([[E, 1, 1, P1, 1], [E, 1, 1, 1,
                                             P1], [E, 1, 1, 1, 1],
                          [E, 1, 1, 1, 1], [E, E, E, E, E]]))
     # fmt: on
     self.assertEqual(ps.score, 12)
Exemple #3
0
    def test_walk_on_tail_kill(self):
        # player 1 has some conquered tiles n the map
        # fmt: off
        gm = create_map_with([[E, E, E], [E, E, E], [1, 1, 1]])
        # fmt: on
        p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1))
        p2 = PlayerState(id=2, name="2", game_map=gm, position=Position(3, 2))
        gs = GameState(gm, players=[p1, p2])
        self.assertEqual(gm.count_tiles_owned_by(player_id=1), 4)
        self.assertEqual(gm.count_tiles_owned_by(player_id=2), 1)
        self.assertEqual(p1.direction, Direction.RIGHT)
        self.assertEqual(p2.direction, Direction.LEFT)

        # create some tail
        gs.apply_action(1, p1, Action.FORWARD)
        gs.apply_action(2, p1, Action.FORWARD)

        # p2 walk on p1 tail and kill it
        gs.apply_action(3, p2, Action.TURN_RIGHT)
        self.assertEqual(p2.position, Position(3, 1))
        self.assertTrue(p1.killed)
        self.assertEqual(gm.count_tiles_owned_by(player_id=1), 1)
        self.assertEqual(gm.count_tiles_owned_by(player_id=2), 1)
        self.assertEqual(1, p1.stats.stats[PlayerStats.KILLED])
        self.assertEqual(p2.name, p1.stats.stats[PlayerStats.NEMESIS])
        self.assertEqual(1, p2.stats.stats[PlayerStats.KILLS])
Exemple #4
0
    def move_player(self, player: PlayerState, action: Action) -> None:
        prev_position = player.position.copy()
        prev_direction = player.direction.copy()

        player.direction.change_direction(action)
        player.position.move(player.direction)

        self.logger.info(
            f"Player '{player.name_str()}' moving to position: {player.position}."
        )
        player.add_history(
            self.game_tick,
            f"Moving {action.value} from {prev_position}-{prev_direction.name()} to {player.position}-{player.direction.name()}.",
        )

        # Append tail if:
        # - currently on an empty (or other player) tile
        # - walking on a captured tile and have non captured tiles in the tail
        if not self.game_map.is_conquered_by(
                player.position,
                player.id) or not self.game_map.is_conquered_by(
                    player.tail[-1], player.id):
            self.logger.debug(
                f"Player '{player.name_str()}' appending position to tail.")
            player.tail.append(player.position.copy())
        else:
            player.tail = [player.position.copy()]
Exemple #5
0
    def test_surround_player_with_conquers(self):
        # fmt: off
        gm = create_map_with([
            [1, 1, 1, E, E],
            [1, E, 1, E, E],
            [E, E, E, E, E],
            [E, E, E, 2, 2],
            [E, E, E, 2, 2],
        ])
        # fmt: on
        p1 = PlayerState(id=1,
                         name="p1",
                         game_map=gm,
                         position=Position(1, 3),
                         direction=Direction(Direction.RIGHT))
        p2 = PlayerState(id=2,
                         name="p2",
                         game_map=gm,
                         position=Position(4, 4),
                         direction=Direction(Direction.LEFT))
        gs = GameState(gm, [p1, p2])

        gs.apply_action(0, p2, Action.FORWARD)
        gs.apply_action(0, p2, Action.FORWARD)
        gs.apply_action(0, p2, Action.TURN_RIGHT)
        gs.apply_action(0, p2, Action.FORWARD)
        self.assertEqual(Position(2, 2), p2.position)
        self.assertEqual(5, len(p2.tail))

        gs.apply_action(0, p1, Action.FORWARD)  # walk on tail, p2 killed
        self.assertTrue(p2.killed)
        self.assertFalse(p1.killed)
        self.assertEqual(0, p2.stats.stats[PlayerStats.CONQUERED])
Exemple #6
0
    def apply_action(self, game_tick: int, player: PlayerState,
                     action: Optional[Action]) -> None:
        self.game_tick = game_tick
        if player.killed:
            msg = f"Player '{player.name_str()}' was killed in the last turn, skip current action."
            self.logger.info(msg)
            player.add_history(game_tick, msg)

            # Respawn a player after killed
            self.respawn_player(player)
        else:
            # Change direction and move
            self.move_player(player, action)

            # If you move outside the map or an asteroids; you die.
            if self.game_map.is_out_of_bound(
                    player.position) or self.game_map.is_asteroids(
                        player.position):
                self.logger.info(
                    f"Player '{player.name_str()}' stepped on an asteroids or outside the map."
                )
                player.stats.add_stat(PlayerStats.SUICIDES)
                player.add_history(
                    game_tick, f"Committed suicide by going out of bound.")
                self.kill_player(player)
            elif not self.stepped_on_a_black_hole(player.position, player):
                # Are we about to kill a player ?
                poor_player = self.will_kill_player(player)
                if poor_player != None:
                    self.kill_player_by(poor_player, player)

                if not player.killed:
                    # check if player captured a blitzium
                    self.check_if_captured_a_blitzium(player.position, player)

                    # Check if player conquered a territory
                    if GameState.is_closed_tail(player, self.game_map):
                        tilesOwnedBeforeConquere = dict()
                        for p in self.players:
                            tilesOwnedBeforeConquere[
                                p.id] = self.game_map.count_tiles_owned_by(
                                    p.id)

                        new_conquers: Set[Position] = self.fill(player)

                        for p in self.players:
                            # check if we conquered current position of a player
                            # if p.id != player.id and (p.position in new_conquers or p.tail[0] in new_conquers):
                            if p.id != player.id and len(
                                    set(p.tail) & new_conquers) > 0:
                                self.kill_player_by(p, player)
                            # check if we conquered the last owned tiles of a player
                            if (p.id != player.id
                                    and tilesOwnedBeforeConquere[p.id] != 0
                                    and self.game_map.count_tiles_owned_by(
                                        p.id) == 0):
                                self.kill_player_by(p, player)
Exemple #7
0
 def check_if_captured_a_blitzium(self, pos: Position,
                                  player: PlayerState) -> None:
     if self.game_map.is_blitzium(pos):
         self.logger.info(f"Player '{player.name_str()}' found a blitzium.")
         player.stats.add_stat(PlayerStats.BLITZIUMS)
         player.add_history(self.game_tick, "Found a blitzium.")
         player.score += GameState.SCORE_CAPTURED_BLITZIUM
         if GameState.relocate_blitzium:
             self.relocate_item(pos)
         else:
             self.game_map.clear_tile(pos)
Exemple #8
0
    def respawn_player(self, player: PlayerState) -> None:
        # discard current action
        msg = f"Respawning player '{player.name_str()}'."
        self.logger.info(msg)
        player.add_history(self.game_tick, msg)
        player.killed = False
        player.reset_position()

        # check if it kill another player
        poor_player = self.will_kill_player(player)
        if poor_player is not None:
            self.kill_player_by(poor_player, player)
    def test_reset_position(self):
        game_map = GameMap(5)
        pos = Position(1, 1)
        ps = PlayerState(id=1,
                         name="dummy",
                         game_map=game_map,
                         position=pos,
                         direction=Direction(Direction.DOWN))
        ps.direction = Direction(Direction.LEFT)
        ps.reset_position()

        self.assertEqual(ps.direction, Direction(Direction.DOWN))
Exemple #10
0
 def test_move_player(self):
     gm = GameMap(5)
     player = PlayerState(id=1,
                          name="1",
                          game_map=gm,
                          position=Position(1, 1))
     player.direction = Direction(Direction.RIGHT)
     gs = GameState(gm, [player])
     gs.move_player(player, Action.FORWARD)
     gs.move_player(player, Action.TURN_RIGHT)
     gs.move_player(player, Action.TURN_RIGHT)
     gs.move_player(player, Action.TURN_LEFT)
     self.assertEqual(Position(1, 3), player.position)
Exemple #11
0
 def stepped_on_a_black_hole(self, pos: Position,
                             player: PlayerState) -> bool:
     if self.game_map.is_black_hole(pos):
         self.logger.info(
             f"Player '{player.name_str()}' stepped on a black hole.")
         player.stats.add_stat(PlayerStats.SUICIDES)
         player.add_history(
             self.game_tick,
             f"Committed suicide by stepping on a black hole.")
         self.kill_player(player)
         if GameState.relocate_black_hole:
             self.relocate_item(pos)
         # permanent black_hole else: self.game_map.clear_tile(pos)
     return player.killed
Exemple #12
0
    def test_kill_player(self):
        # fmt: off
        gm = create_map_with([[E, E, E], [E, E, E], [1, 1, 1]])
        # fmt: on
        p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1))
        gs = GameState(gm, [p1])

        p1.tail = [Position(1, 1), Position(1, 2)]
        p1.position = p1.tail[-1]
        gs.kill_player(p1)

        self.assertEqual(p1.tail, [p1.spawn_position])
        self.assertEqual(p1.position, p1.spawn_position)
        self.assertTrue(p1.killed)
        self.assertEqual(0, p1.stats.stats[PlayerStats.CONQUERED])
Exemple #13
0
    def test_walk_on_tail_suicide(self):
        gm = GameMap(5)
        p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1))
        self.assertEqual(p1.direction, Direction.RIGHT)
        gs = GameState(gm, players=[p1])

        # loop around
        gs.apply_action(1, p1, Action.FORWARD)
        gs.apply_action(2, p1, Action.FORWARD)
        gs.apply_action(3, p1, Action.TURN_RIGHT)
        gs.apply_action(4, p1, Action.TURN_RIGHT)
        self.assertEqual(p1.position, Position(2, 2))
        self.assertEqual(p1.tail, [
            p1.spawn_position,
            Position(2, 1),
            Position(3, 1),
            Position(3, 2),
            Position(2, 2)
        ])
        self.assertFalse(p1.killed)

        # walk on tail = suicide
        gs.apply_action(5, p1, Action.TURN_RIGHT)
        self.assertTrue(p1.killed)
        self.assertEqual(len(p1.tail), 1)
        self.assertEqual(1, p1.stats.stats[PlayerStats.SUICIDES])
Exemple #14
0
    def test_fill_basic_2(self):
        # fmt: off
        game_map = create_map_with([[E, 1, 1, E, E], [E, 1, 1, E, E],
                                    [E, E, E, E, E], [E, E, E, E, E],
                                    [E, E, E, E, E]])
        # fmt: on
        ps = PlayerState(id=1,
                         name="dummy",
                         game_map=game_map,
                         position=Position(2, 2),
                         direction=Direction(Direction.DOWN))
        gs = GameState(game_map, [ps])
        gs.apply_action(0, ps, Action.FORWARD)
        gs.apply_action(1, ps, Action.TURN_RIGHT)
        gs.apply_action(2, ps, Action.TURN_LEFT)
        gs.apply_action(3, ps, Action.TURN_LEFT)
        gs.apply_action(4, ps, Action.FORWARD)
        gs.apply_action(4, ps, Action.FORWARD)
        gs.apply_action(5, ps, Action.TURN_LEFT)
        gs.apply_action(6, ps, Action.FORWARD)
        gs.apply_action(7, ps, Action.TURN_LEFT)

        # fmt: off
        self.assertEqual(
            game_map,
            create_map_with([[E, 1, 1, E, E], [E, 1, 1, 1, E], [1, 1, 1, 1, E],
                             [1, 1, 1, 1, E], [E, E, E, E, E]]))
        # fmt: on
        self.assertEqual(ps.score, 9)
Exemple #15
0
    def test_is_closed_tail(self):
        # fmt: off
        gm = create_map_with([[1, E, 1], [E, E, E], [1, E, E]])
        # fmt: on

        # player without tail
        player = PlayerState(id=1,
                             name="1",
                             game_map=gm,
                             position=Position(1, 1))
        self.assertEqual(player.tail, [Position(1, 1)])
        self.assertFalse(GameState.is_closed_tail(player, gm))

        # player with tail
        player.tail = [Position(1, 1), Position(2, 1)]
        self.assertFalse(GameState.is_closed_tail(player, gm))

        player.tail = [Position(1, 1), Position(2, 1), Position(3, 1)]
        player.direction = Direction(Direction.UP)
        self.assertTrue(GameState.is_closed_tail(player, gm))

        player.tail = [Position(1, 1), Position(2, 1), Position(2, 2)]
        self.assertFalse(GameState.is_closed_tail(player, gm))

        player.tail = [
            Position(1, 1),
            Position(2, 1),
            Position(2, 2),
            Position(1, 2),
            Position(1, 1)
        ]
        self.assertTrue(GameState.is_closed_tail(player, gm))
Exemple #16
0
    def test_relocate_item(self):
        # fmt: off
        gm = create_map_with([[1, E, E], [E, W, E], [E, E, E]])
        # fmt: on
        ps = PlayerState(id=1,
                         name="dummy",
                         game_map=gm,
                         position=Position(1, 1))
        gs = GameState(gm, [ps])
        ps.position = Position(3, 1)
        ps.tail = [Position(2, 1), Position(3, 1)]

        asteroids_pos = Position(2, 2)
        self.assertTrue(gm.is_asteroids(asteroids_pos))

        planet_pos = Position(3, 3)
        gm.set_tile(planet_pos, GameMap.PLANET)
        self.assertTrue(gm.is_planet(planet_pos))

        black_hole_pos = Position(1, 2)
        gm.set_tile(black_hole_pos, GameMap.BLACK_HOLE)
        self.assertTrue(gm.is_black_hole(black_hole_pos))

        blitzium_pos = Position(1, 3)
        gm.set_tile(blitzium_pos, GameMap.BLITZIUM)
        self.assertTrue(gm.is_blitzium(blitzium_pos))

        # Cannot relocate asteroids and planets
        with self.assertRaises(Exception):
            gs.relocate_item(asteroids_pos)
        with self.assertRaises(Exception):
            gs.relocate_item(planet_pos)

        # relocate black holes and blitziums
        for i in range(30):
            new_black_hole_pos = gs.relocate_item(black_hole_pos)
            self.assertTrue(new_black_hole_pos not in list(
                {asteroids_pos, planet_pos, black_hole_pos, blitzium_pos}) +
                            ps.tail)
            black_hole_pos = new_black_hole_pos

        new_blitzium_pos = gs.relocate_item(blitzium_pos)
        self.assertTrue(new_blitzium_pos != blitzium_pos)
Exemple #17
0
 def test_fill_with_asteroids(self):
     # fmt: off
     game_map = create_map_with([[E, E, E, E, E], [E, 1, E, 1, E],
                                 [E, 1, W, 1, E], [E, 1, 1, 1, E],
                                 [E, E, E, E, E]])
     # fmt: on
     ps = PlayerState(id=1,
                      name="dummy",
                      game_map=game_map,
                      position=Position(2, 2))
     ps.tail = [ps.spawn_position, Position(3, 2), Position(4, 2)]
     gs = GameState(game_map, [ps])
     self.assertEqual(len(gs.fill(ps)), 1)
     # fmt: off
     self.assertEqual(
         game_map,
         create_map_with([[E, E, E, E, E], [E, 1, 1, 1, E], [E, 1, W, 1, E],
                          [E, 1, 1, 1, E], [E, E, E, E, E]]))
     # fmt: on
     self.assertEqual(ps.score, 1)
Exemple #18
0
    def kill_player_by(self, poor_player: PlayerState,
                       player: PlayerState) -> None:
        if poor_player.id != player.id:
            self.logger.info(
                f"Player '{player.name_str()}' will kill player '{poor_player.name_str()}'."
            )
            player.score += GameState.SCORE_KILL_PLAYER
            player.stats.kill_player(poor_player.name)
            poor_player.stats.killed_by_player(player.name)
            player.add_history(self.game_tick,
                               f"Killed player '{poor_player.name}'")
        else:
            self.logger.info(
                f"Player '{player.name_str()}' committed suicide!.")
            player.stats.add_stat(PlayerStats.SUICIDES)
            player.add_history(
                self.game_tick,
                f"Committed suicide by walking on player tail.")

        self.kill_player(poor_player)
Exemple #19
0
 def test_surround_player_will_it(self):
     gm = GameMap(6)
     p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1))
     p2 = PlayerState(id=2, name="2", game_map=gm, position=Position(2, 2))
     p3 = PlayerState(id=3, name="3", game_map=gm, position=Position(3, 3))
     p4 = PlayerState(id=4, name="3", game_map=gm, position=Position(4, 4))
     gs = GameState(gm, [p1, p2, p3, p4])
     gs.apply_action(0, p1, Action.FORWARD)
     gs.apply_action(0, p1, Action.FORWARD)
     gs.apply_action(0, p1, Action.TURN_RIGHT)
     gs.apply_action(0, p1, Action.FORWARD)
     gs.apply_action(0, p1, Action.TURN_RIGHT)
     gs.apply_action(0, p1, Action.FORWARD)
     gs.apply_action(0, p1, Action.TURN_RIGHT)
     gs.apply_action(0, p1, Action.FORWARD)
     self.assertEqual(9, p1.stats.stats[PlayerStats.CONQUERED])
     self.assertFalse(p1.killed)
     self.assertTrue(p2.killed)
     self.assertTrue(p3.killed)
     self.assertFalse(p4.killed)
    def test_init(self):
        game_map = GameMap(5)
        pos = Position(1, 1)
        ps = PlayerState(id=1, name="dummy", game_map=game_map, position=pos)

        self.assertFalse(ps.killed)
        self.assertEqual(ps.score, 0)
        self.assertEqual(ps.spawn_position, pos)
        self.assertEqual(ps.position, pos)
        self.assertEqual(ps.tail, [ps.spawn_position])
        self.assertEqual(ps.direction, Direction.RIGHT)
        self.assertTrue(ps.game_map.is_conquered_by(pos, 1))
Exemple #21
0
    def test_surround_player(self):
        # fmt: off
        gm = create_map_with([
            [1, 1, 1],
            [1, E, 1],
            [E, E, E],
        ])
        # fmt: on
        p1 = PlayerState(id=1,
                         name="p1",
                         game_map=gm,
                         position=Position(1, 3),
                         direction=Direction(Direction.RIGHT))
        p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(2, 2))
        gs = GameState(gm, [p1, p2])

        gs.apply_action(0, p1, Action.FORWARD)
        gs.apply_action(0, p1, Action.FORWARD)
        gs.apply_action(0, p1, Action.TURN_LEFT)

        self.assertTrue(p2.killed)
        self.assertFalse(p1.killed)
Exemple #22
0
 def test_fill_kill_other_player_tail(self):
     # fmt: off
     game_map = create_map_with([[E, E, E, E], [E, E, E, E], [0, 0, 0, 0],
                                 [E, 1, E, E]])
     # fmt: on
     p0 = PlayerState(id=0,
                      name="dummy",
                      game_map=game_map,
                      position=Position(1, 3))
     p1 = PlayerState(id=1,
                      name="dummy",
                      game_map=game_map,
                      position=Position(2, 4))
     gs = GameState(game_map, [p0, p1])
     p0.tail = [
         p0.spawn_position.copy(),
         Position(1, 2),
         Position(1, 1),
         Position(2, 1),
         Position(3, 1),
         Position(4, 1),
         Position(4, 2),
     ]
     p0.position = p0.tail[-1].copy()
     p0.direction = Direction(Direction.DOWN)
     p1.tail = [
         p1.spawn_position.copy(),
         Position(2, 3),
         Position(2, 2),
         Position(3, 2),
         Position(3, 3),
         Position(3, 4),
     ]
     p1.position = p1.tail[-1].copy()
     p1.direction = Direction(Direction.DOWN)
     # print(game_state_to_dict(0, 0, gs, 999)['game']['pretty_map'])
     gs.apply_action(0, p0, Action.FORWARD)
     # print(game_state_to_dict(0, 0, gs, 999)['game']['pretty_map'])
     self.assertTrue(p1.killed)
Exemple #23
0
    def test_send_winner_socket_closed(self):
        server = Mock()
        socket = AsyncMock()
        socket.send.side_effect = websockets.ConnectionClosed(0, "")
        viewer = SocketViewer(server, socket)
        viewer.logger = Mock()

        gs = GameState(GameMap(3))
        ps = PlayerState(1, "p1", gs.game_map, Position(1, 1))
        asyncio.run(viewer.send_winner(123, ps))

        viewer.logger.warning.assert_called_once()
        server.game.unregister_viewer.assert_called_once()
Exemple #24
0
    def test_send_winner(self):
        server = Mock()
        server.game.max_nb_ticks = 500
        socket = AsyncMock()
        viewer = SocketViewer(server, socket)

        gs = GameState(GameMap(3))
        ps = PlayerState(1, "p1", gs.game_map, Position(1, 1))
        asyncio.run(viewer.send_winner(123, ps))

        socket.send.assert_called_once()
        data = json.loads(socket.send.call_args[0][0])

        self.assertEqual(data["type"], "winner")
        self.assertEqual(data["tick"], 123)
        self.assertEqual(data["winner"]["name"], "p1")
Exemple #25
0
 def test_capturing_a_black_hole_suicide(self):
     # fmt: off
     gm = create_map_with([[0, 0, 0], [E, E, 0], [0, 0, 0]])
     # fmt: on
     gm.set_tile(Position(2, 2), GameMap.BLACK_HOLE)
     p0 = PlayerState(id=0,
                      name="p0",
                      game_map=gm,
                      position=Position(1, 1),
                      direction=Direction(Direction.DOWN))
     gs = GameState(gm, [p0])
     gs.apply_action(0, p0, Action.FORWARD)
     self.assertFalse(p0.killed)
     gs.apply_action(0, p0, Action.FORWARD)
     self.assertTrue(p0.killed)
     self.assertEqual(1, p0.stats.stats[PlayerStats.SUICIDES])
Exemple #26
0
 def test_capturing_a_blitzium(self):
     # fmt: off
     gm = create_map_with([[0, 0, 0], [E, E, 0], [0, 0, 0]])
     # fmt: on
     gm.set_tile(Position(2, 2), GameMap.BLITZIUM)
     p0 = PlayerState(id=0,
                      name="p0",
                      game_map=gm,
                      position=Position(1, 1),
                      direction=Direction(Direction.DOWN))
     gs = GameState(gm, [p0])
     gs.apply_action(0, p0, Action.FORWARD)
     self.assertFalse(PlayerStats.BLITZIUMS in p0.stats.stats)
     gs.apply_action(0, p0, Action.FORWARD)
     self.assertFalse(p0.killed)
     self.assertEqual(1, p0.stats.stats[PlayerStats.BLITZIUMS])
Exemple #27
0
    def test_respawn_on_someone_tail(self):
        gm = GameMap(5)
        p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 1))
        p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(1, 2))
        gs = GameState(gm, [p1, p2])

        # p1: spawn position = 1,1, current position=3,1, tail [2,1 and 3,1]
        # p2: spawn position = 1,2, current position=2,2, tail [1,1, 1,2 and 2,2]
        p1.position = Position(3, 1)
        p1.tail = [Position(2, 1), Position(3, 1)]
        gm.set_tile(Position(1, 1), GameMap.EMPTY, 2)
        p2.position = Position(2, 2)
        p2.tail = [Position(1, 1), Position(1, 2), Position(2, 2)]
        p2.direction = Direction(Direction.UP)
        gs.apply_action(0, p2, Action.FORWARD)

        self.assertTrue(p1.killed)
        self.assertFalse(p2.killed)

        # respawn
        gs.respawn_player(p1)
        self.assertFalse(p1.killed)
        self.assertTrue(p2.killed)
Exemple #28
0
    def test_conquer_some_player_tile(self):
        # fmt: off
        gm = create_map_with([
            [1, 1, E],
            [1, 2, 2],
            [E, 2, 2],
        ])
        # fmt: on
        p1 = PlayerState(id=1, name="p1", game_map=gm, position=Position(1, 1))
        p2 = PlayerState(id=2, name="p2", game_map=gm, position=Position(2, 2))
        gs = GameState(gm, [p1, p2])

        p1.position = Position(2, 2)
        p1.tail = [Position(1, 2), Position(2, 2)]

        p2.position = Position(2, 3)
        p2.tail = [Position(2, 3)]

        p1.direction = Direction(Direction.RIGHT)
        gs.apply_action(0, p1, Action.TURN_LEFT)

        self.assertFalse(p2.killed)
        self.assertFalse(p1.killed)
Exemple #29
0
    def test_walk_on_blitzium(self):
        gm = GameMap(5)
        p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1))
        self.assertEqual(p1.direction, Direction.RIGHT)
        gs = GameState(gm, players=[p1])
        gm.set_tile(Position(2, 1), GameMap.BLITZIUM)

        gs.apply_action(1, p1, Action.FORWARD)
        self.assertFalse(p1.killed)
        self.assertEqual(p1.position, Position(2, 1))
        self.assertEqual(len(p1.tail), 2)
        self.assertEqual(1, p1.stats.stats[PlayerStats.BLITZIUMS])
        self.assertFalse(gm.is_blitzium(Position(2, 1)))

        if not GameState.relocate_blitzium:
            for y in range(gm.size):
                for x in range(gm.size):
                    self.assertFalse(gm.is_blitzium(Position(x, y)))
Exemple #30
0
    def test_walk_on_asteroids_suicide(self):
        gm = GameMap(5)
        p1 = PlayerState(id=1, name="1", game_map=gm, position=Position(1, 1))
        self.assertEqual(p1.direction, Direction.RIGHT)
        gs = GameState(gm, players=[p1])

        # validate that a turn left should walk on asteroids
        pos = p1.position.copy().move(
            Direction(Direction.RIGHT).change_direction(Action.TURN_LEFT))
        self.assertEqual(pos, Position(1, 0))
        self.assertTrue(gm.is_asteroids(pos))
        gs.apply_action(1, p1, Action.TURN_LEFT)

        # walk on tail = suicide, respawn
        self.assertEqual(p1.position, Position(1, 1))
        self.assertEqual(len(p1.tail), 1)
        self.assertTrue(p1.killed)
        self.assertEqual(1, p1.stats.stats[PlayerStats.SUICIDES])