def gameinit(name: str, money: int) -> tuple: deck = create_deck() # deals 5 cards to deck hand = PokerHand() for i in range(5): hand.add(deck.deal()) player = PokerPlayer(name, money, hand) return deck, player
def test_add_to_poker_hand(): try: a_hand = PokerHand() a_hand.add_card(Card('S', 11)) a_hand.add_card(Card('S', 10)) assert_equals("Making sure that PokerHand object has add_card method.", True, type(a_hand) is PokerHand) except: fail_on_error("Trying to create a PokerHand object and add cards to it.", sys.exc_info())
def test_str(): try: a_hand = PokerHand() a_hand.add_card(Card('S', 11)) a_hand.add_card(Card('S', 10)) a_hand_string = str(a_hand) assert_in("Added card 'S11'. String representation of hand should contain 'Jack of Spades'.", "Jack of Spades", a_hand_string) assert_in("Added card 'S10'. String representation of hand should contain '10 of Spades'.", "10 of Spades", a_hand_string) except: fail_on_error("Runtime error when calling __str__ of PokerHand object.", sys.exc_info())
def __get_all_five_card_hands(self): """Determines all possible 5-card PokerHand objects from combined stud and community cards and returns them""" for i in range(0, len(self.community_card_set.community_cards)): card = self.community_card_set.get_card(i) self.add_card(card) five_card_hands = combinations(self.stud_hand, 5) a_list = [] for combination in five_card_hands: hand = PokerHand() for i in range(0, len(combination)): card = combination[i] hand.add_card(card) a_list.append(hand) return a_list
def assertHandValuedCorrectly(self, poker_hand_value): many_hands_list = [] for order in ["Higher", "Middle", "Lower"]: many_hands_list.append(test_hands[poker_hand_value.name][order]) for hand_string in many_hands_list: self.assertEqual( PokerHand(hand_string).hand_value, poker_hand_value)
def shuffleAndConfirmHandsSorted(self, list_of_hand_strings): manually_sorted_hands = [] for hand_string in list_of_hand_strings: manually_sorted_hands.append(PokerHand(hand_string)) automatically_sorted_hands = manually_sorted_hands.copy() number_of_hands = len(automatically_sorted_hands) if number_of_hands < 10: automatically_sorted_hands.reverse() else: shuffle(automatically_sorted_hands) automatically_sorted_hands.sort() self.assertEqual(manually_sorted_hands, automatically_sorted_hands)
def create_deck() -> PokerHand: deck = PokerHand() SUIT = ['♥', '♦', '♣', '♠'] RANK = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] for s in SUIT: for r in RANK: deck.add(PokerCard(r, s)) # shuffle deck.shuffle() return deck
def main(inputfile_str): #main function file = open(inputfile_str, 'r') #read file #read line by line for line in file: x = PokerHand() y = PokerHand() x = PokerHand.parse_hand(x, line[0:14]) y = PokerHand.parse_hand(y, line[15:29]) print RESULT[x.__cmp__(y)]
def play_1(player): """User decides whether Hand #1 or Hand #2 is worth more. If user answer matches the correct answer given by compare_to function, user gains 1 point; if user answers wrong or deck runs out of cards, game ends and score is printed.""" playing = True deck = Deck() deck.shuffle() score = 0 print( input("Welcome to the casino, " + player + ". Press Enter to play the game.")) while playing: if deck.out(): print("Deck is out of cards. Game over. Your score was ", str(score)) playing = False else: hand_a = PokerHand() hand_b = PokerHand() for i in range(0, MAX_HAND_SIZE): card = deck.deal() hand_a.add_card(card) for i in range(0, MAX_HAND_SIZE): card = deck.deal() hand_b.add_card(card) print("Hand #1: ", hand_a) print("Hand #2: ", hand_b, "\n") user_answer = int( input( "Which hand is worth more? " "Type 1 if Hand #1, -1 if Hand #2, and 0 if they are worth the same\n " )) correct_answer = hand_a.compare_to(hand_b) if user_answer == correct_answer: print("Correct! Let's try again\n") score += 1 else: print("Game over. Your score was ", str(score)) playing = False
def playRound(player: PokerPlayer, deck: PokerHand, cout=sys.stdout, cin=input, safemode=False) -> str: ''' explanation of program logic Take in the poker player's hand from the 52 card deck Ask for what cards they want to hold Add cards from the deck to said card Check hand type Output the hand type ''' h = encodeStr(str(player.hand)) if safemode else player.hand print("{}:\t{}".format(player.getName(), h), file=cout, flush=True) rawcards = player.askHoldChoice(cin, cout).split(' ') cardsToHold = [] if rawcards != ['']: cardsToHold = [player.getCard(int(c) - 1) for c in rawcards] #player hand = cardstohold testhand = PokerHand() testhand.cards = cardsToHold th = encodeStr(str(testhand)) if safemode else testhand print("You held:", th, file=cout, flush=True) #now add additional cards while len(cardsToHold) < 5: dealtcard = deck.deal() cardsToHold.append(dealtcard) newHand = PokerHand() newHand.cards = cardsToHold[:] #make a new poker_hand player.hand = newHand ph = encodeStr(str(player.hand)) if safemode else player.hand print("%s:" % player.getName(), ph, file=cout, flush=True) hand_type = player.hand.handType() return hand_type
def test_time_to_sort(self): dealer = Dealer() random_hands = [] # Benchmark on home PC only running this test: # Hands Times Sorted Time # 50 25000 1.112s, 1.293s, 1.193s number_of_hands_to_benchmark = 50 number_of_times_to_sort_hands = 25000 number_of_packs_to_benchmark = number_of_hands_to_benchmark / 10 test_desc = (str(number_of_hands_to_benchmark) + " hands " + str(number_of_times_to_sort_hands) + " times ") _logger.debug("Benchmark start creating PokerHand objects: " + test_desc) for hand_string in dealer.deal_pack(number_of_packs_to_benchmark): random_hands.append(PokerHand(hand_string)) _logger.debug("Benchmark end creating PokerHand objects: " + test_desc) _logger.debug("Benchmark now sorting: " + test_desc) number_of_times_sorted_hands = 0 while number_of_times_sorted_hands < number_of_times_to_sort_hands: number_of_times_sorted_hands = number_of_times_sorted_hands + 1 sorted(random_hands) _logger.debug("Benchmark end sorting: " + test_desc)
def test_invalid_value(self): with self.assertRaises(Exception): PokerHand("KS AS TS QS XS")
def test_create_poker_hand(): try: a_hand = PokerHand() assert_equals("Created a new PokerHand object.", True, type(a_hand) is PokerHand) except: fail_on_error("Trying to create a PokerHand object.", sys.exc_info())
from poker_hand import PokerHand player = PokerHand('2H 3H 4H 5H 6H') opponent = PokerHand('KS AS TS QS JS') print(player.compare_with(opponent))
# removeCard(pos) - removes a card at the pos number # addMoney(amt) - add amt to player's money # askHoldChoice() - returns players choice of cards to hold with input validation #=========================================================================== class PokerPlayer(Player): def askHoldChoice(self, cin = input, cout = sys.stdout) -> str: cards_to_hold = cin("Which cards would you like to hold?\n") if len(cards_to_hold) == 0: return '' holdlist = cards_to_hold.split(' ') # If there are more than 5 cards, try again (input validation) if len(holdlist) > 5: print("You should have 5 entries at max", file = sys.stdout) return self.askHoldChoice(cin, cout) # Input Validation by splitting by ' ' and checking if each value is an integer between 1 and 5 for s in holdlist: try: ints = int(s) except ValueError: print("Your numbers should be integers between 1 and 5 inclusive", file = cout) return self.askHoldChoice(cin, cout) if not 1 <= ints <= 5: print("Your numbers should be integers between 1 and 5 inclusive", file = cout) return self.askHoldChoice(cin, cout) return cards_to_hold if __name__ == "__main__": player = PokerPlayer("Kwarthik", 42069, PokerHand()) print(player.askHoldChoice())
def test_hand(self): self.assertTrue( PokerHand("TC TH 5C 5H KH").compare_with( PokerHand("9C 9H 5C 5H AC")) == 'WIN') self.assertTrue( PokerHand("TS TD KC JC 7C").compare_with( PokerHand("JS JC AS KC TD")) == 'LOSS') self.assertTrue( PokerHand("7H 7C QC JS TS").compare_with( PokerHand("7D 7C JS TS 6D")) == 'WIN') self.assertTrue( PokerHand("5S 5D 8C 7S 6H").compare_with( PokerHand("7D 7S 5S 5D JS")) == 'LOSS') self.assertTrue( PokerHand("AS AD KD 7C 3D").compare_with( PokerHand("AD AH KD 7C 4S")) == 'LOSS') self.assertTrue( PokerHand("TS JS QS KS AS").compare_with( PokerHand("AC AH AS AS KS")) == 'WIN') self.assertTrue( PokerHand("TS JS QS KS AS").compare_with( PokerHand("TC JS QC KS AC")) == 'WIN') self.assertTrue( PokerHand("TS JS QS KS AS").compare_with( PokerHand("QH QS QC AS 8H")) == 'WIN') self.assertTrue( PokerHand("AC AH AS AS KS").compare_with( PokerHand("TC JS QC KS AC")) == 'WIN') self.assertTrue( PokerHand("AC AH AS AS KS").compare_with( PokerHand("QH QS QC AS 8H")) == 'WIN') self.assertTrue( PokerHand("TC JS QC KS AC").compare_with( PokerHand("QH QS QC AS 8H")) == 'WIN') self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("JH JC JS JD TH")) == 'WIN') self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("4H 5H 9H TH JH")) == 'WIN') self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("7C 8S 9H TH JH")) == 'WIN') self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("TS TH TD JH JD")) == 'WIN') self.assertTrue( PokerHand("7H 8H 9H TH JH").compare_with( PokerHand("JH JD TH TC 4C")) == 'WIN') self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("4H 5H 9H TH JH")) == 'WIN') self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("7C 8S 9H TH JH")) == 'WIN') self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("TS TH TD JH JD")) == 'WIN') self.assertTrue( PokerHand("JH JC JS JD TH").compare_with( PokerHand("JH JD TH TC 4C")) == 'WIN') self.assertTrue( PokerHand("4H 5H 9H TH JH").compare_with( PokerHand("7C 8S 9H TH JH")) == 'WIN') self.assertTrue( PokerHand("4H 5H 9H TH JH").compare_with( PokerHand("TS TH TD JH JD")) == 'LOSS') self.assertTrue( PokerHand("4H 5H 9H TH JH").compare_with( PokerHand("JH JD TH TC 4C")) == 'WIN') self.assertTrue( PokerHand("7C 8S 9H TH JH").compare_with( PokerHand("TS TH TD JH JD")) == 'LOSS') self.assertTrue( PokerHand("7C 8S 9H TH JH").compare_with( PokerHand("JH JD TH TC 4C")) == 'WIN') self.assertTrue( PokerHand("TS TH TD JH JD").compare_with( PokerHand("JH JD TH TC 4C")) == 'WIN')
def appraise_hands(): test_hands = [ # high card [{ "value": 2, "suit": 0 }, { "value": 4, "suit": 1 }, { "value": 5, "suit": 2 }, { "value": 8, "suit": 3 }, { "value": 9, "suit": 1 }, { "value": 12, "suit": 0 }, { "value": 13, "suit": 0 }], # pair [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 4, "suit": 2 }, { "value": 4, "suit": 3 }, { "value": 5, "suit": 0 }, { "value": 8, "suit": 1 }, { "value": 9, "suit": 2 }], # two pair [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 3, "suit": 2 }, { "value": 4, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 5, "suit": 0 }, { "value": 8, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 7, "suit": 2 }, { "value": 7, "suit": 2 }, { "value": 12, "suit": 3 }, { "value": 12, "suit": 0 }, { "value": 14, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 7, "suit": 1 }, { "value": 7, "suit": 2 }, { "value": 12, "suit": 2 }, { "value": 12, "suit": 3 }, { "value": 14, "suit": 0 }, { "value": 14, "suit": 2 }], # trips [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 4, "suit": 2 }, { "value": 4, "suit": 0 }, { "value": 4, "suit": 3 }, { "value": 5, "suit": 0 }, { "value": 9, "suit": 2 }], # straight [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 4, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 6, "suit": 1 }, { "value": 10, "suit": 0 }, { "value": 12, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 4, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 6, "suit": 1 }, { "value": 7, "suit": 0 }, { "value": 8, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 4, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 10, "suit": 1 }, { "value": 12, "suit": 0 }, { "value": 14, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 4, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 6, "suit": 1 }, { "value": 7, "suit": 0 }, { "value": 14, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 3, "suit": 2 }, { "value": 4, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 2 }], # flush [{ "value": 2, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 2 }, { "value": 8, "suit": 0 }, { "value": 9, "suit": 0 }, { "value": 12, "suit": 0 }, { "value": 13, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 0 }, { "value": 8, "suit": 0 }, { "value": 9, "suit": 0 }, { "value": 12, "suit": 0 }, { "value": 13, "suit": 0 }], # full house [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 5, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 12, "suit": 0 }, { "value": 14, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 5, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 12, "suit": 0 }, { "value": 12, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 2, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 12, "suit": 0 }, { "value": 12, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 2, "suit": 2 }, { "value": 6, "suit": 3 }, { "value": 7, "suit": 1 }, { "value": 12, "suit": 0 }, { "value": 12, "suit": 2 }], # quads [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 2, "suit": 2 }, { "value": 2, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 2 }], [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 1 }, { "value": 2, "suit": 2 }, { "value": 2, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 5, "suit": 0 }, { "value": 5, "suit": 2 }], [{ "value": 3, "suit": 0 }, { "value": 3, "suit": 1 }, { "value": 3, "suit": 2 }, { "value": 5, "suit": 3 }, { "value": 5, "suit": 1 }, { "value": 5, "suit": 0 }, { "value": 5, "suit": 2 }], # straight flush [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 0 }, { "value": 10, "suit": 0 }, { "value": 12, "suit": 0 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 0 }, { "value": 7, "suit": 0 }, { "value": 8, "suit": 0 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 0 }, { "value": 7, "suit": 2 }, { "value": 8, "suit": 3 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 2 }, { "value": 6, "suit": 0 }, { "value": 7, "suit": 0 }, { "value": 8, "suit": 0 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 0 }, { "value": 10, "suit": 1 }, { "value": 12, "suit": 3 }, { "value": 14, "suit": 0 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 0 }, { "value": 7, "suit": 0 }, { "value": 14, "suit": 0 }], [{ "value": 2, "suit": 0 }, { "value": 3, "suit": 0 }, { "value": 3, "suit": 2 }, { "value": 4, "suit": 0 }, { "value": 5, "suit": 1 }, { "value": 5, "suit": 0 }, { "value": 6, "suit": 0 }], # royal flush [{ "value": 2, "suit": 0 }, { "value": 2, "suit": 0 }, { "value": 10, "suit": 0 }, { "value": 11, "suit": 0 }, { "value": 12, "suit": 0 }, { "value": 13, "suit": 0 }, { "value": 14, "suit": 0 }], [{ "value": 8, "suit": 0 }, { "value": 9, "suit": 0 }, { "value": 10, "suit": 0 }, { "value": 11, "suit": 0 }, { "value": 12, "suit": 0 }, { "value": 13, "suit": 0 }, { "value": 14, "suit": 0 }], ] # for test_hand in test_hands: # hand = PokerHand(test_hand) # print(hand) hands = {} # test # test_player_cards = [ # [{"value": 14, "suit": 3}, {"value": 14, "suit": 2}], # [{"value": 2, "suit": 1}, {"value": 3, "suit": 1}], # [{"value": 4, "suit": 1}, {"value": 8, "suit": 1}], # [{"value": 12, "suit": 0}, {"value": 5, "suit": 2}], # [{"value": 11, "suit": 1}, {"value": 5, "suit": 1}], # [{"value": 13, "suit": 3}, {"value": 11, "suit": 0}], # [{"value": 5, "suit": 3}, {"value": 6, "suit": 0}] # ] # test_table_cards = [ # {"value": 2, "suit": 0}, # {"value": 3, "suit": 2}, # {"value": 4, "suit": 0}, # {"value": 8, "suit": 2}, # {"value": 14, "suit": 0} # ] # for i in range(len(test_player_cards)): # cards = [] # for j in range(2): # cards.append(test_player_cards[i][j]) # for k in range(5): # cards.append(test_table_cards[k]) # print(cards) # hand = PokerHand(cards) # hands[i] = hand # print(hands) for player in players.find(): if player["status"] > 2: cards = [] for key in player["cards"]: cards.append(player["cards"][key]) table_cards = table.find_one({"_id": "table_cards"}, {"_id": False}) for card in table_cards: cards.append(table_cards[card]) hand = PokerHand(cards) hands[player["_id"]] = hand top = [] for curr in hands: if top == []: top.append(curr) print("first hand") elif hands[curr] == hands[top[0]]: print(hands[curr], hands[top[0]]) top.append(curr) print("hands equal") elif hands[curr] > hands[top[0]]: print(hands[curr], hands[top[0]]) top = [] top.append(curr) print("hand stronger") else: print(hands[curr], hands[top[0]]) print("hand weaker") return top
def test_too_many_cards(self): with self.assertRaises(Exception): PokerHand("KS AS TS QS JS 8S")
def test_no_cards(self): with self.assertRaises(Exception): PokerHand("")
def test_draw_same(self): # pylint: disable=maybe-no-member self.assertEqual( PokerHand(test_hands[PokerHandValue.FLUSH.name]["SPADES"]), PokerHand(test_hands[PokerHandValue.FLUSH.name]["HEARTS"]))
def test_compare_to_safe(msg, cards_a, cards_b, expected): try: hand_a = PokerHand() for s, v in cards_a: hand_a.add_card(Card(s, v)) hand_b = PokerHand() for s, v in cards_b: hand_b.add_card(Card(s, v)) msg += "\nA: {}\nB: {}".format(str(cards_a), str(cards_b)) if expected > 0: assert_equals(msg, True, hand_a.compare_to(hand_b) > 0) elif expected < 0: assert_equals(msg, True, hand_a.compare_to(hand_b) < 0) else: assert_equals(msg, 0, hand_a.compare_to(hand_b)) except: fail_on_error(msg, sys.exc_info())
def test_invalid_suit(self): with self.assertRaises(Exception): PokerHand("KS AS TS QS JX")