From 789abc065a71feec89dcabd4a75d98987aaaeb00 Mon Sep 17 00:00:00 2001 From: kercylan98 Date: Wed, 21 Jun 2023 20:03:26 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=89=91=E5=85=8B=E6=89=8B?= =?UTF-8?q?=E7=89=8C=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game/builtin/poker.go | 58 ++++++++++++++++++++++++++++++++--- game/builtin/poker_options.go | 33 ++++++++++++++++++++ 2 files changed, 87 insertions(+), 4 deletions(-) create mode 100644 game/builtin/poker_options.go diff --git a/game/builtin/poker.go b/game/builtin/poker.go index de8fa47..c264b46 100644 --- a/game/builtin/poker.go +++ b/game/builtin/poker.go @@ -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) } diff --git a/game/builtin/poker_options.go b/game/builtin/poker_options.go new file mode 100644 index 0000000..b87608c --- /dev/null +++ b/game/builtin/poker_options.go @@ -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 + } +}