2024-05-18 09:21:41 +00:00
|
|
|
package pokercore
|
|
|
|
|
2024-05-19 00:33:15 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
2024-05-18 09:21:41 +00:00
|
|
|
|
|
|
|
var ErrDuplicateCard = errors.New("cannot score a poker hand out of a set of cards with duplicates")
|
|
|
|
|
|
|
|
// this method makes a n new hands where n is the number of cards in the hand
|
|
|
|
// each of the new hands has one card removed from the original hand
|
|
|
|
// then it calls the identifyBestFiveCardPokerHand method on each of the new hands
|
2024-05-19 00:33:15 +00:00
|
|
|
// and returns the best hand by score. this is recursion and is exponential in time complexity
|
|
|
|
func (hand Cards) IdentifyBestFiveCardPokerHand() (Cards, error) {
|
|
|
|
|
|
|
|
//fmt.Println("hand: ", hand)
|
2024-05-18 09:21:41 +00:00
|
|
|
if hand.containsDuplicates() {
|
|
|
|
return nil, ErrDuplicateCard
|
|
|
|
}
|
|
|
|
|
|
|
|
newHands := make([]Cards, len(hand))
|
|
|
|
for i := 0; i < len(hand); i++ {
|
2024-05-19 00:33:15 +00:00
|
|
|
newHand := make(Cards, len(hand)-1)
|
|
|
|
copy(newHand, hand[:i])
|
|
|
|
copy(newHand[i:], hand[i+1:])
|
|
|
|
//fmt.Printf("generating new subset of hand: %+v\n", newHand)
|
|
|
|
//fmt.Printf("this subset drops the card at index %d: %s\n", i, hand[i].String())
|
2024-05-18 09:21:41 +00:00
|
|
|
newHands[i] = newHand
|
|
|
|
}
|
2024-05-19 00:33:15 +00:00
|
|
|
//fmt.Printf("newHands: %+v\n", newHands)
|
2024-05-18 09:21:41 +00:00
|
|
|
var bestHand Cards
|
|
|
|
var bestScore HandScore
|
|
|
|
|
|
|
|
for _, h := range newHands {
|
|
|
|
if len(h) == 5 {
|
|
|
|
score, _ := h.PokerHandScore()
|
|
|
|
if score > bestScore {
|
|
|
|
bestScore = score
|
|
|
|
bestHand = h
|
|
|
|
}
|
|
|
|
} else {
|
2024-05-19 00:33:15 +00:00
|
|
|
rh, _ := h.IdentifyBestFiveCardPokerHand()
|
2024-05-18 09:21:41 +00:00
|
|
|
score, _ := rh.PokerHandScore()
|
|
|
|
if score > bestScore {
|
|
|
|
bestScore = score
|
|
|
|
bestHand = rh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bestHand, nil
|
|
|
|
}
|