Golang makes a Doudizhu game server [1]: Start with playing cards

To play cards, you should start with the most basic playing cards.

For a single card, Doudizhu 3 is the smallest, followed by... K, A, 2, Xiao Wang, Da Wang


// 花色
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 // 大王
)

We define that the card value of all playing cards is 1-54 exactly corresponding to 54 cards, so that each card has its own card value, suit, and point

 


// 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
}

Finally, for debugging convenience, write a function that quickly converts strings.


// 转换成字符串
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
}

 

Guess you like

Origin blog.csdn.net/warrially/article/details/88559191