diff --git a/cribbage/scoring.py b/cribbage/scoring.py index 5bbc265..bcc7c0b 100644 --- a/cribbage/scoring.py +++ b/cribbage/scoring.py @@ -14,8 +14,23 @@ def __init__(self): def check(self, hand): raise NotImplementedError - -class HasPairTripleQuad(ScoreCondition): +class HandScoring: + score = None + description = "" + def __init__(self, hand_cards, starter_card): + self.hand_cards = hand_cards + self.starter_card = starter_card + #self.is_crib = is_crib + self.all_cards = hand_cards.copy() + self.all_cards.append(starter_card) + +class NinHand(HandScoring): + def check(self): + ncounter = CountCombinationsEqualToN(n=15) + s,description = ncounter.check(self.all_cards) + return s, description + +class HasPairTripleQuad_DuringPlay(ScoreCondition): def check(self, cards): description = None pair_rank = "" @@ -38,6 +53,27 @@ def check(self, cards): description = "Double Pair Royal (%s)" % pair_rank return score, description +class HasPairTripleQuad_InHand(HandScoring): + def check(self): + description = "" + score = 0 + if len(self.all_cards) > 1: + ranks = [card.rank['rank'] for card in self.all_cards] + pos_pair = {x:ranks.count(x) for x in ranks} + for k,v in pos_pair.items(): + if v == 2: + #found a pair + score += 2 + description += "Pair (%s)" % k + elif v == 3: + #found three of a kind + score += 6 + description += "Three of a kind (%s)" % k + elif v == 4: + #found four of a kind + score += 12 + description += "Four of a kind (%s)" % k + return score, description class ExactlyEqualsN(ScoreCondition): @@ -52,7 +88,7 @@ def check(self, cards): return score, description -class HasStraight_InHand(ScoreCondition): +class HasStraight_InHand(HandScoring): @staticmethod def _enumerate_straights(cards): @@ -74,12 +110,12 @@ def _enumerate_straights(cards): if not subset: straights_deduped.append(s) return straights_deduped - - @classmethod - def check(cls, cards): + + def check(self): description = "" points = 0 - straights = cls._enumerate_straights(cards) + #cards = self.all_cards + straights = self._enumerate_straights(self.all_cards) for s in straights: assert len(s) >= 3, "Straights must be 3 or more cards." description += "%d-card straight " % len(s) @@ -123,11 +159,43 @@ def check(self, cards): return score, description -class HasFlush(ScoreCondition): - def check(self, cards): - card_suits = [card.get_suit() for card in cards] - suit_count = card_suits.count(cards[-1].get_suit()) - score = suit_count if suit_count >= 4 else 0 - assert score < 6, "Flush score exceeded 5" - description = "" if score < 4 else ("%d-card flush" % score) - return score, description +class HasFlush(HandScoring): + def __init__(self,hand_cards,starter_card,is_crib): + self.is_crib = is_crib + super().__init__(hand_cards,starter_card) + + def check(self): + if self.is_crib: + card_suits = [card.get_suit() for card in self.hand_cards] + card_suits.append(self.starter_card.get_suit()) + d = {x:card_suits.count(x) for x in card_suits} + if max(d.values()) == 5: + self.score = 5 + self.description = "5-card flush, 5 score" + else: + self.score = 0 + elif not(self.is_crib): + card_suits = [card.get_suit() for card in self.hand_cards] + d = {x:card_suits.count(x) for x in card_suits} + if max(d.values()) == 4: + v = list(d.values()) + k=list(d.keys()) + flush_suit = k[v.index(max(v))] + if flush_suit == self.starter_card.get_suit(): + self.score = 5 + self.description = "5-card flush, 5 score" + else: + self.score = 4 + self.description = "4-card flush, 4 score" + else: + self.score = 0 + else: + raise Exception("Crib or hand not specified") + return self.score, self.description + + #card_suits = [card.get_suit() for card in cards] + #suit_count = card_suits.count(cards[-1].get_suit()) + #score = suit_count if suit_count >= 4 else 0 + #assert score < 6, "Flush score exceeded 5" + #description = "" if score < 4 else ("%d-card flush" % score) + diff --git a/cribbage/cribbagegame.py b/cribbagegame.py similarity index 91% rename from cribbage/cribbagegame.py rename to cribbagegame.py index cdec34f..54dcdf5 100644 --- a/cribbage/cribbagegame.py +++ b/cribbagegame.py @@ -1,8 +1,8 @@ """Cribbage game.""" import random -import scoring -from player import HumanPlayer, RandomPlayer -from playingcards import Deck +from cribbage import scoring +from cribbage.player import HumanPlayer, RandomPlayer +from cribbage.playingcards import Deck DEBUG = True # Debug flag for debugging output @@ -184,13 +184,15 @@ def play(self): for p in self.game.players: p_cards_played = [move['card'] for move in self.table if move['player'] == p] print("Scoring " + str(p) + "'s hand: " + str(p_cards_played + [self.starter])) - score = self._score_hand(cards=p_cards_played + [self.starter]) # Include starter card as part of hand + #score = self._score_hand(cards=p_cards_played + [self.starter]) # Include starter card as part of hand + score = self._score_hand(hand = p_cards_played, s_card = self.starter, is_crib = False) if score: self.game.board.peg(p, score) # Score the crib print("Scoring the crib: " + str(self.crib + [self.starter])) - score = self._score_hand(cards=(self.crib + [self.starter])) # Include starter card as part of crib + #score = self._score_hand(cards=(self.crib + [self.starter])) # Include starter card as part of crib + score = self._score_hand(hand = self.crib, s_card = self.starter, is_crib = True) if score: self.game.board.peg(self.dealer, score) @@ -202,24 +204,24 @@ def _score_play(self, card_seq): """ score = 0 score_scenarios = [scoring.ExactlyEqualsN(n=15), scoring.ExactlyEqualsN(n=31), - scoring.HasPairTripleQuad(), scoring.HasStraight_DuringPlay()] + scoring.HasPairTripleQuad_DuringPlay(), scoring.HasStraight_DuringPlay()] for scenario in score_scenarios: s, desc = scenario.check(card_seq[:]) score += s print("[SCORE] " + desc) if desc else None return score - def _score_hand(self, cards): + def _score_hand(self, hand, s_card, is_crib): """Score a hand at the end of a round. :param cards: Cards in a single player's hand. :return: Points earned by player. """ score = 0 - score_scenarios = [scoring.CountCombinationsEqualToN(n=15), - scoring.HasPairTripleQuad(), scoring.HasStraight_InHand(), scoring.HasFlush()] + score_scenarios = [scoring.NinHand(hand, s_card), + scoring.HasPairTripleQuad_InHand(hand, s_card), scoring.HasStraight_InHand(hand, s_card), scoring.HasFlush(hand, s_card, is_crib)] for scenario in score_scenarios: - s, desc = scenario.check(cards[:]) + s, desc = scenario.check() score += s print("[EOR SCORING] " + desc) if desc else None return score diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/test_cribbagegame.py b/test/test_cribbagegame.py index 92c3a38..b770eb3 100644 --- a/test/test_cribbagegame.py +++ b/test/test_cribbagegame.py @@ -1,6 +1,6 @@ import unittest -from player import RandomPlayer -import cribbagegame +from cribbage.player import RandomPlayer +import cribbagegame as cribbagegame class TestCribbageBoard(unittest.TestCase): diff --git a/test/test_playingcards.py b/test/test_playingcards.py index d8dd717..a02f23e 100644 --- a/test/test_playingcards.py +++ b/test/test_playingcards.py @@ -1,5 +1,5 @@ import unittest -from playingcards import Card, Deck +from cribbage.playingcards import Card, Deck class TestCardClass(unittest.TestCase): diff --git a/test/test_scoring.py b/test/test_scoring.py index 3b81cba..160aa0e 100644 --- a/test/test_scoring.py +++ b/test/test_scoring.py @@ -1,24 +1,83 @@ import unittest -import scoring -import playingcards as pc +from cribbage import scoring +import cribbage.playingcards as pc +class TestPairScoring_InHand(unittest.TestCase): + def test_pair(self): + + hand = [] + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['seven'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])) + s_card = pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['three']) + + s = scoring.HasPairTripleQuad_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 2) + + def test_two_pair(self): + + hand = [] + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['seven'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])) + s_card = pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two']) + s = scoring.HasPairTripleQuad_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 4) + + def test_three(self): + + hand = [] + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ace'])) + s_card = pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two']) + s = scoring.HasPairTripleQuad_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 6) + + def test_four(self): + + hand = [] + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])) + s_card = pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['nine']) + s = scoring.HasPairTripleQuad_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 12) -class TestPairScoring(unittest.TestCase): +class TestPairScoring_DuringPlay(unittest.TestCase): def setUp(self): pass - def test_pair_pair(self): - s = scoring.HasPairTripleQuad() + def test_pair(self): + s = scoring.HasPairTripleQuad_DuringPlay() hand = [] - for i in pc.Deck.RANKS.keys(): - hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS[i])) hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) score, _ = s.check(hand) self.assertEqual(score, 2) + def test_pair_old(self): + s = scoring.HasPairTripleQuad_DuringPlay() + hand = [] + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four'])) + score, _ = s.check(hand) + self.assertEqual(score, 0) + def test_pair_triple(self): - s = scoring.HasPairTripleQuad() + s = scoring.HasPairTripleQuad_DuringPlay() hand = [] for i in pc.Deck.RANKS.keys(): hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS[i])) @@ -29,7 +88,7 @@ def test_pair_triple(self): self.assertEqual(score, 6) def test_pair_quadruple(self): - s = scoring.HasPairTripleQuad() + s = scoring.HasPairTripleQuad_DuringPlay() hand = [] for i in range(6): hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])) @@ -37,7 +96,7 @@ def test_pair_quadruple(self): self.assertEqual(score, 12) def test_pair_nothing(self): - s = scoring.HasPairTripleQuad() + s = scoring.HasPairTripleQuad_DuringPlay() hand = [] for i in pc.Deck.RANKS.keys(): hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS[i])) @@ -59,7 +118,7 @@ def test_pair_nothing(self): self.assertEqual(score, 0) def test_pair_minimumcards(self): - s = scoring.HasPairTripleQuad() + s = scoring.HasPairTripleQuad_DuringPlay() hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine'])] score, _ = s.check(hand) @@ -207,94 +266,55 @@ def test_HasStraight_DuringPlay_12card(self): class TestHasStraight_InHand(unittest.TestCase): - def setUp(self): - self.s = scoring.HasStraight_InHand() - - def test_HasStraight_InHand_2card(self): - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), - pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 0) - def test_HasStraight_InHand_3card(self): hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), - pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 3) - - def test_HasStraight_InHand_4card(self): - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five']), pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['seven'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 4) - - def test_HasStraight_InHand_Double_4_Straight(self): - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), + pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['ace'])] + s_card = pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['king']) + s = scoring.HasStraight_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 3) + + def test_HasStraight_InHand_5card(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five']), pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['seven']), - pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['five'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 8) - - def test_HasStraight_InHand_6card_outoforder(self): - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), + pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['three'])] + s_card = pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['seven']) + s = scoring.HasStraight_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 5) + + def test_HasStraight_InHand_double3(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five']), pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['seven']), - pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['three'])] - score, _ = self.s.check(hand) + pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['four'])] + s_card = pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['king']) + s = scoring.HasStraight_InHand(hand,s_card) + score, _ = s.check() self.assertEqual(score, 6) - - def test_HasStraight_InHand_4card_broken(self): + + def test_HasStraight_InHand_tripple3(self): hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five']), pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['seven']), - pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['two'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 4) - - def test_HasStraight_InHand_ThreePairs(self): - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), - pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['four']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['five']), + pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['four'])] + s_card = pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['four']) + s = scoring.HasStraight_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 9) + + def test_HasStraight_InHand_double4(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['three']), pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['five']), pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['two'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 24) - - def test_HasStraight_InHand_NoStraight(self): - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two']), - pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['four']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['six']), - pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['eight']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['ten']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['queen'])] - score, _ = self.s.check(hand) - self.assertEqual(score, 0) - - def test_HasStraight_InHand_EmptyHand(self): - hand = [] - score, _ = self.s.check(hand) - self.assertEqual(score, 0) - - def test_HasStraight_InHand_12card(self): - hand = [] - for rank in pc.Deck.RANKS: - hand.append(pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS[rank])) - score, _ = self.s.check(hand) - self.assertEqual(score, 13) - - + pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['four'])] + s_card = pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['four']) + s = scoring.HasStraight_InHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 8) class TestCountCombinationsEqualToN(unittest.TestCase): def setUp(self): @@ -332,66 +352,89 @@ class TestHasFlush(unittest.TestCase): def setUp(self): pass - def test_HasFlush_four_card_flush(self): - s = scoring.HasFlush() + def test_noflush_four_card_crib(self): hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ace']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack'])] - score, _ = s.check(hand) - self.assertEqual(score, 4) - - def test_HasFlush_five_card_flush(self): - s = scoring.HasFlush() - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ace']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])] - score, _ = s.check(hand) - self.assertEqual(score, 5) - - def test_HasFlush_three_card_non_flush(self): - s = scoring.HasFlush() - hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['two'])] - score, _ = s.check(hand) + s_card = pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['nine']) + s = scoring.HasFlush(hand,s_card,True) + score, _ = s.check() self.assertEqual(score, 0) - def test_HasFlush_four_card_old_flush(self): - """Tests to make sure latest card must be part of flush""" - s = scoring.HasFlush() + def test_flush_crib(self): hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ace']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['two'])] - score, _ = s.check(hand) - self.assertEqual(score, 0) + pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack'])] + s_card = pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']) + s = scoring.HasFlush(hand,s_card,True) + score, _ = s.check() + self.assertEqual(score, 5) - def test_HasFlush_four_card_split_flush(self): - s = scoring.HasFlush() + def test_flush_four_card_hand(self): hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ace']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['two']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack'])] - score, _ = s.check(hand) + s_card = pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['nine']) + s = scoring.HasFlush(hand,s_card,False) + score, _ = s.check() self.assertEqual(score, 4) - - def test_HasFlush_five_card_split_flush(self): - s = scoring.HasFlush() + + def test_flush_five_card_hand(self): hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ace']), - pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['king']), - pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['ace']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), - pc.Card(suit=pc.Deck.SUITS['diamonds'], rank=pc.Deck.RANKS['two']), pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack'])] - score, _ = s.check(hand) + s_card = pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']) + s = scoring.HasFlush(hand,s_card,False) + score, _ = s.check() self.assertEqual(score, 5) + + def test_noflush_hand(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['nine']), + pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['ace']), + pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), + pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['jack'])] + s_card = pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']) + s = scoring.HasFlush(hand,s_card,False) + score, _ = s.check() + self.assertEqual(score, 0) +class test_15_hand(unittest.TestCase): + def setUp(self): + pass + + def test_no_15(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['four']), + pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['ace']), + pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), + pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['ace'])] + s_card = pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['three']) + s = scoring.NinHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 0) + + def test_single_15(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['three']), + pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['ace']), + pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']), + pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['jack'])] + s_card = pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['three']) + s = scoring.NinHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 2) + + def test_multiple_15(self): + hand = [pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['jack']), + pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['queen']), + pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['ten']), + pc.Card(suit=pc.Deck.SUITS['clubs'], rank=pc.Deck.RANKS['five'])] + s_card = pc.Card(suit=pc.Deck.SUITS['hearts'], rank=pc.Deck.RANKS['five']) + s = scoring.NinHand(hand,s_card) + score, _ = s.check() + self.assertEqual(score, 12) if __name__ == '__main__': unittest.main()