remove some guardrails from internal methods now that tests are passing

This commit is contained in:
Jeffrey Paul 2024-05-18 22:31:55 -07:00
parent f527c6eb07
commit 79cf2fd9aa
1 changed files with 9 additions and 61 deletions

View File

@ -1,9 +1,12 @@
package pokercore 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 { func (c Cards) pairRank() Rank {
if !c.containsPair() {
panic("hand must have a pair to have a pair rank")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank { if sorted[0].Rank == sorted[1].Rank {
return sorted[0].Rank return sorted[0].Rank
@ -21,9 +24,6 @@ func (c Cards) pairRank() Rank {
} }
func (c Cards) pairFirstKicker() Card { func (c Cards) pairFirstKicker() Card {
if !c.containsPair() {
panic("hand must have a pair to have a first kicker")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank { if sorted[0].Rank == sorted[1].Rank {
return sorted[4] return sorted[4]
@ -41,9 +41,6 @@ func (c Cards) pairFirstKicker() Card {
} }
func (c Cards) pairSecondKicker() Card { func (c Cards) pairSecondKicker() Card {
if !c.containsPair() {
panic("hand must have a pair to have a second kicker")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank { if sorted[0].Rank == sorted[1].Rank {
// first kicker is [4] // first kicker is [4]
@ -65,9 +62,6 @@ func (c Cards) pairSecondKicker() Card {
} }
func (c Cards) pairThirdKicker() Card { func (c Cards) pairThirdKicker() Card {
if !c.containsPair() {
panic("hand must have a pair to have a third kicker")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank { if sorted[0].Rank == sorted[1].Rank {
// first kicker is [4] // first kicker is [4]
@ -93,9 +87,6 @@ func (c Cards) pairThirdKicker() Card {
} }
func (c Cards) twoPairBiggestPair() Rank { func (c Cards) twoPairBiggestPair() Rank {
if !c.containsTwoPair() {
panic("hand must have two pair to have a biggest pair")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank {
return sorted[2].Rank return sorted[2].Rank
@ -111,9 +102,6 @@ func (c Cards) twoPairBiggestPair() Rank {
} }
func (c Cards) twoPairSmallestPair() Rank { func (c Cards) twoPairSmallestPair() Rank {
if !c.containsTwoPair() {
panic("hand must have two pair to have a smallest pair")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank {
return sorted[0].Rank return sorted[0].Rank
@ -128,9 +116,6 @@ func (c Cards) twoPairSmallestPair() Rank {
} }
func (c Cards) twoPairKicker() Card { func (c Cards) twoPairKicker() Card {
if !c.containsTwoPair() {
panic("hand must have two pair to have a twoPairKicker")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank {
return sorted[4] return sorted[4]
@ -145,9 +130,6 @@ func (c Cards) twoPairKicker() Card {
} }
func (c Cards) threeOfAKindTripsRank() Rank { func (c Cards) threeOfAKindTripsRank() Rank {
if !c.containsThreeOfAKind() {
panic("hand must have three of a kind to have a trips rank")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank {
return sorted[0].Rank return sorted[0].Rank
@ -162,9 +144,6 @@ func (c Cards) threeOfAKindTripsRank() Rank {
} }
func (c Cards) threeOfAKindKickers() Cards { func (c Cards) threeOfAKindKickers() Cards {
if !c.containsThreeOfAKind() {
panic("hand must have three of a kind to have kickers")
}
sorted := c.SortByRankAscending() sorted := c.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank {
return Cards{sorted[3], sorted[4]} return Cards{sorted[3], sorted[4]}
@ -255,14 +234,7 @@ func (hand Cards) containsDuplicates() bool {
return false return false
} }
func (hand Cards) IsFiveCardPokerHand() bool {
return len(hand) == 5 && !hand.containsDuplicates()
}
func (hand Cards) containsFlush() bool { func (hand Cards) containsFlush() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
suit := hand[0].Suit suit := hand[0].Suit
for i := 1; i < len(hand); i++ { for i := 1; i < len(hand); i++ {
if hand[i].Suit != suit { if hand[i].Suit != suit {
@ -273,9 +245,6 @@ func (hand Cards) containsFlush() bool {
} }
func (hand Cards) containsStraight() bool { func (hand Cards) containsStraight() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
sorted := hand.SortByRankAscending() sorted := hand.SortByRankAscending()
if sorted[4].Rank == ACE && sorted[3].Rank == FIVE { if sorted[4].Rank == ACE && sorted[3].Rank == FIVE {
@ -293,9 +262,8 @@ func (hand Cards) containsStraight() bool {
} }
func (hand Cards) containsStraightFlush() bool { func (hand Cards) containsStraightFlush() bool {
if !hand.IsFiveCardPokerHand() { // this of course only works on five card hands
panic("hand must have 5 cards to be scored") // but these hand helpers are only ever called from a five card hand
}
if hand.containsStraight() && hand.containsFlush() { if hand.containsStraight() && hand.containsFlush() {
return true return true
} }
@ -303,9 +271,6 @@ func (hand Cards) containsStraightFlush() bool {
} }
func (hand Cards) containsRoyalFlush() 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 // 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. // and the highest ranked card in five-high straigh flush is an ace.
//if hand.containsStraightFlush() && hand.HighestRank() == ACE { //if hand.containsStraightFlush() && hand.HighestRank() == ACE {
@ -319,9 +284,6 @@ func (hand Cards) containsRoyalFlush() bool {
} }
func (hand Cards) containsFourOfAKind() bool { func (hand Cards) containsFourOfAKind() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
sorted := hand.SortByRankAscending() sorted := hand.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank && sorted[2].Rank == sorted[3].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank && sorted[2].Rank == sorted[3].Rank {
// the quads precede the kicker // the quads precede the kicker
@ -335,9 +297,6 @@ func (hand Cards) containsFourOfAKind() bool {
} }
func (hand Cards) containsFullHouse() bool { func (hand Cards) containsFullHouse() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
sorted := hand.SortByRankAscending() sorted := hand.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank && sorted[3].Rank == sorted[4].Rank { 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 { func (hand Cards) containsPair() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
sorted := hand.SortByRankAscending() sorted := hand.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank { if sorted[0].Rank == sorted[1].Rank {
return true return true
@ -372,9 +328,6 @@ func (hand Cards) containsPair() bool {
} }
func (hand Cards) containsThreeOfAKind() bool { func (hand Cards) containsThreeOfAKind() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
sorted := hand.SortByRankAscending() sorted := hand.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[1].Rank == sorted[2].Rank {
return true return true
@ -389,9 +342,6 @@ func (hand Cards) containsThreeOfAKind() bool {
} }
func (hand Cards) containsTwoPair() bool { func (hand Cards) containsTwoPair() bool {
if !hand.IsFiveCardPokerHand() {
panic("hand must have 5 cards to be scored")
}
sorted := hand.SortByRankAscending() sorted := hand.SortByRankAscending()
if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank { if sorted[0].Rank == sorted[1].Rank && sorted[2].Rank == sorted[3].Rank {
return true return true
@ -406,8 +356,6 @@ func (hand Cards) containsTwoPair() bool {
} }
func (hand Cards) isUnmadeHand() bool { func (hand Cards) isUnmadeHand() bool {
if !hand.IsFiveCardPokerHand() { // i suspect this is expensive but we use it only in tests
panic("hand must have 5 cards to be scored")
}
return !hand.containsPair() && !hand.containsTwoPair() && !hand.containsThreeOfAKind() && !hand.containsStraight() && !hand.containsFlush() && !hand.containsFullHouse() && !hand.containsFourOfAKind() && !hand.containsStraightFlush() && !hand.containsRoyalFlush() return !hand.containsPair() && !hand.containsTwoPair() && !hand.containsThreeOfAKind() && !hand.containsStraight() && !hand.containsFlush() && !hand.containsFullHouse() && !hand.containsFourOfAKind() && !hand.containsStraightFlush() && !hand.containsRoyalFlush()
} }