Ejemplo n.º 1
0
 def play_card(self, cards):
     tot_count = sum([StandardRules.get_numeric_card_value(card) for card in cards])
     playable_cards = [card for card in self.hand if card not in self.played_cards]
     playable_cards = [card for card in playable_cards if 31 - tot_count >= StandardRules.get_numeric_card_value(card)]
     if len(playable_cards) == 0:
         return None
     else:
         card_to_play = playable_cards.   pop(randint(0, len(playable_cards) - 1))
         self.played_cards.append(card_to_play)
         return card_to_play
Ejemplo n.º 2
0
    def play_round(self):
        self.deck = Deck.get_shuffled_deck()

        # Set the dealer for the round
        if self.rounds % 2 == 0:
            dealer = self.player1
            non_dealer = self.player2
        else:
            dealer = self.player2
            non_dealer = self.player1

        # Get player hands for the round
        non_dealer_hand = []
        dealer_hand = []
        for i in range(6):
            non_dealer_hand.append(self.deck.draw())
            dealer_hand.append(self.deck.draw())

        non_dealer.set_hand(non_dealer_hand)
        dealer.set_hand(dealer_hand)

        # Set the communal card
        up_card = self.deck.draw()
        dealer.peg(self.board, StandardRules.get_cut_jack_score(up_card))
        if self.victory():
            return

        # Set the crib
        crib = non_dealer.place_crib_cards() + dealer.place_crib_cards()

        # "The Play" phase
        last_card = None
        played_cards = []
        current_stack = []
        active_player = non_dealer
        inactive_player = dealer
        while len(played_cards) < 8:
            played_card = active_player.play_card(current_stack)
            if played_card is not None:
                played_cards.append(played_card)
                current_stack.append(played_card)
                if sum([
                        StandardRules.get_numeric_card_value(card)
                        for card in current_stack
                ]) == 31:
                    active_player.peg(self.board,
                                      StandardRules.get_thirtyone_score())
                    if self.victory():
                        return
                    current_stack = []
            else:
                if last_card is None:
                    active_player.peg(self.board,
                                      StandardRules.get_last_card_score())
                    if self.victory():
                        return
                    current_stack = []

            last_card = played_card
            active_player, inactive_player = inactive_player, active_player

        # "The Show" phase
        non_dealer_hand = non_dealer.get_hand() + [up_card]
        dealer_hand = dealer.get_hand() + [up_card]
        crib_hand = crib + [up_card]
        non_dealer.peg(self.board,
                       StandardRules.get_hand_score(non_dealer_hand, up_card))
        if self.victory():
            return
        dealer.peg(self.board,
                   StandardRules.get_hand_score(dealer_hand, up_card))
        dealer.peg(self.board,
                   StandardRules.get_hand_score(crib_hand, up_card))