138 lines
2.4 KiB
Go
138 lines
2.4 KiB
Go
package poker
|
|
|
|
import (
|
|
"math"
|
|
"math/rand"
|
|
"samba/util/util"
|
|
"time"
|
|
)
|
|
|
|
func init() {
|
|
rand.Seed(time.Now().UnixNano())
|
|
}
|
|
|
|
type Color int
|
|
|
|
const (
|
|
Unknown Color = 0
|
|
Spade Color = 1 // 黑
|
|
Heart Color = 2 // 红
|
|
Club Color = 3 // 梅
|
|
Diamond Color = 4 // 方
|
|
)
|
|
|
|
func (c Color) String() string {
|
|
switch c {
|
|
case Spade:
|
|
return "♠"
|
|
case Heart:
|
|
return "♥"
|
|
case Club:
|
|
return "♣"
|
|
case Diamond:
|
|
return "◆"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
func (c Color) Int() int { return int(c) }
|
|
|
|
type Point int
|
|
|
|
const (
|
|
PointA Point = 1
|
|
Point2 Point = 2
|
|
Point3 Point = 3
|
|
Point4 Point = 4
|
|
Point5 Point = 5
|
|
Point6 Point = 6
|
|
Point7 Point = 7
|
|
Point8 Point = 8
|
|
Point9 Point = 9
|
|
Point10 Point = 10
|
|
PointJ Point = 11
|
|
PointQ Point = 12
|
|
PointK Point = 13
|
|
PointMinJoker Point = 14
|
|
PointMaxJoker Point = 15
|
|
)
|
|
|
|
func (p Point) String() string {
|
|
switch p {
|
|
case PointA:
|
|
return "A"
|
|
case Point2:
|
|
return "2"
|
|
case Point3:
|
|
return "3"
|
|
case Point4:
|
|
return "4"
|
|
case Point5:
|
|
return "5"
|
|
case Point6:
|
|
return "6"
|
|
case Point7:
|
|
return "7"
|
|
case Point8:
|
|
return "8"
|
|
case Point9:
|
|
return "9"
|
|
case Point10:
|
|
return "10"
|
|
case PointJ:
|
|
return "J"
|
|
case PointQ:
|
|
return "Q"
|
|
case PointK:
|
|
return "K"
|
|
case PointMinJoker:
|
|
return "MinJoker"
|
|
case PointMaxJoker:
|
|
return "MaxJoker"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
func (p Point) Int() int { return int(p) }
|
|
|
|
type Poker struct {
|
|
Color Color // 花色
|
|
Point Point // 点数
|
|
IsDark bool // true:暗牌
|
|
}
|
|
|
|
func NewPoker(values ...int) *Poker {
|
|
if len(values) == 1 {
|
|
a := int(math.Abs(float64(values[0])))
|
|
return &Poker{Color: Color(a / 100), Point: Point(a % 100), IsDark: util.Tie(values[0] < 0, true, false)}
|
|
}
|
|
if len(values) == 2 {
|
|
ac := int(math.Abs(float64(values[0])))
|
|
ap := int(math.Abs(float64(values[1])))
|
|
return &Poker{Color: Color(ac), Point: Point(ap), IsDark: util.Tie(values[0] < 0 || values[1] < 0, true, false)}
|
|
}
|
|
return &Poker{0, 0, false}
|
|
}
|
|
|
|
func (p *Poker) ToInt() int {
|
|
return util.Tie(p.IsDark, -1, 1) * (int(p.Color*100) + int(p.Point))
|
|
}
|
|
|
|
func (p *Poker) ToString() string {
|
|
if p.Point != PointMinJoker && p.Point != PointMaxJoker {
|
|
return util.Tie(p.IsDark, "-", "") + p.Color.String() + p.Point.String()
|
|
} else {
|
|
return p.Point.String()
|
|
}
|
|
}
|
|
|
|
func PokersToString(pokers []*Poker) string {
|
|
s := ""
|
|
for _, poker := range pokers {
|
|
s += poker.ToString() + " "
|
|
}
|
|
return s
|
|
}
|