Ejemplo n.º 1
0
    def __call__(self, common_vars, button_status):
        """

        :param common_vars:
        :param button_status:
        :return: None

        """
        logging.debug(type(self).__name__ + ':' + 'enter')

        if is_cut_passed(common_vars.shoe_of_decks):
            logging.info(
                type(self).__name__ +
                ': Cut passed, create new shoe with {0} decks'.format(
                    NUM_OF_DECKS))
            common_vars.shoe_of_decks = CardDecks(NUM_OF_DECKS)

        plot_chips(common_vars.screen, common_vars.player_cash,
                   common_vars.chips_image_width, False)

        common_vars.first_card_hidden = False  # Show the dealers second card
        num_of_hands = len(common_vars.player_hands)
        value_of_dealer_hand = get_value_of_dealers_hand(
            common_vars.dealer_cards)
        common_vars.dealer_last_hand = value_of_dealer_hand
        value_of_player_hand = get_value_of_players_hand(
            common_vars.player_hands[self._current_hand])

        if value_of_dealer_hand == 21:
            logging.info(
                type(self).__name__ +
                ': Le Dealer a{0}, le Joueur a {1}'.format(
                    value_of_dealer_hand, value_of_player_hand))
            if value_of_player_hand < 21:
                # Player's current hand loose against dealer
                common_vars.pause_time = PAUSE_TIMER3
                plot_results(
                    common_vars.screen, common_vars.text_font,
                    'Le Dealer a{0}, le Joueur a {1}'.format(
                        value_of_dealer_hand, value_of_player_hand))
                if num_of_hands == 1:
                    # Only one player hand
                    common_vars.hands_status['first_hand_loose'] = True
                    self._current_hand = 0
                    button_status.reset()
                    self.next_state(InitialState)
                elif num_of_hands == 2 and self._current_hand == 0:
                    # Pop one bet pile from the player which is lost
                    common_vars.player_bets.pop()
                    # First hand in split mode, step to next hand
                    self._current_hand += 1
                    common_vars.hands_status['first_hand_loose'] = True
                else:
                    # Second hand in a split mode
                    common_vars.hands_status['second_hand_loose'] = True
                    self._current_hand = 0
                    button_status.reset()
                    self.next_state(InitialState)
            else:
                logging.info(
                    type(self).__name__ +
                    ': Both dealer and player has 21, a push')
                common_vars.pause_time = PAUSE_TIMER3
                plot_results(common_vars.screen, common_vars.text_font,
                             'Both dealer and player has 21, a push')
                # Pay back one bet to player
                common_vars.player_cash += sum(common_vars.player_bets.pop())
                if num_of_hands == 1 or self._current_hand == 1:
                    # Only one player hand or last hand evaluated
                    common_vars.hands_status['first_hand_push'] = True
                    self._current_hand = 0
                    button_status.reset()
                    self.next_state(InitialState)
                else:
                    # First hand in split mode, step to next hand
                    self._current_hand += 1
                    common_vars.hands_status['first_hand_push'] = True
        elif value_of_dealer_hand > 15 and value_of_dealer_hand > value_of_player_hand:
            # Dealer has at least 16 and more than the player and the dealer wins
            logging.info(
                type(self).__name__ +
                ': Le dealeur gagne avec {0} sur le joueur {1}'.format(
                    value_of_dealer_hand, value_of_player_hand))
            common_vars.pause_time = PAUSE_TIMER3
            plot_results(
                common_vars.screen, common_vars.text_font,
                'Le dealeur gagne avec {0} sur le joueur {1}'.format(
                    value_of_dealer_hand, value_of_player_hand))
            if num_of_hands == 1 or self._current_hand == 1:
                # Only one player hand or last hand evaluated
                common_vars.hands_status['first_hand_loose'] = True
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            else:
                # First hand in split mode, step to next hand
                self._current_hand += 1
                common_vars.hands_status['first_hand_loose'] = True
        elif value_of_player_hand > 21:
            # Player is busted from previous state (possibly at a double down)
            logging.info(
                type(self).__name__ +
                ': Le joueur est bust avec {0}'.format(value_of_player_hand))
            common_vars.pause_time = PAUSE_TIMER3
            plot_results(
                common_vars.screen, common_vars.text_font,
                'Le joueur est bust avec {0}'.format(value_of_player_hand))
            if num_of_hands == 1 or self._current_hand == 1:
                # Only one player hand or last hand evaluated
                common_vars.hands_status['first_hand_busted'] = True
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            else:
                # First hand in split mode, step to next hand
                self._current_hand += 1
                common_vars.hands_status['first_hand_busted'] = True
        else:
            self._current_hand = 0
            self.next_state(DealerHitState)

        plot_bets(common_vars.screen, common_vars.player_bets)

        plot_buttons(common_vars.screen, button_status)

        plot_players_hands(common_vars.screen, PLAYER_CARD_START_POS,
                           common_vars.player_hands, common_vars.double_downs,
                           common_vars.hands_status)

        plot_dealers_hand(common_vars.screen, DEALER_CARD_START_POS,
                          common_vars.dealer_cards,
                          common_vars.first_card_hidden)
