Ejemplo n.º 1
0
def handle_show_statistics(albums_list):
    """ Handler for manu option to display satatistics regarding albums collection. """
    music_reports.default_view()
    print('Albums statistics:\n')
    print('Albums count is: {}'.format(len(albums_list)))
    longest_shortest_albums = music_reports.max_min_time(albums_list)
    display.show_table(longest_shortest_albums, 'Shortest and longest albums')
    youngest_oldest_albums = music_reports.get_young_old_album(albums_list)
    display.show_table(youngest_oldest_albums, 'Youngest and oldest albums')
    albums_count_by_given_genres = music_reports.get_albums_count_by_given_genres(
        albums_list)
    albums_count_by_given_genres_sorted = music_reports.sort_list_by_column(
        list(albums_count_by_given_genres.items()), 1)
    albums_count_by_given_genres_sorted.reverse()
    display.show_dict_in_table(albums_count_by_given_genres_sorted,
                               'Album counts in genres')
Ejemplo n.º 2
0
def handle_find_albums_by_title(albums_list):
    """
    Handler for menu option to search albums by user given album title and display it as table. Below search
    results handler displays table with albums with similar genre.
    """
    music_reports.default_view()
    chosen_album_title = music_reports.get_user_string_input(
        'Enter album title: ')
    albums_by_album_title = music_reports.find_albums_by_condition(
        albums_list, chosen_album_title, 1)
    music_reports.default_view()
    display.show_table(albums_by_album_title,
                       'Albums with title: ' + chosen_album_title)
    unique_set_with_propositions = music_reports.get_unique_propositions(
        albums_list, albums_by_album_title)
    display.show_table(unique_set_with_propositions,
                       'Similar albums chosen for you', 'yes')
Ejemplo n.º 3
0
 def showdown(self):
     """Run the showdown phase."""
     display.show_table(self.initial_players, self.table, 0)
     display.show_phase_change_alert('Showdown', self.pause)
     # Divvy chips to the winner(s) of each pot/side pot
     for i in reversed(range(len(self.table.pots))):
         showdown_players = []
         for player in self.table.pots[i][1]:
             if not player.isFolded:
                 showdown_players.append(player)
         hand_winners = handranker.determine_showdown_winner(
             showdown_players, self.table.community)
         for winner in hand_winners:
             winner.chips += int(self.table.pots[i][0] / len(hand_winners))
         display.show_showdown_results(self.initial_players,
                                       self.table,
                                       hand_winners,
                                       showdown_players,
                                       pot_num=i)
Ejemplo n.º 4
0
 def reset_hand(self):
     """Get deck, table, and players ready to play another hand"""
     if any(player.isHuman for player in self.active_players):
         self.pause = 2.75
         self.long_pause = 5.0
         self.short_pause = 0.75
     else:
         self.pause = 1.25
         self.long_pause = 3.0
         self.short_pause = 0.5
     for player in self.active_players:
         player.bet = 0
         player.hand = []
         player.isFolded = False
         player.isAllIn = False
         player.isFirstAct = False
         player.isLocked = False
         player.best_hand_cards = None
         player.best_hand_score = 0
         player.best_hand_rank = ''
         player.rank_subtype = ''
         player.kicker_card = None
     self.table.community = []
     self.table.pots = [[0, self.active_players]]
     self.table.last_bet = 0
     self.table.phase = None
     self.table.hands_played += 1
     # Double the big blind every 5 hands played to make game go faster
     if self.table.hands_played % 5 == 0:
         self.table.big_blind *= 2
         display.clear_screen()
         display.show_table(self.initial_players, self.table, 0)
         display.show_blind_increase(self.table.big_blind, self.long_pause)
     self.table.raise_amount = self.table.big_blind
     self.table.num_times_raised = 0
     self.assign_positions()
     # Reset deck
     self.deck.reset()
     self.deck.shuffle()
     display.clear_screen()
     display.show_shuffling(self.pause)
Ejemplo n.º 5
0
    def run_phase(self, phase_name):
        """Run the events of a phase.

        Args:
            phase_name (str): name of the phase
            num_deal (int): number of cards to deal to community
        """
        if phase_name == 'preflop':
            for player in self.active_players:
                if player.isDealer:
                    dealer_name = player.name
            display.show_table(self.initial_players, self.table, 0)
            display.show_phase_change_alert(phase_name, self.pause,
                                            dealer_name)
            self.deal_hole(dealer_name)
            num_deal = 0
        elif phase_name == 'flop':
            display.show_phase_change_alert(phase_name, self.pause)
            num_deal = 3
        else:
            display.show_phase_change_alert(phase_name, self.pause)
            num_deal = 1
        self.table.phase = phase_name
        self.table.num_times_raised = 0
        self.deck.deal(self.table.community, num_deal)
        display.show_table(self.initial_players, self.table, 0)
        self.round_of_bets()
        display.show_table(self.initial_players, self.table, 0)
