impl some hands
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
type Hand struct {
|
||||
Public []Card
|
||||
Private []Card
|
||||
@@ -15,3 +19,38 @@ func (hand *Hand) Push(card Card) {
|
||||
func (hand Hand) Len() int {
|
||||
return len(hand.Public) + len(hand.Private)
|
||||
}
|
||||
|
||||
func (hand Hand) AllCards() []Card {
|
||||
allcards := make([]Card, 0, len(hand.Public)+len(hand.Private))
|
||||
for _, cards := range [][]Card{hand.Public, hand.Private} {
|
||||
allcards = append(allcards, cards...)
|
||||
}
|
||||
return allcards
|
||||
}
|
||||
|
||||
func (hand Hand) Flush() bool {
|
||||
suit := -1
|
||||
for _, card := range hand.AllCards() {
|
||||
if suit == -1 {
|
||||
suit = card.Suit
|
||||
} else if suit != card.Suit {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return suit != -1
|
||||
}
|
||||
|
||||
func (hand Hand) Straight() bool {
|
||||
cards := hand.AllCards()
|
||||
values := make([]int, 0, len(cards))
|
||||
for _, card := range cards {
|
||||
values = append(values, card.Value)
|
||||
}
|
||||
sort.Ints(values)
|
||||
for i := 1; i < len(values); i++ {
|
||||
if values[i-1]+1 != values[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(values) > 1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user