def test_hand_can_cast_itself_to_new_ranked(self): hand = Hand() hand.add_card(Card("C", "A")) hand.add_card(Card("D", "A")) new_hand = hand.to_ranked("C") for card in new_hand: assert isinstance(card, RankedCard)
class Player: def __init__(self, is_dealer=False): self.type = 'dealer' if is_dealer else 'player' self.hand = Hand() def hit(self, card): self.hand.add_card(card) def hand_total(self): return self.hand.get_total() def show_hand(self, hole_card=False): total = self.hand.get_total() for n in range(len(self.hand.cards)): if self.type == 'dealer' and n == 0 and hole_card: print('{num}) {val}'.format(num=n, val='–')) continue val = self.hand.cards[n].value print('{num}) {val}'.format(num=n, val=val)) if self.type == 'player' or not hole_card: print('Total: {total}'.format(total=total)) def __str__(self): return 'Player Type: ' + self.type + '\n'
def hit(deck: cards.Deck, hand: cards.Hand): assert type(deck) is cards.Deck assert type(hand) is cards.Hand hand.add_card(deck.deal()) hand.adjust_for_aces() return hand
def split(self, hand, bet): new_hand = Hand("Split") new_hand.add_card(hand.cards[1]) hand.remove_card(hand.cards[1]) self.deck.move_cards(hand, 1) self.deck.move_cards(new_hand, 1) self.player_hands.append(new_hand) self.player_chips = self.player_chips - bet self.bets.append(bet) self.in_play.append(True)
def hand_add_card_test(): c = Card('A', 'd') h = Hand() h.add_card(c) if h.card_count() == 1: if h.cards[0].rank == 'A' and h.cards[0].suit == 'd': return True else: return False else: return False
def hand_is_empty_test(): c = Card("A", 'd') h = Hand() if h.is_empty(): h.add_card(c) h.remove_card1() if h.is_empty(): return True else: return False else: return False
def hand_remove_card1_test(): c = Card('A', 'd') h = Hand() h.add_card(c) if h.card_count() == 1: h.remove_card1() if h.card_count() == 0: return True else: return False else: return False
def play_round(deck): playerHand = Hand() dealerHand = Hand() playerHand.add_card(deck.deal()) playerHand.add_card(deck.deal()) dealerHand.add_card(deck.deal()) dealerHand.add_card(deck.deal()) print("The dealer has: ") dealerHand.show(partial=True) print("You have: ") playerHand.show() while True: action = input("Do you want to \n" + "1. [H]it \n" + "2. [S]tand \n") if action in ["1", "h", "H"]: playerHand.add_card(deck.deal()) print("You have: ") playerHand.show() if playerHand.cur_value == GOAL_CONSTANT: win_statement("You", playerHand.cur_value) return playerHand, dealerHand, deck if playerHand.cur_value > GOAL_CONSTANT: lose_statement("You", playerHand.cur_value) return playerHand, dealerHand, deck if action in ['2', 'S', 's']: while dealerHand.cur_value < DEALER_CONSTANT: dealerHand.add_card(deck.deal()) dealerHand.show(partial=True) if dealerHand.cur_value > GOAL_CONSTANT: print("The dealer has: ") dealerHand.show() lose_statement("Dealer", dealerHand.cur_value) return playerHand, dealerHand, deck else: print("Please make a selection using 1 or 2")
def test_prints_correctly(self): hand = Hand() hand.add_card(Card("C", "A")) hand.add_card(Card("D", "A")) regex = regex_builder("Hand") assert regex.match(str(hand))
def test_add_too_many_cards_should_raise_valueerror(self): hand = Hand([Card("C", value) for value in Card.value_names]) with pytest.raises(ValueError): hand.add_card(Card("D", "S"))
def test_add_duplicate_card_should_raise_valueerror(self): hand = Hand([Card("C", "E")]) with pytest.raises(ValueError): hand.add_card(Card("C", "E"))
def test_add_valid_card_should_result_in_new_hand(self): hand = Hand([Card("C", "E")]) hand.add_card(Card("C", "T")) assert hand.cards == Hand([Card("C", "E"), Card("C", "T")]).cards
class Player: def __init__(self, name="", starting_strategy=None, playing_strategy=None): self.name = name self.hand = Hand() self.starting_strategy = starting_strategy self.playing_strategy = playing_strategy self.teamID = None def play(self, trick, trump_suit): card = self.choose_card(trick, trump_suit) self.play_card(card, trick) def choose_card(self, trick, trump_suit): if len(trick) == 0: return self.choose_card_from(self.hand) else: demanded_suit = trick[0].suit demanded_cards_in_hand = self.get_demanded_suit_cards_in_hand( demanded_suit) trump_cards_in_hand = self.get_trump_cards_in_hand(trump_suit) if len(demanded_cards_in_hand) > 0: return self.choose_card_from(demanded_cards_in_hand) elif len(trump_cards_in_hand) > 0: return self.choose_card_from(trump_cards_in_hand) else: return self.choose_card_from(self.hand) @staticmethod def choose_card_from(cards): return cards[0] def get_trump_cards_in_hand(self, trump_suit): return self._get_cards_in_hand_with_suit(trump_suit) def get_demanded_suit_cards_in_hand(self, demanded_suit): return self._get_cards_in_hand_with_suit(demanded_suit) def _get_cards_in_hand_with_suit(self, suit): return [card for card in self.hand if card.suit == suit] def play_card(self, card, trick): self.take_card(card) self.add_card_to_trick(card, trick) def take_card(self, card): self.hand.remove(card) @staticmethod def add_card_to_trick(card, trick): trick.add_card(card) def chooses_to_start(self, card): return True def add_card_to_hand(self, card): self.hand.add_card(card.with_owner(self)) def set_team_id(self, id): self.teamID = id def set_trump_suit(self, suit): self.hand = self.hand.to_ranked(suit) def __str__(self): return self.name def __repr__(self): return str(self)