def PokerGame(): # Create the shuffled deck of Cards deck = StackOfCards() ls_suit = ['♥', '♦', '♣', '♠'] ls_rank = [ 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K' ] for rank in ls_rank: for suit in ls_suit: deck.add(PokerCard(rank, suit)) deck.shuffle() # Poker Player initializing name = input("What is your name? ") str_amount = input("How many credits you have? ") amount = int(str_amount) print("You have " + str(amount) + " credits.") cards = PokerHand() cards.add(deck.deal()) cards.add(deck.deal()) cards.add(deck.deal()) cards.add(deck.deal()) cards.add(deck.deal()) cards.sort() player = PokerPlayer(name, amount, cards) PlayRound(player, deck)
def test_straight(self): arr = [ Card(6, "D"), Card(7, "C"), Card(8, "H"), Card(9, "C"), Card(10, "C") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, STRAIGHT)
def test_straight_flush(self): arr = [ Card(8, "C"), Card(9, "C"), Card(10, "C"), Card(11, "C"), Card(12, "C") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, STRAIGHT_FLUSH)
def test_flush(self): arr = [ Card(6, "C"), Card(7, "C"), Card(10, "C"), Card(12, "C"), Card(3, "C") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, FLUSH)
def test_one_pair(self): arr = [ Card(11, "D"), Card(3, "S"), Card(10, "C"), Card(2, "H"), Card(2, "D") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, ONE_PAIR)
def test_two_pair(self): arr = [ Card(11, "D"), Card(4, "S"), Card(4, "C"), Card(1, "H"), Card(1, "D") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, TWO_PAIR)
def test_score_hand_correctly_shows_pair(): test_hand = PokerHand('3D 5S 10H 5D QC') score = test_hand.score_hand() assert (score.cards == [Card('5D'), Card('5S'), Card('QC'), Card('10H'), Card('3D')]) or (score.cards == [ Card('5D'), Card('5S'), Card('QC'), Card('10H'), Card('3D') ])
def test_full_house(self): arr = [ Card(2, "C"), Card(2, "S"), Card(2, "H"), Card(12, "C"), Card(12, "D") ] arr = [ Card(2, "C"), Card(2, "S"), Card(12, "H"), Card(12, "C"), Card(12, "D") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, FULL_HOUSE)
def test_three_of_a_kind(self): arr = [ Card(6, "D"), Card(6, "S"), Card(6, "C"), Card(2, "H"), Card(3, "C") ] arr = [ Card(9, "D"), Card(6, "S"), Card(7, "C"), Card(7, "H"), Card(7, "D") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, THREE_OF_A_KIND)
def rank_players(self, pre_flop=False): player_hands = {} if pre_flop: for i, player in enumerate(self._players): player_hands[i] = MiniPokerHand.from_str( \ " ".join(player.own_cards)) else: for i, player in enumerate(self._players): possible_hands = [] for c in combinations(self._community_cards, 3): hand = player.own_cards + list(c) possible_hands.append(PokerHand.from_str(" ".join(hand))) player_hands[i] = sorted(possible_hands, reverse=True)[0] players = (sorted(player_hands.items(), key=lambda kv: kv[1], reverse=True)) for i in range(len(players)): player_id = players[i][0] self._table_ranks[i] = player_id
def test_four_of_kind(self): arr = [ Card(9, "C"), Card(9, "S"), Card(9, "H"), Card(9, "D"), Card(11, "S") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, FOUR_OF_A_KIND) arr = [ Card(3, "S"), Card(13, "C"), Card(13, "S"), Card(13, "H"), Card(13, "D") ] ph = PokerHand(arr) ph.classify() self.assertEqual(ph.hand_type, FOUR_OF_A_KIND)
def test_pokerhand_creates_a_hand_from_string(): myhand = PokerHand('2H 3D 5S 9C KD') assert (type(myhand) == PokerHand)
def test_lowhand_calling_highhand_loses(): high_hand = PokerHand('2C 3H 4S 8C AH') low_hand = PokerHand('2H 3D 5S 9C KD') assert (low_hand.call(high_hand) == "Lose")
def test_highhand_calling_lowhand_wins(): high_hand = PokerHand('2C 3H 4S 8C AH') low_hand = PokerHand('2H 3D 5S 9C KD') assert (high_hand.call(low_hand) == "Win")
def test_string_representation_of_pokerhand(): myhand = PokerHand(['2H', '3D', '5S', '9C', 'KD']) assert (str(myhand) == '2H 3D 5S 9C KD')
def test_pokerhand_does_not_accept_invalid_hand(): with pytest.raises(ValueError): myhand = PokerHand('13H 3D 5S 9C KD')
def test_pokerhand_creates_a_hand_from_list(): myhand = PokerHand(['2H', '3D', '5S', '9C', 'KD']) assert (type(myhand) == PokerHand)
def PlayRound(player, deck): print("Cards Dealt: " + player.hand.__str__()) # Deal 2nd hand of cards hand2 = PokerHand() cards_held = player.askHoldChoice() for i in range(player.hand.size()): if i in cards_held: hand2.add(player.getCard(i)) else: hand2.add(deck.deal()) hand2.sort() print(hand2.__str__()) # Check poker score of hand2 handType = hand2.handTyoe() print("You got: " + handType) if handType == "Royal Flush": player_win = 250 elif handType == "Straight Flush": player_win = 50 elif handType == "Four of a Kind": player_win = 25 elif handType == "Full House": player_win = 9 elif handType == "Flush": player_win = 6 elif handType == "Straight": player_win = 4 elif handType == "Three of a kind": player_win = 3 elif handType == "Two Pairs": player_win = 2 elif handType == "Pair (Jacks or better)": player_win = 1 else: player_win = 0 player.addMoney( player_win - 1) # Need to subtract 1 credit as each play costs 1 credit. print("You won these many credits: " + str(player_win)) print("Credits remaining: " + str(player.getMoney())) # Ask if you want to play again playAgain = input("Would you like to play again? (y/n)? ") if player.getMoney() == 0: print("You have no more credits, GAME OVER") elif playAgain == 'y' or playAgain == 'Y': cards = PokerHand() cards.add(deck.deal()) cards.add(deck.deal()) cards.add(deck.deal()) cards.add(deck.deal()) cards.add(deck.deal()) cards.sort() player = PokerPlayer(player.getName(), player.getMoney(), cards) deck = StackOfCards() ls_suit = ['♥', '♦', '♣', '♠'] ls_rank = [ 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K' ] for rank in ls_rank: for suit in ls_suit: deck.add(PokerCard(rank, suit))
from pokerhand import PokerHand import time if __name__ == '__main__': with open('poker.txt', 'r') as f: then = time.time() scores = [] for line in f: hand1 = line[0:14] hand2 = line[15:] pass #print "Hand 1:", hand1, "Hand 2:", hand2.strip() ph1, ph2 = PokerHand.from_str(hand1), PokerHand.from_str(hand2) scores.append(ph1.score) scores.append(ph2.score) if ph1 < ph2: print "Winner:", hand2.strip(), "Score:", ph2.score, "Loosing Score:", ph1.score elif ph2 < ph1: print "Winner:", hand1.strip(), "Score:", ph1.score, "Loosing Score:", ph2.score else: print "Equal:", hand1.strip(), ":", hand2.strip(), "Score:", ph1.score print "-------" now = time.time() print now - then d = dict() for score in set(scores): d.setdefault(score, scores.count(score)) print d
from card import Card from deck import Deck from constants import * from pokerhand import PokerHand #Create array named counts counts = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for x in range(0, 2000): deck = Deck() deck.shuffle() poker_hands = [] for i in range(0, 5): arr = [] for j in range(0, 5): arr.append(deck.deal()) ph = PokerHand(arr) poker_hands.append(ph) for ph in poker_hands: ph.classify() for ph in poker_hands: counts[ph.hand_type] += 1 #Print array named counts print(counts)