def alert_players(self, game_result):
     """
     Send the GameResult to the active players in the game (non-kicked players).
     """
     if type(self.__game) is GameEnded:
         for player in self.get_players():
             safe_call(self.__timeout, player.game_over, [game_result])
 def __broadcast_current_state(self):
     """
     Update players with new state. Called when a player has been kicked.
     """
     for player in self.get_players():
         safe_call(self.__timeout, player.set_state,
                   [self.__game.get_state()])
    def __assign_color_to_player(self, index, ext_player):
        """
        Assigns color to player based on index. Returns color assigned.

        :index: int		Index of color
        :ext_player: PlayerInterface	Player to assign color to
        :returns: Maybe Color		Color assigned, False if color assignment call failed
        """
        colors = Color.get_all_colors()
        assign_color = colors[index]
        if safe_call(self.__timeout, ext_player.assign_color,
                     [assign_color]) is False:
            return False
        else:
            return assign_color
Ejemplo n.º 4
0
    def __broadcast_tournament_start(self):
        """
        Notifies all active players that the tournament is about to start. If the player fails to
        respond then they will be kicked.
        EFFECT: calls the tournament_start method on all active PlayerInterfaces in this tournament
                Players who fail to respond will be removed from the active_players and the queue
        void -> void
        """
        bad_players = set()
        for player in self.__active_players:
            response = safe_call(self.__timeout, player.tournament_start)
            if response is not True:
                bad_players.add(player)

        self.__kick_players(bad_players)
 def run_game(self):
     """
     Run the game until the end.
     """
     while not self.has_game_ended():
         color = self.__game.get_current_player_color()
         current_player = self.get_player_with_color(color)
         maybe_action = safe_call(self.__timeout,
                                  current_player.choose_next_move)
         if maybe_action is not False:
             maybe_game_tree = self.check_move_validity(maybe_action)
         if maybe_action is False or maybe_game_tree is False:
             self.__kick_player(color)
             self.__broadcast_current_state()
         else:
             self.__game = maybe_game_tree
             self.__broadcast_player_action(maybe_action)
Ejemplo n.º 6
0
    def __broadcast_tournament_end(self):
        """
        Notifies all active players (those who have not failed or cheated) whether they
        have won or lost the game. Winners who fail to respond to the message become
        losers.

        EFFECT: calls the tournament_end method on all active PlayerInterfaces in this tournament
                Players who fail to respond will be removed from the active_players and the queue
        void -> void
        """
        bad_players = set()
        for player in self.__active_players:
            did_win = player in self.__queue
            response = safe_call(self.__timeout, player.tournament_end,
                                 [did_win])
            if response is not True:
                bad_players.add(player)

        self.__kick_players(bad_players)
    def _Referee__assign_player_colors(self, players):
        """
        assigns the player colors to all players in this game. it also informs
        players about the other colors that are playing in this game
        Effect: calls the super().__assign_player_colors method, and then calls
            player.play_with() for all players in the game with the colors of their opponents

        [List-of PlayerInterface] -> Void
        """
        internal_players = super()._Referee__assign_player_colors(players)
        for player_data in copy(internal_players):
            colors_in_play = super().get_players_as_colors()
            color = player_data.get_color()
            colors_in_play.remove(color)
            if safe_call(self._Referee__timeout,
                         self.get_player_with_color(color).play_with,
                         [colors_in_play]) is False:
                print("with kick", color)
                self._Referee__kick_player(color)
                internal_players.remove(player_data)

        return internal_players
    def __run_penguin_placement(self):
        """
        Run as many penguin placement rounds as necessary.
        Call on Players to place their penguins.

        EFFECT: Updates the current GameTree when penguins are placed

        void -> void
        """
        rounds = get_max_penguin_count(len(self.__players))
        for round in range(0, rounds):
            for color in self.get_players_as_colors():
                state = self.__game.get_state()
                player = self.get_player_with_color(color)
                maybe_posn = safe_call(self.__timeout, player.choose_placement,
                                       [state])
                if maybe_posn is not False:
                    maybe_state = state.place_penguin_for_player(
                        color, maybe_posn)
                if maybe_posn is False or maybe_state is False:
                    self.__kick_player(color)
                else:
                    self.__game = GameTree(maybe_state)
 def __broadcast_player_action(self, action):
     """
     Update players who have not been kicked on ongoing game actions.
     """
     for player in self.get_players():
         safe_call(self.__timeout, player.update_with_action, [action])