Ejemplo n.º 6
0
    def check_game_over(self):
        """"Check if the game is over.

        If the game is not over (i.e. all but one player has no chips), ask the user if they would
        like to continue the game.

        Returns:
            bool: True if the game is over, False otherwise.
        """
        for player in self.active_players:
            if player.chips == 0:
                player.isInGame = False
        self.active_players = [
            player for player in self.active_players if player.isInGame
        ]
        if len(self.active_players) == 1:
            display.show_table(self.initial_players, self.table, 0)
            display.show_game_winners(self.initial_players,
                                      [self.active_players[0].name])
            return True
        else:
            while True:
                display.clear_screen()
                user_choice = input_no_return(
                    "Continue on to next hand? Press (enter) to continue or (n) to stop.   "
                )
                if 'n' in user_choice.lower():
                    max_chips = max(self.active_players,
                                    key=lambda player: player.chips).chips
                    winners_names = [
                        player.name for player in self.active_players
                        if player.chips == max_chips
                    ]
                    display.show_table(self.initial_players, self.table, 0)
                    display.show_game_winners(self.initial_players,
                                              winners_names)
                    return True
                return False
Ejemplo n.º 7
0
def handle_view_all_albums(albums_list):
    """ Handler for manu option to view all albums sorted in form of dynamic table. """
    music_reports.default_view()
    albums_list = music_reports.sort_list_by_column(albums_list, 0)
    display.show_table(albums_list, 'All albums in collection')
Ejemplo n.º 8
0
def handle_shortest_longest_albums(albums_list):
    """ Handler for manu oprion to get shortest and longest albums in collection and display it as table."""
    music_reports.default_view()
    max_min_list = music_reports.max_min_time(albums_list)
    music_reports.default_view()
    display.show_table(max_min_list, 'Shortest and longest albums')
Ejemplo n.º 9
0
    def round_of_bets(self):
        """Run a round of betting."""
        num_players = len(self.active_players)
        if self.table.phase == 'preflop':
            # Place blind bets
            for i in range(num_players):
                if self.active_players[i].isDealer:

                    # Player to left of dealer makes small blind bet
                    small_blind_player = None
                    for player in self.active_players:
                        if player.isSB:
                            small_blind_player = player

                    small_blind = int(self.table.big_blind / 2)
                    display.show_bet_blind(small_blind_player.name, 'small',
                                           self.pause)
                    if small_blind_player.chips > small_blind:
                        self.make_bet(small_blind_player, small_blind)
                    else:
                        self.player_moves(small_blind_player, 'all-in')
                    display.show_table(self.initial_players, self.table, 0)

                    # Player two spaces to the left of dealer makes big blind bet
                    big_blind_player = None
                    for player in self.active_players:
                        if player.isBB:
                            big_blind_player = player

                    display.show_bet_blind(big_blind_player.name, 'big',
                                           self.pause)

                    if big_blind_player.chips > self.table.big_blind:
                        self.make_bet(big_blind_player, self.table.big_blind)
                    else:
                        self.player_moves(big_blind_player, 'all-in')
                    display.show_table(self.initial_players, self.table, 0)
                    break
        if self.table.phase == 'flop':
            '''
            During the preflop round, the "first act" is the player three places to the left of the dealer 
            (as the two players before the "first act" must bet the small blind and the big blind). 
            For all subsequent rounds the "first act" is the player immediately to the left of the dealer.
            '''
            # Assign First Act position (i.e. player left of dealer)
            for player in self.active_players:
                player.isFirstAct = False
            for i in range(num_players):
                if self.active_players[i].isDealer:
                    self.active_players[(i + 1) %
                                        num_players].isFirstAct = True
                    break
        x = [player.isFirstAct for player in self.active_players].index(True)
        # End round of betting when all but one player fold or when all unfolded players have locked in
        while True:
            if all(player.isLocked or player.isAllIn
                   for player in self.active_players):
                break
            if [player.isFolded
                    for player in self.active_players].count(False) == 1:
                break
            betting_player = self.active_players[(x) % num_players]
            if betting_player.isFolded or betting_player.isAllIn:
                x += 1
                continue
            self.table.update_raise_amount()
            self.player_moves(betting_player)
            betting_player.isLocked = True
            x += 1
            display.show_table(self.initial_players, self.table, 0)
        for player in self.active_players:
            if not player.isFolded and not player.isAllIn:
                player.isLocked = False
        # Determine amounts of side pots and players eligible for each
        if self.table.pot_transfers:
            self.table.pot_transfers.sort()
            net_transfers = []
            for i in range(len(self.table.pot_transfers) - 1):
                net_transfers.append(self.table.pot_transfers[i + 1] -
                                     self.table.pot_transfers[i])
            net_transfers.insert(0, self.table.pot_transfers[0])
            for i in range(len(net_transfers)):
                for player in self.active_players:
                    if player.bet == 0:
                        continue
                    if player.bet < net_transfers[i]:
                        self.table.pots[-1][0] += player.bet
                        player.bet = 0
                    else:
                        player.bet -= net_transfers[i]
                        self.table.pots[-1][0] += net_transfers[i]
                if i == len(net_transfers) - 1:
                    eligible_players = []
                    for player in self.active_players:
                        if not player.isFolded and not player.isAllIn:
                            eligible_players.append(player)
                else:
                    eligible_players = [
                        player for player in self.active_players
                        if player.bet > 0
                    ]
                self.table.pots.append([0, eligible_players])
        for player in self.active_players:
            if player.bet:
                self.table.pots[-1][0] += player.bet
                player.bet = 0
        self.table.last_bet = 0
        self.table.pot_transfers = []