def test_hand_scoring(self): card = [ Card('4', 'H'), Card('7', 'H'), Card('Q', 'H'), Card('K', 'H'), Card('2', 'H') ] up_card = Card('4', 'H') assert StandardRules.get_hand_score(card, up_card) == 5 card = [ Card('4', 'H'), Card('7', 'H'), Card('Q', 'H'), Card('K', 'S'), Card('2', 'H') ] up_card = Card('4', 'H') assert StandardRules.get_hand_score(card, up_card) == 0 card = [ Card('4', 'S'), Card('7', 'H'), Card('Q', 'H'), Card('K', 'H'), Card('2', 'H') ] up_card = Card('4', 'S') assert StandardRules.get_hand_score(card, up_card) == 4 card = [ Card('5', 'S'), Card('5', 'C'), Card('5', 'D'), Card('5', 'H'), Card('J', 'S') ] up_card = Card('5', 'S') assert StandardRules.get_hand_score(card, up_card) == 29
def place_crib_cards(self): best_combo = [] best_score = -1 for combo in combinations(self.hand, 4): hand_score = StandardRules.get_hand_score(combo, None) if hand_score > best_score: best_score = hand_score best_combo = combo crib_cards = [] for i in range(len(self.hand)): if self.hand[i] not in best_combo: crib_cards.append(self.hand[i]) self.hand = list(best_combo) return crib_cards
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))