From a6ea32205dacefaeda2bbe55d44ff33cff156c89 Mon Sep 17 00:00:00 2001 From: cjensen506 Date: Sat, 2 Nov 2019 18:13:31 +0000 Subject: [PATCH 1/5] adjusted test framework --- test/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/__init__.py diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 From 69e70af616568ea99b1d997a0abf51ee5084cbcf Mon Sep 17 00:00:00 2001 From: cjensen506 Date: Sat, 2 Nov 2019 18:14:32 +0000 Subject: [PATCH 2/5] adjusted test framework --- cribbage/cribbagegame.py | 6 +++--- test/test_cribbagegame.py | 4 ++-- test/test_playingcards.py | 2 +- test/test_scoring.py | 5 +++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cribbage/cribbagegame.py b/cribbage/cribbagegame.py index cdec34f..9d71581 100644 --- a/cribbage/cribbagegame.py +++ b/cribbage/cribbagegame.py @@ -1,8 +1,8 @@ """Cribbage game.""" import random -import scoring -from player import HumanPlayer, RandomPlayer -from playingcards import Deck +from . import scoring +from .player import HumanPlayer, RandomPlayer +from .playingcards import Deck DEBUG = True # Debug flag for debugging output diff --git a/test/test_cribbagegame.py b/test/test_cribbagegame.py index 92c3a38..349e8ad 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 cribbage.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..1e15b92 100644 --- a/test/test_scoring.py +++ b/test/test_scoring.py @@ -1,6 +1,7 @@ import unittest -import scoring -import playingcards as pc +#import cribbage.scoring as scoring +from cribbage import scoring +import cribbage.playingcards as pc class TestPairScoring(unittest.TestCase): From d9d6f0b8f105e68250ac99e09daadda599aa27df Mon Sep 17 00:00:00 2001 From: cjensen506 Date: Sun, 3 Nov 2019 04:48:16 +0000 Subject: [PATCH 3/5] pair scoring fix --- cribbage/scoring.py | 23 ++++++- cribbage/cribbagegame.py => cribbagegame.py | 10 +-- test/test_cribbagegame.py | 2 +- test/test_scoring.py | 73 ++++++++++++++++++--- 4 files changed, 91 insertions(+), 17 deletions(-) rename cribbage/cribbagegame.py => cribbagegame.py (96%) diff --git a/cribbage/scoring.py b/cribbage/scoring.py index 5bbc265..bec7f53 100644 --- a/cribbage/scoring.py +++ b/cribbage/scoring.py @@ -15,7 +15,7 @@ def check(self, hand): raise NotImplementedError -class HasPairTripleQuad(ScoreCondition): +class HasPairTripleQuad_DuringPlay(ScoreCondition): def check(self, cards): description = None pair_rank = "" @@ -38,6 +38,27 @@ def check(self, cards): description = "Double Pair Royal (%s)" % pair_rank return score, description +class HasPairTripleQuad_InHand(ScoreCondition): + def check(self, cards): + description = "" + score = 0 + if len(cards) > 1: + ranks = [card.rank['rank'] for card in 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): diff --git a/cribbage/cribbagegame.py b/cribbagegame.py similarity index 96% rename from cribbage/cribbagegame.py rename to cribbagegame.py index 9d71581..8a8c728 100644 --- a/cribbage/cribbagegame.py +++ b/cribbagegame.py @@ -1,8 +1,8 @@ """Cribbage game.""" import random -from . 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 @@ -202,7 +202,7 @@ 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 @@ -217,7 +217,7 @@ def _score_hand(self, cards): """ score = 0 score_scenarios = [scoring.CountCombinationsEqualToN(n=15), - scoring.HasPairTripleQuad(), scoring.HasStraight_InHand(), scoring.HasFlush()] + scoring.HasPairTripleQuad_InHand(), scoring.HasStraight_InHand(), scoring.HasFlush()] for scenario in score_scenarios: s, desc = scenario.check(cards[:]) score += s diff --git a/test/test_cribbagegame.py b/test/test_cribbagegame.py index 349e8ad..b770eb3 100644 --- a/test/test_cribbagegame.py +++ b/test/test_cribbagegame.py @@ -1,6 +1,6 @@ import unittest from cribbage.player import RandomPlayer -import cribbage.cribbagegame as cribbagegame +import cribbagegame as cribbagegame class TestCribbageBoard(unittest.TestCase): diff --git a/test/test_scoring.py b/test/test_scoring.py index 1e15b92..274402f 100644 --- a/test/test_scoring.py +++ b/test/test_scoring.py @@ -1,25 +1,78 @@ import unittest -#import cribbage.scoring as scoring from cribbage import scoring import cribbage.playingcards as pc +class TestPairScoring_InHand(unittest.TestCase): + def test_pair(self): + s = scoring.HasPairTripleQuad_InHand() + 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'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['three'])) + score, _ = s.check(hand) + self.assertEqual(score, 2) + + def test_two_pair(self): + s = scoring.HasPairTripleQuad_InHand() + 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'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) + score, _ = s.check(hand) + self.assertEqual(score, 4) + + def test_three(self): + s = scoring.HasPairTripleQuad_InHand() + 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'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) + score, _ = s.check(hand) + self.assertEqual(score, 6) + + def test_four(self): + s = scoring.HasPairTripleQuad_InHand() + 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['nine'])) + hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) + score, _ = s.check(hand) + 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])) @@ -30,7 +83,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'])) @@ -38,7 +91,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])) @@ -60,7 +113,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) From ba07d9831a1c326587e43c89868d5eee752516ca Mon Sep 17 00:00:00 2001 From: cjensen506 Date: Sun, 3 Nov 2019 18:58:33 +0000 Subject: [PATCH 4/5] Flush scoring & hand scoring class --- cribbage/scoring.py | 91 +++++++++++++--- cribbagegame.py | 14 +-- test/test_scoring.py | 241 +++++++++++++++++++++---------------------- 3 files changed, 198 insertions(+), 148 deletions(-) diff --git a/cribbage/scoring.py b/cribbage/scoring.py index bec7f53..8403c9f 100644 --- a/cribbage/scoring.py +++ b/cribbage/scoring.py @@ -14,6 +14,21 @@ def __init__(self): def check(self, hand): raise NotImplementedError +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): @@ -38,12 +53,12 @@ def check(self, cards): description = "Double Pair Royal (%s)" % pair_rank return score, description -class HasPairTripleQuad_InHand(ScoreCondition): - def check(self, cards): +class HasPairTripleQuad_InHand(HandScoring): + def check(self): description = "" score = 0 - if len(cards) > 1: - ranks = [card.rank['rank'] for card in cards] + 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: @@ -73,7 +88,7 @@ def check(self, cards): return score, description -class HasStraight_InHand(ScoreCondition): +class HasStraight_InHand(HandScoring): @staticmethod def _enumerate_straights(cards): @@ -96,11 +111,23 @@ def _enumerate_straights(cards): straights_deduped.append(s) return straights_deduped - @classmethod - def check(cls, cards): + # @classmethod + # def check(cls, cards): + # description = "" + # points = 0 + # #cards = self.all_cards + # straights = cls._enumerate_straights(cards) + # for s in straights: + # assert len(s) >= 3, "Straights must be 3 or more cards." + # description += "%d-card straight " % len(s) + # points += len(s) + # return points, description + + 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) @@ -144,11 +171,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/cribbagegame.py b/cribbagegame.py index 8a8c728..54dcdf5 100644 --- a/cribbagegame.py +++ b/cribbagegame.py @@ -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) @@ -209,17 +211,17 @@ def _score_play(self, card_seq): 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_InHand(), 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/test_scoring.py b/test/test_scoring.py index 274402f..160aa0e 100644 --- a/test/test_scoring.py +++ b/test/test_scoring.py @@ -4,47 +4,52 @@ class TestPairScoring_InHand(unittest.TestCase): def test_pair(self): - s = scoring.HasPairTripleQuad_InHand() + 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'])) - hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['three'])) - score, _ = s.check(hand) + 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): - s = scoring.HasPairTripleQuad_InHand() + 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'])) - hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) - score, _ = s.check(hand) + 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): - s = scoring.HasPairTripleQuad_InHand() + 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'])) - hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) - score, _ = s.check(hand) + 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): - s = scoring.HasPairTripleQuad_InHand() + 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['nine'])) - hand.append(pc.Card(suit=pc.Deck.SUITS['spades'], rank=pc.Deck.RANKS['two'])) - score, _ = s.check(hand) + 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_DuringPlay(unittest.TestCase): @@ -261,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): @@ -386,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() From 58100fd2c6ff7983b232cb1dd9771a0aa2a3354f Mon Sep 17 00:00:00 2001 From: cjensen506 Date: Sun, 8 Dec 2019 17:57:09 +0000 Subject: [PATCH 5/5] comment clean up --- cribbage/scoring.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/cribbage/scoring.py b/cribbage/scoring.py index 8403c9f..bcc7c0b 100644 --- a/cribbage/scoring.py +++ b/cribbage/scoring.py @@ -110,18 +110,6 @@ def _enumerate_straights(cards): if not subset: straights_deduped.append(s) return straights_deduped - - # @classmethod - # def check(cls, cards): - # description = "" - # points = 0 - # #cards = self.all_cards - # straights = cls._enumerate_straights(cards) - # for s in straights: - # assert len(s) >= 3, "Straights must be 3 or more cards." - # description += "%d-card straight " % len(s) - # points += len(s) - # return points, description def check(self): description = ""