Ejemplo n.º 2
0
    def __call__(self, common_vars, button_status):
        """

        :param common_vars:
        :param button_status:
        :return: None

        """
        logging.debug(type(self).__name__ + ':' + 'enter')

        if is_cut_passed(common_vars.shoe_of_decks):
            logging.info(
                type(self).__name__ +
                ': Cut passed, create new shoe with {0} decks'.format(
                    NUM_OF_DECKS))
            common_vars.shoe_of_decks = CardDecks(NUM_OF_DECKS)

        plot_chips(common_vars.screen, common_vars.player_cash,
                   common_vars.chips_image_width, False)

        sound_db = SoundDB.get_instance()
        card_sound = sound_db.get_sound(SOUND_PATH + 'cardslide.wav')

        num_of_hands = len(common_vars.player_hands)
        value_of_dealer_hand = get_value_of_dealers_hand(
            common_vars.dealer_cards)
        common_vars.dealer_last_hand = value_of_dealer_hand
        value_of_player_hand = get_value_of_players_hand(
            common_vars.player_hands[self._current_hand])

        if value_of_dealer_hand < 16:
            # Dealer is forced to hit until 16, no matter what hand the player has
            card_sound.play()
            card = common_vars.shoe_of_decks.pop()
            common_vars.dealer_cards.append(card)
            common_vars.pause_time = 1.0
        elif value_of_dealer_hand < 17 and value_of_dealer_hand < value_of_player_hand:
            # Dealer has less than 17 and less than the players current hand
            card_sound.play()
            card = common_vars.shoe_of_decks.pop()
            common_vars.dealer_cards.append(card)
            common_vars.pause_time = 1.0
        elif value_of_player_hand > 21 or 22 > value_of_dealer_hand > value_of_player_hand:
            # Dealer wins this hand
            common_vars.pause_time = PAUSE_TIMER3
            if value_of_player_hand > 21:
                logging.info(
                    type(self).__name__ +
                    ': Player is busted {0}'.format(value_of_player_hand))
                plot_results(
                    common_vars.screen, common_vars.text_font,
                    'Player is busted {0}'.format(value_of_player_hand))
                if self._current_hand == 0:
                    common_vars.hands_status['first_hand_busted'] = True
                else:
                    common_vars.hands_status['second_hand_busted'] = True
            else:
                logging.info(
                    type(self).__name__ +
                    ': Le Dealer gagne avec {0} sur le joueur {1}'.format(
                        value_of_dealer_hand, value_of_player_hand))
                plot_results(
                    common_vars.screen, common_vars.text_font,
                    'Le Dealer gagne avec {0} sur le joueur {1}'.format(
                        value_of_dealer_hand, value_of_player_hand))
                if self._current_hand == 0:
                    common_vars.hands_status['first_hand_loose'] = True
                else:
                    common_vars.hands_status['second_hand_loose'] = True
            # Pop one bet pile from the player which is lost
            common_vars.player_bets.pop()
            if common_vars.double_downs[self._current_hand]:
                # Pop the second bet pile for this hand which has been doubled down'ed
                common_vars.player_bets.pop()
            common_vars.pause_time = PAUSE_TIMER3
            if num_of_hands == 1 or self._current_hand == 1:
                # We're done if there is one player hand only or second hand has been evaluated
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            else:
                # First hand in split mode evaluated, let's switch to second hand
                self._current_hand += 1
        elif value_of_dealer_hand == value_of_player_hand:
            # Both dealer and player has the same value, a push
            common_vars.pause_time = PAUSE_TIMER3
            logging.info(
                type(self).__name__ +
                ': Un push, le dealer a {0}, le joueur a {1}'.format(
                    value_of_dealer_hand, value_of_player_hand))
            plot_results(
                common_vars.screen, common_vars.text_font,
                'Un push, le dealer a {0}, le joueur a {1}'.format(
                    value_of_dealer_hand, value_of_player_hand))
            if self._current_hand == 0:
                common_vars.hands_status['first_hand_push'] = True
            else:
                common_vars.hands_status['second_hand_push'] = True

            if num_of_hands == 1 or self._current_hand == 1:
                # We're done if there is one player hand only or second hand has been evaluated
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            else:
                # First hand in split mode evaluated, let's switch to second hand
                self._current_hand += 1

            # Pay back one bet to player
            common_vars.player_cash += sum(common_vars.player_bets.pop())
            if common_vars.double_downs[self._current_hand]:
                # And pay back the second bet pile for this hand which has been doubled down'ed
                common_vars.player_cash += sum(common_vars.player_bets.pop())

        else:
            # Player wins this hand
            if self._current_hand == 0:
                common_vars.hands_status['first_hand_win'] = True
            else:
                common_vars.hands_status['second_hand_win'] = True
            logging.info(
                type(self).__name__ +
                ': Le joueur gagne avec {0} sur le  dealer {1}, le pari est {2}'
                .format(value_of_player_hand, value_of_dealer_hand,
                        common_vars.player_bets[0]))
            common_vars.pause_time = PAUSE_TIMER3
            plot_results(
                common_vars.screen, common_vars.text_font,
                "Le joueur gagne avec {0} sur le  dealer {1}".format(
                    value_of_player_hand, value_of_dealer_hand))
            common_vars.player_cash += sum(common_vars.player_bets.pop()) * 2
            if common_vars.double_downs[self._current_hand]:
                # Doubled down hand, add additional win
                logging.info(
                    type(self).__name__ +
                    ': Doublez la mise, ajoutez une victoire supplementaire {0}'
                    .format(common_vars.player_bets[0]))
                common_vars.player_cash += sum(
                    common_vars.player_bets.pop()) * 2
            common_vars.dealer_last_hand = value_of_dealer_hand
            if num_of_hands == 1 or self._current_hand == 1:
                # We're done if there is one player hand only or second hand has been evaluated
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            else:
                # First hand in split mode evaluated, let's switch to second hand
                self._current_hand += 1

        plot_bets(common_vars.screen, common_vars.player_bets)

        plot_buttons(common_vars.screen, button_status)

        plot_players_hands(common_vars.screen, PLAYER_CARD_START_POS,
                           common_vars.player_hands, common_vars.double_downs,
                           common_vars.hands_status)

        plot_dealers_hand(common_vars.screen, DEALER_CARD_START_POS,
                          common_vars.dealer_cards,
                          common_vars.first_card_hidden)
