Created
January 12, 2018 00:00
-
-
Save bennuttall/a7c7df5194a9a3b8d7c50f8cc2dc878c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import product | |
from random import shuffle | |
VALUES = '23456789TJQKA' | |
SUITS = 'HDSC' | |
HAND_TYPES = ( | |
'High Card', | |
'Pair', | |
'Two Pairs', | |
'Three of a Kind', | |
'Straight', | |
'Flush', | |
'Full House', | |
'Four of a Kind', | |
'Straight Flush' | |
) | |
class Card: | |
def __init__(self, value_suit): | |
value, suit = value_suit | |
if value not in VALUES: | |
raise ValueError | |
self.value = value | |
if suit not in SUITS: | |
raise ValueError | |
self.suit = suit | |
def __repr__(self): | |
return '<Card object {}{}>'.format(self.value, self.suit) | |
def __eq__(self, other): | |
return self.value == other.value | |
def __lt__(self, other): | |
return self.value_index < other.value_index | |
def __gt__(self, other): | |
return self.value_index > other.value_index | |
@property | |
def value_index(self): | |
return VALUES.index(self.value) | |
class Deck: | |
def __init__(self): | |
cards = [Card('{}{}'.format(v, s)) for v, s in product(VALUES, SUITS)] | |
shuffle(cards) | |
self.deck = cards | |
def deal(self, n): | |
return [self.deck.pop() for i in range(n)] | |
class PokerHand: | |
def __init__(self, cards): | |
cards = cards.split(' ') | |
self.cards = [Card(sv) for sv in cards] | |
card_set = {repr(card) for card in cards} | |
duplicate_cards = len(card_set) < len(cards) | |
if duplicate_cards: | |
raise ValueError | |
self.cards = sorted(cards) | |
def __repr__(self): | |
return '<PokerHand object {}>'.format(', '.join(str(card) for card in self)) | |
def __iter__(self): | |
return iter(self.cards) | |
def __gt__(self, other): | |
return self.cards[-1] > other.cards[-1] | |
def __lt__(self, other): | |
return self.cards[-1] < other.cards[-1] | |
def __eq__(self, other): | |
return self.cards[-1] == other.cards[-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from poker import Card, PokerHand, Deck | |
# test single card | |
card_1 = Card('AS') | |
card_2 = Card('AD') | |
assert card_1 == card_2 | |
card_3 = Card('KS') | |
assert card_1 != card_3 | |
assert card_1 > card_3 | |
assert not card_1 < card_3 | |
# test comparing two poker hands | |
hand_1 = PokerHand('AS 2D 4C 7H KS') | |
hand_2 = PokerHand('AC 3D 5C 8H TS') | |
print(hand_1, hand_2) | |
assert hand_1 == hand_2 | |
assert hand_2 == hand_1 | |
hand_1 = PokerHand('AS 2D 4C 7H KS') | |
hand_2 = PokerHand('QS 3D 5C 8H TS') | |
assert hand_1 > hand_2 | |
assert hand_2 < hand_1 | |
# test creating a deck | |
deck = Deck() | |
assert len(deck) == 52 | |
assert all(isinstance(card, Card) for card in deck) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment