打牌, 首先就应该从最基础的扑克牌类入手
单个牌来说 斗地主3最小, 然后依次是 ... K, A, 2, 小王, 大王
// 花色
const (
flowerNIL int = iota // 留空
flowerHEITAO // 黑桃(小王)
flowerHONGTAO // 红桃(大王)
flowerMEIHUA // 梅花
flowerFANGKUAI // 方块
)
// 点数
const (
cardPointNIL int = iota // 留空
cardPoint3
cardPoint4
cardPoint5
cardPoint6
cardPoint7
cardPoint8
cardPoint9
cardPointT
cardPointJ
cardPointQ
cardPointK
cardPointA
cardPoint2
cardPointX // 小王
cardPointY // 大王
)
我们定义所有扑克牌的牌值是 1 - 54 正好对应54张牌, 这样每张牌都有自己的牌值,花色,点数
// TCard 扑克牌类
type TCard struct {
nValue int // 牌值
nFlower int // 花色
nPoint int // 点数
}
// NewCard 新建卡牌
func NewCard(nValue int) *TCard {
p := &TCard{}
p.nValue = nValue
p.nFlower = toFlower(nValue)
p.nPoint = toCardValue(nValue)
return p
}
// 从牌值获取花色
func toFlower(nValue int) int {
if nValue <= 0 || nValue > 54 {
return flowerNIL
}
return ((nValue - 1) / 13) + 1
}
// 从牌值获取点数
func toCardValue(nValue int) int {
if nValue <= 0 {
return cardPointNIL
}
if nValue == 53 {
return cardPointX // 小王
}
if nValue == 54 {
return cardPointY // 大王
}
return ((nValue - 1) % 13) + 1
}
最后, 为了调试方便, 写一个快速转化字符串的函数
// 转换成字符串
func (self *TCard) tostr() string {
strResult := ""
// 花色
switch self.nFlower {
case flowerHEITAO:
{
strResult = "♠"
}
case flowerHONGTAO:
{
strResult = "♥"
}
case flowerMEIHUA:
{
strResult = "♣"
}
case flowerFANGKUAI:
{
strResult = "♦"
}
}
// 点数
switch self.nPoint {
case cardPoint3:
{
strResult = strResult + "3"
}
case cardPoint4:
{
strResult = strResult + "4"
}
case cardPoint5:
{
strResult = strResult + "5"
}
case cardPoint6:
{
strResult = strResult + "6"
}
case cardPoint7:
{
strResult = strResult + "7"
}
case cardPoint8:
{
strResult = strResult + "8"
}
case cardPoint9:
{
strResult = strResult + "9"
}
case cardPointT:
{
strResult = strResult + "T"
}
case cardPointJ:
{
strResult = strResult + "J"
}
case cardPointQ:
{
strResult = strResult + "Q"
}
case cardPointK:
{
strResult = strResult + "K"
}
case cardPointA:
{
strResult = strResult + "A"
}
case cardPoint2:
{
strResult = strResult + "2"
}
case cardPointX:
{
strResult = "小王"
}
case cardPointY:
{
strResult = "大王"
}
}
return strResult
}