game_sync/gamerule/thirteen/poker.go

195 lines
3.5 KiB
Go

package thirteen
import (
"fmt"
"math/rand"
"strings"
)
//牌序- A, K, Q, J,10, 9, 8, 7, 6, 5, 4, 3, 2
//黑桃-51,50,49,48,47,46,45,44,43,42,41,40,39
//红桃-38,37,36,35,34,33,32,31,30,29,28,27,26
//梅花-25,24,23,22,21,20,19,18,17,16,15,14,13
//方片-12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
const (
PokersTypeZero = 0
PokersTypeFive = 1 //五同
PokersTypeStraightFlush = 2 //同花顺
PokersTypeFour = 3 //铁支
PokersTypeFullHouse = 4 //葫芦
PokersTypeFlush = 5 //同花
PokersTypeStraight = 6 //顺子
PokersTypeThree = 7 //三条
PokersTypeTwoPairs = 8 //两对
PokersTypePair = 9 //对子
PokersTypeOne = 10 //乌龙
PokersTypeMax = 11
)
var PokersTypeName = map[int]string{
PokersTypeZero: "-",
PokersTypeFive: "五同",
PokersTypeStraightFlush: "同花顺",
PokersTypeFour: "铁支",
PokersTypeFullHouse: "葫芦",
PokersTypeFlush: "同花",
PokersTypeStraight: "顺子",
PokersTypeThree: "三条",
PokersTypeTwoPairs: "两对",
PokersTypePair: "对子",
PokersTypeOne: "乌龙",
}
// ToPoint 获取牌点数
func ToPoint(c int) int {
if c > 53 || c < 0 {
return -1
}
if c == 52 {
return 14
}
if c == 53 {
return 15
}
r := c % 13
if r < 12 {
return r + 2
}
if r == 12 {
return 1
}
return r
}
// ToLogic 获取逻辑点数
func ToLogic(c int) int {
if c > 53 || c < 0 {
return -1
}
if c == 52 {
return 13
}
if c == 53 {
return 14
}
return c % 13
}
// ToColor 获取花色
func ToColor(c int) int {
if c > 51 || c < 0 {
return -1
}
return c / 13
}
func PokerName(n int) string {
var AIRecordMap = map[int]string{
0: "2",
1: "3",
2: "4",
3: "5",
4: "6",
5: "7",
6: "8",
7: "9",
8: "T",
9: "J",
10: "Q",
11: "K",
12: "A",
52: "w", // 小王
53: "W", // 大王
}
if n == -1 {
return "-1"
}
color := ""
switch n / 13 {
case 0:
color = "♦"
case 1:
color = "♣"
case 2:
color = "♥"
case 3:
color = "♠"
}
if n > 51 {
return fmt.Sprint(AIRecordMap[n])
}
return fmt.Sprint(color, AIRecordMap[n%13])
}
func PokersShow(cards []int) string {
buf := strings.Builder{}
for _, v := range cards {
buf.WriteString(PokerName(v))
buf.WriteString(",")
}
return buf.String()
}
// Pokers 牌堆
type Pokers struct {
N int // 几副牌
HasKing bool // 有没有大小王
AllCards []int
}
func NewPokers(n int, hasKing bool) *Pokers {
p := &Pokers{
N: n,
HasKing: hasKing,
}
count := n * 52
if hasKing {
count += n * 2
}
p.AllCards = make([]int, 0, count)
p.Init()
return p
}
// Init 初始化牌
func (this *Pokers) Init() {
this.AllCards = this.AllCards[:0]
num := 52 // 一副牌几张牌
if this.HasKing {
num += 2
}
for k := 0; k < this.N; k++ {
for i := 0; i < num; i++ {
this.AllCards = append(this.AllCards, i)
}
}
rand.Shuffle(len(this.AllCards), func(i, j int) {
this.AllCards[i], this.AllCards[j] = this.AllCards[j], this.AllCards[i]
})
}
// Get13Crads 单人获取13张牌
func (this *Pokers) Get13Crads() (cards [13]int) {
if len(this.AllCards) < 13 {
this.Init()
}
for i := 0; i < 13; i++ {
cards[i] = this.Next()
}
return
}
// Next 获取单张牌
func (this *Pokers) Next() int {
if len(this.AllCards) == 0 {
this.Init()
}
c := this.AllCards[len(this.AllCards)-1]
this.AllCards = this.AllCards[:len(this.AllCards)-1]
return c
}