Ejemplo n.º 1
0
    def check_loss(self, player, players):
        is_loss = False

        if player.y < 0 + round(WIDTH / 2):
            is_loss = True

        if player.y > WINDOW_HEIGHT - round(WIDTH / 2):
            is_loss = True

        if player.x < 0 + round(WIDTH / 2):
            is_loss = True

        if player.x > WINDOW_WIDTH - round(WIDTH / 2):
            is_loss = True

        for p in players:
            if (p.x, p.y) in player.lines:
                if p != player:
                    p.score += LINE_KILL_SCORE
                is_loss = True

        for p in players:
            if is_intersect((player.x, player.y), (p.x, p.y)) and p != player:
                if len(player.lines) >= len(p.lines):
                    is_loss = True

        if len(player.territory.points) == 0:
            is_loss = True

        return is_loss
Ejemplo n.º 2
0
 def use_saw(self, player):
     line = player.get_direction_line()
     Saw.append_line(line)
     for p in self.players:
         if p != player:
             if any([is_intersect(p.pos(), point) for point in line]):
                 self.losers.append(p)
                 self.append_event('killed by saw', p, player)
                 Saw.log.append({
                     'player': player.id,
                     'loser': p.id,
                     'killed': True
                 })
                 player.tick_score += SAW_KILL_SCORE
             else:
                 removed = p.territory.split(line, player.direction, p)
                 if len(removed) > 0:
                     player.tick_score += SAW_SCORE
                     Saw.append_territory(removed, p.territory.color)
                     Saw.log.append({
                         'player': player.id,
                         'loser': p.id,
                         'points': removed,
                         'killed': False
                     })
Ejemplo n.º 3
0
    def check_loss_head_intersection(self, player, players):
        for p in players:
            if p != player:

                if len(player.lines) > 0:
                    if is_intersect(player.pos(), p.pos()):
                        if len(player.lines) >= len(p.lines):
                            self.append_event('faced with other player (lines > 0)', player, p)
                            return True
                elif len(p.lines) == 0:
                    # both on self'territory (0 line) ...
                
                    # both not in center (otherwise forward and backward will be invalid)
                    if not player.is_in_cell_center() and not p.is_in_cell_center():
                    
                        # 1st move to 2nd, 2nd move to 1st
                        # start move on the same tick
                        # both will be killed (1st - now, 2nd - later)
                        if player.get_forward_center_position() == p.get_backward_center_position() and \
                                player.get_backward_center_position() == p.get_forward_center_position():
                            self.append_event('faced with other player ([move] 1 <-> 2 [move])', player, p)
                            return True
                        # otherwise both will survive (includes case when 1st -> move to 2nd, and 2nd -> move to another point, and vise versa)
                        
                    # 1st move to 2nd, 2nd stay in center
                    # 1st will be killed now, 2nd will survive (will no be killed - later)
                    elif not player.is_in_cell_center() and player.get_forward_center_position() == p.pos():
                        self.append_event('faced with other player ([move] 1 -> 2 [stop])', player, p)
                        return True
                        
                    # otherwise in safe on self'territory (0 line)
        return False
Ejemplo n.º 4
0
    def check_loss(self, player, players):
        is_loss = False

        if player.y < 0 + round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        if player.y > WINDOW_HEIGHT - round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        if player.x < 0 + round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        if player.x > WINDOW_WIDTH - round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        for p in players:
            if (p.x, p.y) in player.lines[:-1]:
                if p != player:
                    p.tick_score += LINE_KILL_SCORE
                is_loss = True
                self.append_event('line crossed by other player', player, p)

        if len(player.lines) > 0:
            for p in players:
                if is_intersect((player.x, player.y), (p.x, p.y)) and p != player:
                    if len(player.lines) >= len(p.lines):
                        is_loss = True
                        self.append_event('faced with other player', player, p)
                        break
        else:
            zero_tail_opps = [opp for opp in players if opp != player and len(opp.lines) == 0]
            for p in zero_tail_opps:
                if is_intersect((player.x, player.y), (p.x, p.y)) and player.direction == OPPOSITE_DIRECTION[p.direction]:
                    is_loss = True
                    self.append_event('faced with other player', player, p)
                    break

        if len(player.territory.points) == 0:
            is_loss = True
            self.append_event('has no territory', player)

        return is_loss
Ejemplo n.º 5
0
    def check_loss(self, player, players):
        # TODO ну ты тут полежи, потом в оценочную добавлю
        is_loss = False

        for p in players:
            if is_intersect((player.x, player.y), (p.x, p.y)) and p != player:
                if len(player.lines) >= len(p.lines):
                    is_loss = True

        if len(player.territory.points) == 0:
            is_loss = True

        return is_loss
Ejemplo n.º 6
0
    def check_loss(self, player, players):
        is_loss = False

        if player.y < 0 + round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        if player.y > WINDOW_HEIGHT - round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        if player.x < 0 + round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        if player.x > WINDOW_WIDTH - round(WIDTH / 2):
            is_loss = True
            self.append_event('faced the border', player)

        for p in players:
            if (p.x, p.y) in player.lines[:-1]:
                if p != player:
                    p.tick_score += LINE_KILL_SCORE
                is_loss = True
                self.append_event('line crossed by other player', player, p)

        if len(player.lines) > 0:
            for p in players:
                if is_intersect((player.x, player.y),
                                (p.x, p.y)) and p != player:
                    if len(player.lines) >= len(p.lines):
                        is_loss = True
                        self.append_event('faced with other player', player, p)
                        break

        if len(player.territory.points) == 0:
            is_loss = True
            self.append_event('has no territory', player)

        return is_loss
