From 79cf2fd9aac1ec9f8df0a495ac7a3ec590432c1a Mon Sep 17 00:00:00 2001 From: sneak Date: Sat, 18 May 2024 22:31:55 -0700 Subject: [PATCH] remove some guardrails from internal methods now that tests are passing --- handhelpers.go | 70 +++++++------------------------------------------- 1 file changed, 9 insertions(+), 61 deletions(-) diff --git a/handhelpers.go b/handhelpers.go index 386a703..0ed8365 100644 --- a/handhelpers.go +++ b/handhelpers.go @@ -1,9 +1,12 @@ package pokercore +// these helper functions are used in a Cards.PokerHand() constructor and in +// the calculateScore() function called from it. +// they only work with exactly five cards, no duplicates, and no wild cards. +// they are not intended to be used in any other context, which is why +// they are not exported + func (c Cards) pairRank() Rank { - if !c.containsPair() { - panic("hand must have a pair to have a pair rank") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank { return sorted[0].Rank @@ -21,9 +24,6 @@ func (c Cards) pairRank() Rank { } func (c Cards) pairFirstKicker() Card { - if !c.containsPair() { - panic("hand must have a pair to have a first kicker") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank { return sorted[4] @@ -41,9 +41,6 @@ func (c Cards) pairFirstKicker() Card { } func (c Cards) pairSecondKicker() Card { - if !c.containsPair() { - panic("hand must have a pair to have a second kicker") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank { // first kicker is [4] @@ -65,9 +62,6 @@ func (c Cards) pairSecondKicker() Card { } func (c Cards) pairThirdKicker() Card { - if !c.containsPair() { - panic("hand must have a pair to have a third kicker") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank { // first kicker is [4] @@ -93,9 +87,6 @@ func (c Cards) pairThirdKicker() Card { } func (c Cards) twoPairBiggestPair() Rank { - if !c.containsTwoPair() { - panic("hand must have two pair to have a biggest pair") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { return sorted[2].Rank @@ -111,9 +102,6 @@ func (c Cards) twoPairBiggestPair() Rank { } func (c Cards) twoPairSmallestPair() Rank { - if !c.containsTwoPair() { - panic("hand must have two pair to have a smallest pair") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { return sorted[0].Rank @@ -128,9 +116,6 @@ func (c Cards) twoPairSmallestPair() Rank { } func (c Cards) twoPairKicker() Card { - if !c.containsTwoPair() { - panic("hand must have two pair to have a twoPairKicker") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { return sorted[4] @@ -145,9 +130,6 @@ func (c Cards) twoPairKicker() Card { } func (c Cards) threeOfAKindTripsRank() Rank { - if !c.containsThreeOfAKind() { - panic("hand must have three of a kind to have a trips rank") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank { return sorted[0].Rank @@ -162,9 +144,6 @@ func (c Cards) threeOfAKindTripsRank() Rank { } func (c Cards) threeOfAKindKickers() Cards { - if !c.containsThreeOfAKind() { - panic("hand must have three of a kind to have kickers") - } sorted := c.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank { return Cards{sorted[3], sorted[4]} @@ -255,14 +234,7 @@ func (hand Cards) containsDuplicates() bool { return false } -func (hand Cards) IsFiveCardPokerHand() bool { - return len(hand) == 5 && !hand.containsDuplicates() -} - func (hand Cards) containsFlush() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } suit := hand[0].Suit for i := 1; i < len(hand); i++ { if hand[i].Suit != suit { @@ -273,9 +245,6 @@ func (hand Cards) containsFlush() bool { } func (hand Cards) containsStraight() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } sorted := hand.SortByRankAscending() if sorted[4].Rank == ACE && sorted[3].Rank == FIVE { @@ -293,9 +262,8 @@ func (hand Cards) containsStraight() bool { } func (hand Cards) containsStraightFlush() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } + // this of course only works on five card hands + // but these hand helpers are only ever called from a five card hand if hand.containsStraight() && hand.containsFlush() { return true } @@ -303,9 +271,6 @@ func (hand Cards) containsStraightFlush() bool { } func (hand Cards) containsRoyalFlush() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } // This seems like it works, but a five-high straight flush is not a royal flush // and the highest ranked card in five-high straigh flush is an ace. //if hand.containsStraightFlush() && hand.HighestRank() == ACE { @@ -319,9 +284,6 @@ func (hand Cards) containsRoyalFlush() bool { } func (hand Cards) containsFourOfAKind() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } sorted := hand.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank && sorted[2].Rank == sorted[3].Rank { // the quads precede the kicker @@ -335,9 +297,6 @@ func (hand Cards) containsFourOfAKind() bool { } func (hand Cards) containsFullHouse() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } sorted := hand.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank && sorted[3].Rank == sorted[4].Rank { @@ -352,9 +311,6 @@ func (hand Cards) containsFullHouse() bool { } func (hand Cards) containsPair() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } sorted := hand.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank { return true @@ -372,9 +328,6 @@ func (hand Cards) containsPair() bool { } func (hand Cards) containsThreeOfAKind() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } sorted := hand.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank { return true @@ -389,9 +342,6 @@ func (hand Cards) containsThreeOfAKind() bool { } func (hand Cards) containsTwoPair() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } sorted := hand.SortByRankAscending() if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { return true @@ -406,8 +356,6 @@ func (hand Cards) containsTwoPair() bool { } func (hand Cards) isUnmadeHand() bool { - if !hand.IsFiveCardPokerHand() { - panic("hand must have 5 cards to be scored") - } + // i suspect this is expensive but we use it only in tests return !hand.containsPair() && !hand.containsTwoPair() && !hand.containsThreeOfAKind() && !hand.containsStraight() && !hand.containsFlush() && !hand.containsFullHouse() && !hand.containsFourOfAKind() && !hand.containsStraightFlush() && !hand.containsRoyalFlush() }