Ejemplo n.º 3
0
    def __call__(self, common_vars, button_status):
        """

        :param common_vars:
        :param button_status:
        :return: None

        """
        logging.debug(type(self).__name__ + ':' + 'enter')

        if is_cut_passed(common_vars.shoe_of_decks):
            logging.info(
                type(self).__name__ +
                ': Cut passed, create new shoe with {0} decks'.format(
                    NUM_OF_DECKS))
            common_vars.shoe_of_decks = CardDecks(NUM_OF_DECKS)

        plot_chips(common_vars.screen, common_vars.player_cash,
                   common_vars.chips_image_width, False)
        plot_buttons(common_vars.screen, button_status)

        sound_db = SoundDB.get_instance()
        card_sound = sound_db.get_sound(SOUND_PATH + 'cardslide.wav')

        first_hand = 0
        second_hand = 1
        if len(common_vars.player_hands) == 1:
            hand_instance = []
            common_vars.player_hands.append(hand_instance)
            common_vars.player_hands[second_hand].append(
                common_vars.player_hands[first_hand].pop())

        logging.info(
            type(self).__name__ +
            ': {0}:{1}'.format(len(common_vars.player_hands[first_hand]),
                               len(common_vars.player_hands[second_hand])))

        if len(common_vars.player_hands[second_hand]) != 2:
            # Fill up each hand with one additional card
            common_vars.pause_time = PAUSE_TIMER1
            if len(common_vars.player_hands[first_hand]) < 2:
                card_sound.play()
                card = common_vars.shoe_of_decks.pop()
                common_vars.player_hands[first_hand].append(card)
            elif len(common_vars.player_hands[second_hand]) < 2:
                card_sound.play()
                card = common_vars.shoe_of_decks.pop()
                common_vars.player_hands[second_hand].append(card)
        else:
            # Both hands have now two cards, let's evaluate
            value_of_players_hands = 0
            for hand in common_vars.player_hands:
                value_of_players_hands += get_value_of_players_hand(hand)
            if value_of_players_hands != 42:
                # Not two times 21 or the answer to the meaning of life, continue to next state
                button_status.hit = True
                button_status.stand = True
                button_status.double_down = can_double_bet(
                    common_vars.player_bets, common_vars.player_cash)
                self.next_state(PlayerHitState)
            else:
                # WOW!!! The player got two two-card hands with 21, what's the chance for this
                value_of_dealers_hand = get_value_of_dealers_hand(
                    common_vars.dealer_cards)
                common_vars.dealer_last_hand = value_of_dealers_hand
                sum_of_bets = 0
                for bet in common_vars.player_bets:
                    sum_of_bets += sum(bet)
                logging.info(
                    type(self).__name__ + ':' +
                    'sum_of_bets = {0}'.format(sum_of_bets))
                if value_of_dealers_hand == 21:
                    # A Tie or push, bets going back to player
                    logging.info(type(self).__name__ + ':' + 'Push')
                    plot_results(common_vars.screen, common_vars.text_font,
                                 'Push')
                    common_vars.player_hands['first_hand_push'] = True
                    common_vars.player_hands['second_hand_push'] = True
                    common_vars.player_cash += sum_of_bets
                else:
                    # Double BlackJack, pay 3/2 (1.5)
                    logging.info(
                        type(self).__name__ + ':' + 'Double BlackJack!!!')
                    plot_results(common_vars.screen, common_vars.text_font,
                                 'Double Black Jack!!!')
                    common_vars.player_hands['first_hand_blackjack'] = True
                    common_vars.player_hands['second_hand_blackjack'] = True
                    common_vars.player_cash += sum_of_bets  # First get the bet back
                    common_vars.player_cash += int(sum_of_bets * 1.5)

                # Create a short pause to present the result of the hand
                common_vars.pause_time = PAUSE_TIMER3
                button_status.reset()
                self.next_state(InitialState)

        plot_bets(common_vars.screen, common_vars.player_bets)

        plot_players_hands(common_vars.screen, PLAYER_CARD_START_POS,
                           common_vars.player_hands, common_vars.double_downs,
                           common_vars.hands_status)

        plot_dealers_hand(common_vars.screen, DEALER_CARD_START_POS,
                          common_vars.dealer_cards,
                          common_vars.first_card_hidden)
