class Player: # Initialize function def __init__(self): self._hand = [] self._brain = Calc() # String function def __str__(self): player_hand = "Player:\t " + " ".join(str(c) for c in self._hand) + "\n" player_best = "\t " + str(self._brain) return player_hand + player_best # Receive cards function def receive(self, card): self._hand.extend(card) self._brain.add_cards(card) # Get hand function def get_hand(self): return self._hand # discard cards function def discard(self, to_discard): discarded_cards = [crd for crd in self._hand if str(crd) in to_discard] discarded = [str(crd) for crd in discarded_cards] self._hand = [crd for crd in self._hand if str(crd) not in discarded] self._brain.clear() self._brain.add_cards(self._hand) return discarded_cards # Give back hand function def put_back_hand(self): putback = self._hand.copy() self._hand = [] self._brain.clear() return putback # Get cards from table function def look_at_table(self, cards): self._brain.add_cards(cards) # Get best hand function def best_hand(self): return self._brain.best_hand() # Get rank function def get_rank(self): return self._brain.get_rank() # Get rank as string function def get_rank_as_string(self): return str(self._brain)
def test_high_card(self): hand = [ Card("2", "H", 2), Card("3", "H", 3), Card("4", "H", 4), Card("6", "S", 6), Card("7", "D", 7), Card("8", "S", 8), Card("A", "D", 14) ] result = sorted(hand, reverse=True)[:5] test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 1) self.assertEqual(str(test_calc), "HighCard") self.assertEqual(test_calc.best_hand(), result)
def test_three_of_kind(self): hand = [ Card("2", "H", 2), Card("3", "H", 3), Card("4", "H", 4), Card("6", "C", 6), Card("6", "D", 6), Card("6", "S", 6), Card("A", "D", 14) ] result = sorted(hand, reverse=True) result = sorted(result, key=result.count, reverse=True)[:5] test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 4) self.assertEqual(str(test_calc), "ThreeOfKind") self.assertEqual(test_calc.best_hand(), result)
def test_two_pair(self): hand = [ Card("2", "H", 2), Card("4", "D", 4), Card("4", "H", 4), Card("6", "S", 6), Card("6", "D", 6), Card("8", "S", 8), Card("A", "D", 14) ] result = sorted(hand, reverse=True) result = sorted(result, key=result.count, reverse=True)[:5] test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 3) self.assertEqual(str(test_calc), "TwoPair") self.assertEqual(test_calc.best_hand(), result)
def test_royal_flush(self): hand = [ Card("10", "H", 10), Card("Q", "H", 12), Card("9", "H", 9), Card("J", "H", 11), Card("K", "H", 13), Card("6", "S", 6), Card("A", "H", 14) ] result = [c for c in hand if c.get_suit() == "H"] result = sorted(result, reverse=True)[:5] test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 10) self.assertEqual(str(test_calc), "RoyalFlush") self.assertEqual(test_calc.best_hand(), result)
def test_straight_flush(self): hand = [ Card("2", "H", 2), Card("3", "H", 3), Card("4", "H", 4), Card("6", "H", 6), Card("5", "H", 5), Card("6", "S", 6), Card("A", "D", 14) ] result = [c for c in hand if c.get_suit() == "H"][:5] result = sorted(result, reverse=True) test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 9) self.assertEqual(str(test_calc), "StraightFlush") self.assertEqual(test_calc.best_hand(), result)
def test_full_house(self): hand = [ Card("A", "H", 14), Card("A", "H", 14), Card("4", "H", 4), Card("6", "C", 6), Card("6", "D", 6), Card("6", "S", 6), Card("A", "D", 14) ] result = sorted(hand, reverse=True) result = sorted(result, key=result.count, reverse=True)[:5] test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 7) self.assertEqual(str(test_calc), "FullHouse") self.assertEqual(test_calc.best_hand(), result)
def test_flush(self): hand = [ Card("2", "H", 2), Card("3", "H", 3), Card("4", "H", 4), Card("6", "H", 6), Card("7", "H", 7), Card("8", "S", 8), Card("A", "H", 14) ] result = [c for c in hand if c.get_suit() == "H"] result = sorted(result, reverse=True)[:5] test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 6) self.assertEqual(str(test_calc), "Flush") self.assertEqual(test_calc.best_hand(), result)
def test_straight(self): hand = [ Card("2", "H", 2), Card("3", "H", 3), Card("4", "H", 4), Card("6", "S", 6), Card("5", "D", 5), Card("8", "S", 8), Card("A", "D", 14) ] result = [ Card("2", "H", 2), Card("3", "H", 3), Card("4", "H", 4), Card("6", "S", 6), Card("5", "D", 5) ] result = sorted(result, reverse=True) test_calc = Calc(hand) self.assertEqual(test_calc.get_rank(), 5) self.assertEqual(str(test_calc), "Straight") self.assertEqual(test_calc.best_hand(), result)