def test_beats(self): def checks(card1, card2, trump, served): if card1.suit is trump and card2.suit is not trump: self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' ) if card1.suit is served and card2.suit is not trump and card2.suit is not served: self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' ) if card1.suit is not trump and card1.suit is card2.suit and card1.rank >= card2.rank: self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' ) if card1.suit is trump and card2.suit is trump: if card1.rank is Rank.jack: self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' ) elif card1.rank is Rank.nine and card2.rank is not Rank.jack: self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' ) elif card2.rank is not Rank.jack and card2.rank is not Rank.nine and card1.rank >= card2.rank: self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' ) for _ in range(1000): card1 = Card(self.random_rank(), self.random_suit()) card2 = Card(self.random_rank(), self.random_suit()) checks(card1, card2, trump=self.random_suit(), served=self.random_suit()) checks(card2, card1, trump=self.random_suit(), served=self.random_suit())
def choose_trump(self, state: ChooseTrumpState) -> ChooseTrumpAction: print(state.hand) s = '' if state.can_chibre: s = ' or chibre' print(f'Choose trump suit... (d, s, h, c{s})') while True: try: inputs = input() if state.can_chibre and inputs == 'chibre': suit = None else: suit = Card(Rank.six, inputs).suit # small hack to parse string properly return ChooseTrumpAction(suit=suit) except ValueError: print('Invalid input...') pass
def test_sorted(self): cards = sorted([ Card(self.random_rank(), self.random_suit()) for _ in range(1000) ], reverse=True) trump = Suit.clubs served = Suit.hearts def is_not_special(card): return not (card.suit is trump and (card.rank is Rank.jack or card.rank is Rank.nine)) for card1, card2 in zip(cards[:-1], cards[1:]): if is_not_special(card1) and is_not_special(card2): self.assertTrue( card1.beats(card2, served=served, trump=trump), msg= f'{card1} should beat {card2} (trump={trump}, served={served})' )
def play_card(self, state: PlayCardState) -> PlayCardAction: print(f'My hand [{" ".join([str(c) for c in sorted(state.hand_cards)])}]') # print(f'Cards playable [{" ".join([str(c) for c in sorted(state.playable_cards)])}]') print('Choose a card to play (e.g. 7d = 7♢)...') while True: try: inputs = input() if len(inputs) >= 2: card_to_play = Card(inputs[:-1], inputs[-1]) if card_to_play in state.playable_cards: return PlayCardAction( card_to_play=card_to_play ) else: print('Do you know the rules bro?') continue except ValueError: pass print('Invalid input...')
def test_hash(self): self.assertEqual(hash(Suit.hearts), hash(Suit('♡'))) self.assertEqual(hash(Rank.queen), hash(Rank(12))) self.assertEqual(hash(Card(Rank.eight, Suit.hearts)), hash(Card(Rank(8), Suit('♡'))))
def test_equal(self): self.assertEqual(Suit.hearts, Suit('♡')) self.assertEqual(Rank.queen, Rank(12)) self.assertEqual(Card(Rank.eight, Suit.hearts), Card(Rank(8), Suit('♡')))
def test_card_init_bad(self): with self.assertRaises(ValueError): Card(5, Suit.clubs) with self.assertRaises(ValueError): Card(10, 'hearts')
def test_card_str(self): self.assertEqual(str(Card(6, Suit.spades)), '6♠') self.assertEqual(str(Card(Rank.ace, Suit.clubs)), 'A♣') self.assertEqual(str(Card(10, Suit.diamonds)), '10♢') self.assertEqual(str(Card(11, Suit.hearts)), 'J♡') self.assertEqual(str(Card('q', 'h')), 'Q♡')
def has_7_diamonds(self) -> bool: return self.__hand.has(Card(7, Suit.diamonds))