Ejemplo n.º 4
0
    def __call__(self, common_vars, button_status):
        """

        :param common_vars:
        :param button_status:
        :return: None

        """
        logging.debug(type(self).__name__ + ':' + 'enter')

        if is_cut_passed(common_vars.shoe_of_decks):
            logging.info(
                type(self).__name__ +
                ': Cut passed, create new shoe with {0} decks'.format(
                    NUM_OF_DECKS))
            common_vars.shoe_of_decks = CardDecks(NUM_OF_DECKS)

        plot_chips(common_vars.screen, common_vars.player_cash,
                   common_vars.chips_image_width, False)

        sound_db = SoundDB.get_instance()
        card_sound = sound_db.get_sound(SOUND_PATH + 'cardslide.wav')

        num_of_hands = len(common_vars.player_hands)
        if num_of_hands == 2:
            image_db = ImageDB.get_instance()
            if self._current_hand == 0:
                common_vars.screen.blit(
                    image_db.get_image(IMAGE_PATH + 'hand.png'), (100, 315))
            else:
                common_vars.screen.blit(
                    image_db.get_image(IMAGE_PATH + 'hand.png'),
                    (100 + GAP_BETWEEN_SPLIT, 315))

        value_of_players_hand = get_value_of_players_hand(
            common_vars.player_hands[self._current_hand])
        if value_of_players_hand > 21:
            logging.info(
                type(self).__name__ +
                ': Joueur a bust {0}'.format(value_of_players_hand))
            common_vars.pause_time = PAUSE_TIMER3
            plot_results(common_vars.screen, common_vars.text_font,
                         'Joueur a bust {0}'.format(value_of_players_hand))
            if num_of_hands == 1:
                common_vars.hands_status['first_hand_busted'] = True
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            elif self._current_hand == 0:
                # In split mode and first hand busted
                common_vars.hands_status['first_hand_busted'] = True
                button_status.double_down = True
                self._current_hand += 1
            elif self._current_hand == 1 and common_vars.hands_status[
                    'first_hand_busted']:
                # In split mode and both hands busted
                common_vars.hands_status['second_hand_busted'] = True
                self._current_hand = 0
                button_status.reset()
                self.next_state(InitialState)
            else:
                # In split mode and first hand ok, but second hand busted
                common_vars.hands_status['second_hand_busted'] = True
                self._current_hand = 0
                self.next_state(DealerInitState)
        elif value_of_players_hand == 21:
            if num_of_hands == 2 and self._current_hand == 0:
                logging.info(
                    type(self).__name__ + ': first hand has ' +
                    '21, save this hand for later evaluation')
                self._current_hand += 1
            else:
                logging.info(
                    type(self).__name__ + ': second hand has ' +
                    '21, lets see what the dealer has')
                self._current_hand = 0
                self.next_state(DealerInitState)
        else:
            # Create detectable areas for the buttons, used when mouse is clicked
            button_collide_instance = ButtonCollideArea.get_instance(
                common_vars)
            plot_buttons(common_vars.screen, button_status)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    common_vars.done = True
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    mouse_position = pygame.mouse.get_pos(
                    )  # returns (x, y) in a tuple
                    if button_collide_instance.hit_button_area.collidepoint(
                            mouse_position[0], mouse_position[1]):
                        logging.info(type(self).__name__ + ': [Hit] pressed')
                        card_sound.play()
                        card = common_vars.shoe_of_decks.pop()
                        common_vars.player_hands[self._current_hand].append(
                            card)
                        button_status.double_down = False
                    elif button_status.double_down and button_collide_instance.double_down_button_area.\
                            collidepoint(mouse_position[0], mouse_position[1]):
                        logging.info(
                            type(self).__name__ + ': [Double down] pressed')
                        common_vars.double_downs[self._current_hand] = True
                        common_vars.player_cash -= sum(
                            common_vars.player_bets[0])
                        common_vars.player_bets.append(
                            common_vars.player_bets[0])
                        logging.info(
                            type(self).__name__ +
                            ': Remaining credits {0}'.format(
                                common_vars.player_cash))
                        card_sound.play()
                        card = common_vars.shoe_of_decks.pop()
                        common_vars.player_hands[self._current_hand].append(
                            card)
                        if num_of_hands == 2 and self._current_hand == 0:
                            # One hand left to handle
                            self._current_hand += 1
                        else:
                            self._current_hand = 0
                            button_status.double_down = False
                            self.next_state(DealerInitState)
                    elif button_collide_instance.stand_button_area.collidepoint(
                            mouse_position[0], mouse_position[1]):
                        logging.info(
                            type(self).__name__ +
                            ': [Stands] pressed, player has {0}'.format(
                                value_of_players_hand))
                        if num_of_hands == 2 and self._current_hand == 0:
                            # One hand left to handle
                            self._current_hand += 1
                            button_status.double_down = True
                        else:
                            self._current_hand = 0
                            self.next_state(DealerInitState)

        plot_bets(common_vars.screen, common_vars.player_bets)

        plot_buttons(common_vars.screen, button_status)

        plot_players_hands(common_vars.screen, PLAYER_CARD_START_POS,
                           common_vars.player_hands, common_vars.double_downs,
                           common_vars.hands_status)

        plot_dealers_hand(common_vars.screen, DEALER_CARD_START_POS,
                          common_vars.dealer_cards,
                          common_vars.first_card_hidden)