Ejemplo n.º 7
0
    async def game_loop(self, *args, **kwargs):
        self.send_game_tick()

        for player in self.players:
            if (player.x - round(WIDTH / 2)) % WIDTH == 0 and (player.y - round(WIDTH / 2)) % WIDTH == 0:
                command = await player.get_command(self.tick)
                if command:
                    player.change_direction(command)

        for player in self.players:
            player.move()

        for index, player in enumerate(self.players):
            is_loss = self.check_loss(player, self.players)
            if is_loss:
                self.losers.append(self.players[index])

        for player in self.players:
            player.remove_saw_bonus()

            if (player.x - round(WIDTH / 2)) % WIDTH == 0 and (player.y - round(WIDTH / 2)) % WIDTH == 0:
                player.update_lines()

                captured = player.territory.capture(player.lines)
                if len(captured) > 0:
                    player.lines.clear()
                    player.score += NEUTRAL_TERRITORY_SCORE * len(captured)

                player.tick_action()

                for bonus in self.bonuses[:]:
                    if bonus.is_ate(player, captured):
                        bonus.apply(player)
                        self.bonuses.remove(bonus)

                        if isinstance(bonus, Saw):
                            line = player.get_direction_line()
                            Saw.append_line(line)
                            for p in self.players:
                                if p != player:
                                    if any([is_intersect((p.x, p.y), point) for point in line]):
                                        self.losers.append(p)
                                        Saw.log.append({
                                            'player': player.id,
                                            'loser': p.id,
                                            'killed': True
                                        })
                                        player.score += SAW_KILL_SCORE
                                    else:
                                        removed = p.territory.split(line, player.direction, p)
                                        if len(removed) > 0:
                                            player.score += SAW_SCORE
                                            Saw.append_territory(removed, p.territory.color)
                                            Saw.log.append({
                                                'player': player.id,
                                                'loser': p.id,
                                                'points': removed,
                                                'killed': False
                                            })
                for p in self.players:
                    if p != player:
                        removed = p.territory.remove_points(captured)
                        player.score += (ENEMY_TERRITORY_SCORE - NEUTRAL_TERRITORY_SCORE) * len(removed)

        for player in self.losers:
            if player in self.players:
                self.players.remove(player)

        self.generate_bonus()

        self.tick += 1
        return len(self.players) == 0
Ejemplo n.º 8
0
    async def game_loop(self, *args, **kwargs):
        self.send_game_tick()

        futures = []
        for player in self.players:
            if (player.x - round(WIDTH / 2)) % WIDTH == 0 and (
                    player.y - round(WIDTH / 2)) % WIDTH == 0:
                futures.append(
                    asyncio.ensure_future(self.get_command_wrapper(player)))
        if futures:
            await asyncio.wait(futures)

        for player in self.players:
            player.move()

        players_to_captured = {}
        for player in self.players:
            player.remove_saw_bonus()

            if (player.x - round(WIDTH / 2)) % WIDTH == 0 and (
                    player.y - round(WIDTH / 2)) % WIDTH == 0:
                player.update_lines()

                captured = player.territory.capture(player.lines)
                players_to_captured[player] = captured
                if len(captured) > 0:
                    player.lines.clear()
                    player.tick_score += NEUTRAL_TERRITORY_SCORE * len(
                        captured)

        for player in self.players:
            is_loss = self.check_loss(player, self.players)
            if is_loss:
                self.losers.append(player)

        players_to_captured = self.collision_resolution(players_to_captured)

        for player in self.players:
            is_loss, p = player.is_ate(players_to_captured)
            if is_loss:
                self.append_event('eaten by other player', player, p)
                self.losers.append(player)

        for player in self.players:
            if (player.x - round(WIDTH / 2)) % WIDTH == 0 and (
                    player.y - round(WIDTH / 2)) % WIDTH == 0:
                captured = players_to_captured.get(player, set())

                player.tick_action()

                for bonus in self.bonuses[:]:
                    if bonus.is_ate(player, captured):
                        bonus.apply(player)
                        self.bonuses.remove(bonus)

                        if isinstance(bonus, Saw):
                            line = player.get_direction_line()
                            Saw.append_line(line)
                            for p in self.players:
                                if p != player:
                                    if any([
                                            is_intersect((p.x, p.y), point)
                                            for point in line
                                    ]):
                                        self.losers.append(p)
                                        self.append_event(
                                            'killed by saw', p, player)
                                        Saw.log.append({
                                            'player': player.id,
                                            'loser': p.id,
                                            'killed': True
                                        })
                                        player.tick_score += SAW_KILL_SCORE
                                    else:
                                        removed = p.territory.split(
                                            line, player.direction, p)
                                        if len(removed) > 0:
                                            player.tick_score += SAW_SCORE
                                            Saw.append_territory(
                                                removed, p.territory.color)
                                            Saw.log.append({
                                                'player': player.id,
                                                'loser': p.id,
                                                'points': removed,
                                                'killed': False
                                            })
                if captured:
                    player.territory.points.update(captured)
                    for p in self.players:
                        if p != player:
                            removed = p.territory.remove_points(captured)
                            player.tick_score += (
                                ENEMY_TERRITORY_SCORE -
                                NEUTRAL_TERRITORY_SCORE) * len(removed)

        for player in self.losers:
            if player in self.players:
                self.players.remove(player)

        for player in self.players:
            player.score += player.tick_score
            player.tick_score = 0

        self.generate_bonus()

        self.tick += 1
        return len(self.players) == 0