2024-05-18 09:21:41 +00:00
|
|
|
package pokercore
|
|
|
|
|
2024-05-19 05:10:53 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
2024-05-18 09:21:41 +00:00
|
|
|
|
|
|
|
type PokerHandType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
HighCard PokerHandType = iota
|
|
|
|
Pair
|
|
|
|
TwoPair
|
|
|
|
ThreeOfAKind
|
|
|
|
Straight
|
|
|
|
Flush
|
|
|
|
FullHouse
|
|
|
|
FourOfAKind
|
|
|
|
StraightFlush
|
|
|
|
RoyalFlush
|
|
|
|
)
|
|
|
|
|
|
|
|
type PokerHand struct {
|
|
|
|
Hand Cards
|
|
|
|
Type PokerHandType
|
|
|
|
Score HandScore
|
|
|
|
}
|
|
|
|
|
2024-05-19 00:33:15 +00:00
|
|
|
func (c Cards) Append(other Cards) Cards {
|
|
|
|
return append(c, other...)
|
|
|
|
}
|
|
|
|
|
2024-05-18 09:21:41 +00:00
|
|
|
func (c Cards) PokerHand() (*PokerHand, error) {
|
|
|
|
if c.containsDuplicates() {
|
2024-05-19 05:10:53 +00:00
|
|
|
return nil, errors.New("hand must have no duplicates to be scored as a poker hand")
|
|
|
|
}
|
2024-05-18 09:21:41 +00:00
|
|
|
|
|
|
|
ph := new(PokerHand)
|
|
|
|
|
2024-05-19 05:23:14 +00:00
|
|
|
// IdentifyBestFiveCardPokerHand() calls us to score hands
|
|
|
|
// but only if the hand is 5 cards exactly which avoids recursion loop
|
|
|
|
if len(c) > 5 {
|
|
|
|
phc, err := c.IdentifyBestFiveCardPokerHand()
|
|
|
|
if err != nil {
|
|
|
|
// this should in theory never happen
|
|
|
|
return nil, err
|
2024-05-19 00:33:15 +00:00
|
|
|
}
|
2024-05-19 05:23:14 +00:00
|
|
|
ph.Hand = phc.SortByRankAscending()
|
|
|
|
} else if len(c) == 5 {
|
|
|
|
ph.Hand = c.SortByRankAscending()
|
|
|
|
} else {
|
|
|
|
return nil, errors.New("hand must have at least 5 cards to be scored as a poker hand")
|
2024-05-18 09:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 05:23:14 +00:00
|
|
|
// this doesn't return an error because we've already checked for
|
|
|
|
// duplicates and we have already identified the best 5-card hand
|
2024-05-18 09:21:41 +00:00
|
|
|
|
2024-05-19 05:23:14 +00:00
|
|
|
// this code used to be here, but got factored out so it can go in
|
|
|
|
// scoring.go
|
|
|
|
ph.calculateScore()
|
2024-05-18 09:21:41 +00:00
|
|
|
|
|
|
|
return ph, nil
|
|
|
|
}
|
|
|
|
|
2024-05-19 00:33:15 +00:00
|
|
|
func (ph PokerHand) ToSortedCards() Cards {
|
2024-05-19 05:23:14 +00:00
|
|
|
// I believe ph.Hand is already sorted, but in any case it's only 5
|
|
|
|
// cards and sorting it costs ~nothing
|
|
|
|
return ph.Hand.SortByRankAscending()
|
2024-05-19 00:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ph PokerHand) Compare(other PokerHand) int {
|
|
|
|
if ph.Score > other.Score {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if ph.Score < other.Score {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ph PokerHand) HighestRank() Rank {
|
|
|
|
return ph.Hand.HighestRank()
|
2024-05-18 09:21:41 +00:00
|
|
|
}
|
|
|
|
|
2024-05-19 00:33:15 +00:00
|
|
|
func (ph PokerHand) String() string {
|
|
|
|
return fmt.Sprintf("<PokerHand: %s (%s)>", ph.Hand.String(), ph.Description())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c PokerHand) Description() string {
|
2024-05-18 09:21:41 +00:00
|
|
|
if c.Type == RoyalFlush {
|
|
|
|
return fmt.Sprintf("a royal flush in %s", c.Hand[0].Suit)
|
|
|
|
}
|
|
|
|
if c.Hand.containsStraightFlush() {
|
2024-05-19 05:23:14 +00:00
|
|
|
if c.Hand[3].Rank == FIVE && c.Hand[4].Rank == ACE {
|
2024-05-19 00:33:15 +00:00
|
|
|
// special case for steel wheel
|
2024-05-19 05:23:14 +00:00
|
|
|
return fmt.Sprintf("%s high straight flush in %s", c.Hand[3].Rank.WithArticle(), c.Hand[4].Suit)
|
2024-05-19 00:33:15 +00:00
|
|
|
}
|
2024-05-19 05:23:14 +00:00
|
|
|
return fmt.Sprintf("%s high straight flush in %s", c.HighestRank().WithArticle(), c.Hand[4].Suit)
|
2024-05-18 09:21:41 +00:00
|
|
|
}
|
|
|
|
if c.Hand.containsFourOfAKind() {
|
|
|
|
return fmt.Sprintf("four %s with %s", c.Hand.fourOfAKindRank().Pluralize(), c.Hand.fourOfAKindKicker().Rank.WithArticle())
|
|
|
|
}
|
|
|
|
if c.Hand.containsFullHouse() {
|
|
|
|
return fmt.Sprintf("a full house, %s full of %s", c.Hand.fullHouseTripsRank().Pluralize(), c.Hand.fullHousePairRank().Pluralize())
|
|
|
|
}
|
|
|
|
if c.Hand.containsFlush() {
|
2024-05-19 05:23:14 +00:00
|
|
|
return fmt.Sprintf("%s high flush in %s", c.HighestRank().WithArticle(), c.Hand[4].Suit)
|
2024-05-18 09:21:41 +00:00
|
|
|
}
|
|
|
|
if c.Hand.containsStraight() {
|
2024-05-19 05:23:14 +00:00
|
|
|
if c.Hand[3].Rank == FIVE && c.Hand[4].Rank == ACE {
|
2024-05-19 00:33:15 +00:00
|
|
|
// special case for wheel straight
|
2024-05-19 05:23:14 +00:00
|
|
|
return fmt.Sprintf("%s high straight", c.Hand[3].Rank.WithArticle())
|
2024-05-19 00:33:15 +00:00
|
|
|
}
|
2024-05-18 09:21:41 +00:00
|
|
|
return fmt.Sprintf("%s high straight", c.HighestRank().WithArticle())
|
|
|
|
}
|
|
|
|
if c.Hand.containsThreeOfAKind() {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"three %s with %s and %s",
|
|
|
|
c.Hand.threeOfAKindTripsRank().Pluralize(),
|
|
|
|
c.Hand.threeOfAKindFirstKicker().Rank.WithArticle(),
|
|
|
|
c.Hand.threeOfAKindSecondKicker().Rank.WithArticle(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if c.Hand.containsTwoPair() {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"two pair, %s and %s with %s",
|
|
|
|
c.Hand.twoPairBiggestPair().Pluralize(),
|
|
|
|
c.Hand.twoPairSmallestPair().Pluralize(),
|
|
|
|
c.Hand.twoPairKicker().Rank.WithArticle(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if c.Hand.containsPair() {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"a pair of %s with %s, %s, and %s",
|
|
|
|
c.Hand.pairRank().Pluralize(),
|
|
|
|
c.Hand.pairFirstKicker().Rank.WithArticle(),
|
|
|
|
c.Hand.pairSecondKicker().Rank.WithArticle(),
|
|
|
|
c.Hand.pairThirdKicker().Rank.WithArticle(),
|
|
|
|
)
|
|
|
|
}
|
2024-05-19 00:33:15 +00:00
|
|
|
|
2024-05-18 09:21:41 +00:00
|
|
|
return fmt.Sprintf(
|
|
|
|
// "ace high with an eight, a seven, a six, and a deuce"
|
|
|
|
"%s high with %s, %s, %s, and %s",
|
2024-05-19 05:23:14 +00:00
|
|
|
c.Hand[4].Rank,
|
|
|
|
c.Hand[3].Rank.WithArticle(),
|
|
|
|
c.Hand[2].Rank.WithArticle(),
|
|
|
|
c.Hand[1].Rank.WithArticle(),
|
|
|
|
c.Hand[0].Rank.WithArticle(),
|
2024-05-18 09:21:41 +00:00
|
|
|
)
|
|
|
|
}
|