Ejemplo n.º 5
0
    def __call__(self, common_vars, button_status):
        """

        :param common_vars:
        :param button_status:
        :return: None

        """
        logging.debug(type(self).__name__ + ':' + 'enter')

        if is_cut_passed(common_vars.shoe_of_decks):
            logging.info(
                type(self).__name__ +
                ': Cut passed, create new shoe with {0} decks'.format(
                    NUM_OF_DECKS))
            common_vars.shoe_of_decks = CardDecks(NUM_OF_DECKS)
            # common_vars.shoe_of_decks = TestingCardDeck()

        plot_chips(common_vars.screen, common_vars.player_cash,
                   common_vars.chips_image_width, False)

        sound_db = SoundDB.get_instance()
        card_sound = sound_db.get_sound(SOUND_PATH + 'cardslide.wav')

        first_hand = 0  # We have only one hand for the player in this state
        if len(common_vars.dealer_cards) < 2:
            # Create a short pause between the dealt first two cards
            common_vars.pause_time = PAUSE_TIMER1

            if not common_vars.player_hands[first_hand]:
                # Empty hand, pull first card for the player
                card_sound.play()
                card = common_vars.shoe_of_decks.pop()
                common_vars.player_hands[first_hand].append(card)

            elif not common_vars.dealer_cards:
                # Empty hand, pull first card for the dealer
                card_sound.play()
                card = common_vars.shoe_of_decks.pop()
                common_vars.dealer_cards.append(card)

            elif len(common_vars.player_hands[first_hand]) == 1:
                # Pull second card for the player
                card_sound.play()
                card = common_vars.shoe_of_decks.pop()
                common_vars.player_hands[first_hand].append(card)

            elif len(common_vars.dealer_cards) == 1:
                # Pull second card for the dealer
                card_sound.play()
                card = common_vars.shoe_of_decks.pop()
                common_vars.dealer_cards.append(card)
        elif not button_status.hit:
            # Two cards picked for both player and dealer, let's evaluate if
            # BlackJack, Tie or possible Split.
            logging.info(
                type(self).__name__ + ': Two cards dealt, first evaluation')
            common_vars.pause_time = 0
            value_of_dealers_hand = get_value_of_dealers_hand(
                common_vars.dealer_cards)
            for hand in common_vars.player_hands:
                value_of_players_hand = get_value_of_players_hand(hand)
                if value_of_players_hand == 21 and len(
                        common_vars.player_hands) != 2:  # Not in split mode
                    # Let's evaluate and compare towards dealers hand
                    common_vars.first_card_hidden = False
                    if value_of_dealers_hand == 21:
                        # A Tie or push, bets going back to player
                        logging.info(type(self).__name__ + ':' + 'Push')
                        common_vars.pause_time = PAUSE_TIMER3
                        plot_results(common_vars.screen, common_vars.text_font,
                                     'Push')
                        common_vars.hands_status['first_hand_push'] = True
                        common_vars.player_cash += sum(
                            common_vars.player_bets[0])
                    else:
                        # A BlackJack, pay 3/2 (1.5)
                        logging.info(
                            type(self).__name__ + ':' + 'Black Jack!!!')
                        common_vars.pause_time = PAUSE_TIMER3
                        plot_results(common_vars.screen, common_vars.text_font,
                                     'Black Jack!!!')
                        common_vars.hands_status['first_hand_blackjack'] = True
                        common_vars.player_cash += sum(
                            common_vars.player_bets[0]
                        )  # First get the bet back
                        common_vars.player_cash += int(
                            sum(common_vars.player_bets[0]) * 1.5)

                    common_vars.dealer_last_hand = value_of_dealers_hand
                    # Create a short pause to present the result of the hand
                    common_vars.pause_time = PAUSE_TIMER3
                    button_status.reset()
                    self.next_state(InitialState)
                elif len(common_vars.player_hands) != 2 and is_possible_split(
                        hand):
                    # Not in split already and two equal cards
                    button_status.split = can_double_bet(
                        common_vars.player_bets, common_vars.player_cash)
                    button_status.hit = True
                else:
                    button_status.hit = True
        else:
            button_status.hit = True
            button_status.stand = True
            button_status.double_down = can_double_bet(common_vars.player_bets,
                                                       common_vars.player_cash)

        # Create detectable areas for the buttons, used when mouse is clicked
        button_collide_instance = ButtonCollideArea.get_instance(common_vars)

        plot_buttons(common_vars.screen, button_status)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                common_vars.done = True
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_position = pygame.mouse.get_pos(
                )  # returns (x, y) in a tuple
                if button_status.hit and button_collide_instance.hit_button_area.\
                        collidepoint(mouse_position[0], mouse_position[1]):
                    logging.info(type(self).__name__ + ': [Hit] pressed')
                    card_sound.play()
                    card = common_vars.shoe_of_decks.pop()
                    common_vars.player_hands[first_hand].append(card)
                    button_status.split = False
                    button_status.double_down = False
                    self.next_state(PlayerHitState)
                elif button_status.stand and button_collide_instance.stand_button_area.\
                        collidepoint(mouse_position[0], mouse_position[1]):
                    logging.info(type(self).__name__ + ': [Stand] pressed')
                    self.next_state(DealerInitState)
                elif button_status.double_down and button_collide_instance.double_down_button_area.\
                        collidepoint(mouse_position[0], mouse_position[1]):
                    logging.info(
                        type(self).__name__ + ': [Double down] pressed')
                    # Double the bet before going to DealerInitState
                    common_vars.player_cash -= sum(common_vars.player_bets[0])
                    common_vars.player_bets.append(common_vars.player_bets[0])
                    logging.info(
                        type(self).__name__ + ': Remaining credits {0}'.format(
                            common_vars.player_cash))
                    card_sound.play()
                    card = common_vars.shoe_of_decks.pop()
                    common_vars.player_hands[first_hand].append(
                        card)  # Pull a third card
                    common_vars.double_downs[first_hand] = True
                    button_status.double_down = False
                    self.next_state(DealerInitState)
                elif button_status.split and button_collide_instance.split_button_area.\
                        collidepoint(mouse_position[0], mouse_position[1]):
                    # Double the bet before going to SplitState
                    logging.info(type(self).__name__ + ': [Split] pressed')
                    common_vars.player_cash -= sum(common_vars.player_bets[0])
                    common_vars.player_bets.append(common_vars.player_bets[0])
                    # button_status.split = False
                    button_status.reset()
                    logging.info(
                        type(self).__name__ + ': Remaining credits {0}'.format(
                            common_vars.player_cash))
                    self.next_state(SplitState)

        plot_bets(common_vars.screen, common_vars.player_bets)

        plot_buttons(common_vars.screen, button_status)

        plot_players_hands(common_vars.screen, PLAYER_CARD_START_POS,
                           common_vars.player_hands, common_vars.double_downs,
                           common_vars.hands_status)

        plot_dealers_hand(common_vars.screen, DEALER_CARD_START_POS,
                          common_vars.dealer_cards,
                          common_vars.first_card_hidden)
