✨ 扑克手牌实现
This commit is contained in:
parent
13d5d2e7dc
commit
789abc065a
|
@ -1,12 +1,62 @@
|
|||
package builtin
|
||||
|
||||
func NewPoker() {
|
||||
import "github.com/kercylan98/minotaur/utils/maths"
|
||||
|
||||
func NewPoker[PlayerID comparable](pile *PokerCardPile, options ...PokerOption[PlayerID]) *Poker[PlayerID] {
|
||||
poker := &Poker[PlayerID]{
|
||||
pile: pile,
|
||||
handCards: map[PlayerID][][]PokerCard{},
|
||||
}
|
||||
for _, option := range options {
|
||||
option(poker)
|
||||
}
|
||||
return poker
|
||||
}
|
||||
|
||||
type Poker struct {
|
||||
type Poker[PlayerID comparable] struct {
|
||||
pile *PokerCardPile
|
||||
comparePoint map[PokerPoint]int
|
||||
compareColor map[PokerColor]int
|
||||
handCards map[PlayerID][][]PokerCard
|
||||
}
|
||||
|
||||
func (slf *Poker) Generate() {
|
||||
|
||||
// HandCard 获取玩家特定索引的手牌组
|
||||
func (slf *Poker[PlayerID]) HandCard(playerId PlayerID, index int) []PokerCard {
|
||||
return slf.handCards[playerId][index]
|
||||
}
|
||||
|
||||
// HandCards 获取玩家所有手牌
|
||||
// - 获取结果为多份手牌
|
||||
func (slf *Poker[PlayerID]) HandCards(playerId PlayerID) [][]PokerCard {
|
||||
return slf.handCards[playerId]
|
||||
}
|
||||
|
||||
// HandCardGroupCount 获取玩家共有多少副手牌
|
||||
func (slf *Poker[PlayerID]) HandCardGroupCount(playerId PlayerID) int {
|
||||
return len(slf.handCards[playerId])
|
||||
}
|
||||
|
||||
// GetPile 获取牌堆
|
||||
func (slf *Poker[PlayerID]) GetPile() *PokerCardPile {
|
||||
return slf.pile
|
||||
}
|
||||
|
||||
// Compare 比较两张扑克牌大小
|
||||
func (slf *Poker[PlayerID]) Compare(card1 PokerCard, expression maths.CompareExpression, card2 PokerCard) bool {
|
||||
var point1, point2 int
|
||||
if slf.comparePoint == nil {
|
||||
point1, point2 = int(card1.GetPoint()), int(card2.GetPoint())
|
||||
} else {
|
||||
point1, point2 = slf.comparePoint[card1.GetPoint()], slf.comparePoint[card2.GetPoint()]
|
||||
}
|
||||
if maths.Compare(point1, expression, point2) {
|
||||
return true
|
||||
}
|
||||
var color1, color2 int
|
||||
if slf.comparePoint == nil {
|
||||
color1, color2 = int(card1.GetColor()), int(card2.GetColor())
|
||||
} else {
|
||||
color1, color2 = slf.compareColor[card1.GetColor()], slf.compareColor[card2.GetColor()]
|
||||
}
|
||||
return maths.Compare(color1, expression, color2)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package builtin
|
||||
|
||||
type PokerOption[PlayerID comparable] func(poker *Poker[PlayerID])
|
||||
|
||||
// WithPokerPointOrderOfSize 通过特定的点数大小顺序创建扑克玩法
|
||||
// - 顺序由大到小,points 数组必须包含每一个点数
|
||||
func WithPokerPointOrderOfSize[PlayerID comparable](points [15]PokerPoint) PokerOption[PlayerID] {
|
||||
return func(poker *Poker[PlayerID]) {
|
||||
var compare = make(map[PokerPoint]int)
|
||||
for i, point := range points {
|
||||
compare[point] = len(points) - i
|
||||
}
|
||||
if len(compare) != len(points) {
|
||||
panic("not every point included")
|
||||
}
|
||||
poker.comparePoint = compare
|
||||
}
|
||||
}
|
||||
|
||||
// WithPokerColorOrderOfSize 通过特定的花色大小顺序创建扑克玩法
|
||||
// - 顺序由大到小,colors 数组必须包含每一种花色
|
||||
func WithPokerColorOrderOfSize[PlayerID comparable](colors [4]PokerColor) PokerOption[PlayerID] {
|
||||
return func(poker *Poker[PlayerID]) {
|
||||
var compare = make(map[PokerColor]int)
|
||||
for i, color := range colors {
|
||||
compare[color] = len(colors) - i
|
||||
}
|
||||
if len(compare) != len(colors) {
|
||||
panic("not every color included")
|
||||
}
|
||||
poker.compareColor = compare
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue