Skip to content

Commit

Permalink
Fix poker calculation and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zooba committed Feb 15, 2022
1 parent 70aed8d commit 14e3248
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install PyTest
run: python -m pip install pytest
- name: Test
run: python deck-tests.py -v
run: pytest deck-tests.py -v

blackify:
runs-on: ubuntu-latest
Expand Down
112 changes: 112 additions & 0 deletions deck-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,117 @@ def test_eq(self):
assert c1 == c2


class PokerHandTests(unittest.TestCase):
def test_high_card(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 7),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.HighCard, deck.Value.Nine)

def test_high_card_ace(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 7),
Card("Spades", "Ace"),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.HighCard, deck.Value.Ace)

def test_pair(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 5),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Pair, deck.Value.Nine, deck.Value.Five)

def test_two_pair(self):
cards = [
Card("Spades", 2),
Card("Clubs", 5),
Card("Hearts", 5),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.TwoPair, deck.Value.Nine, deck.Value.Five)

def test_triples(self):
cards = [
Card("Spades", 2),
Card("Clubs", 4),
Card("Hearts", 9),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.ThreeOfAKind, deck.Value.Nine, deck.Value.Four)

def test_straight(self):
cards = [
Card("Spades", 2),
Card("Clubs", 3),
Card("Hearts", 4),
Card("Diamonds", 5),
Card("Spades", 6),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Straight, deck.Value.Six)

def test_flush(self):
cards = [
Card("Clubs", 2),
Card("Clubs", 4),
Card("Clubs", 5),
Card("Clubs", 7),
Card("Clubs", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.Flush, deck.Value.Nine)

def test_full_house(self):
cards = [
Card("Spades", 4),
Card("Clubs", 4),
Card("Hearts", 9),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.FullHouse, deck.Value.Nine)

def test_fours(self):
cards = [
Card("Spades", 2),
Card("Clubs", 9),
Card("Hearts", 9),
Card("Diamonds", 9),
Card("Spades", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.FourOfAKind, deck.Value.Nine)

def test_straight_flush(self):
cards = [
Card("Clubs", 5),
Card("Clubs", 6),
Card("Clubs", 7),
Card("Clubs", 8),
Card("Clubs", 9),
]
hand = deck.get_poker_hand(cards)
assert hand == (deck.PokerHand.StraightFlush, deck.Value.Nine)


if __name__ == "__main__":
unittest.main()
8 changes: 6 additions & 2 deletions deck.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Implementation of the deck collection type."""

__version__ = "3.0.0rc1"
__version__ = "3.0.0rc2"

import collections
import enum
Expand Down Expand Up @@ -170,7 +170,11 @@ def get_poker_hand(cards):
if of_a_kind == 2 and second_pair == 2:
return (
PokerHand.TwoPair,
*sorted(filter(None, (of_a_kind_card, second_pair_card)), key=aces_high),
*sorted(
filter(None, (of_a_kind_card, second_pair_card)),
reverse=True,
key=aces_high,
),
)
if of_a_kind == 2:
return (PokerHand.Pair, of_a_kind_card) + (
Expand Down

0 comments on commit 14e3248

Please sign in to comment.