Ejemplo n.º 6
0
 def test_new_carddeck_pop(self):
     # Asserts if pop doesn't remove card from deck
     from carddecks import CardDecks
     deck = CardDecks()  # length 52
     deck.pop()
     self.assertTrue(deck.length() == 51)
Ejemplo n.º 7
0
 def test_size_of_new_double_carddeck(self):
     # Asserts if the default size of newly created card deck with 2 decks doesnt contain 104 cards
     from carddecks import CardDecks
     decks = CardDecks(2)
     self.assertTrue(decks.length() == 104)
Ejemplo n.º 8
0
 def test_size_of_new_carddeck(self):
     # Asserts if the default size of newly created card deck is not 52 cards
     from carddecks import CardDecks
     deck = CardDecks()
     self.assertTrue(deck.length() == 52)
Ejemplo n.º 9
0
#!/usr/bin/env python
"""
Simple test of the card deck and playing card classes

Copyright (C) Torbjorn Hedqvist - All Rights Reserved
You may use, distribute and modify this code under the
terms of the MIT license. See LICENSE file in the project
root for full license information.

"""
import sys
import os

MAIN_DIR = (os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.insert(1, os.path.join(MAIN_DIR, 'includes'))
from carddecks import CardDecks

CARD_RANK = ["Invalid", "Ace", "Two", "Three", "Four", "Five", "Six",
             "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"]
CARD_SUIT = ["Spades", "Clubs", "Diamonds", "Hearts"]

DECK = CardDecks(2)
COUNT = 0
while DECK.length():
    COUNT += 1
    CARD = DECK.pop()
    print(str(COUNT) + ": " + CARD_RANK[CARD.get_rank()] + " of " + CARD_SUIT[CARD.get_suit()])