//取扑克花色
func GetPokerColor(cardData byte) uint8 {
return cardData / 16
}
//取扑克牌值
func GetPokerValue(cardData byte) uint8 {
return cardData & 255 % 16
}
//取扑克牌值,花色
func GetPokerValueAndColor(cardData byte) (uint8, uint8) {
return cardData & 255 % 16, cardData / 16
}
//生成随机数
func GetRandNum(theMaxNum uint32) int32 {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Int31n(int32(theMaxNum))
}
//取不重复的牌
func GetCards(count uint32, cardMaxCount uint32) []byte {
cards := make([]byte, count)
for j := 0; j < 5; j++ {
for i := 0; i < 3; i++ {
v := GetRandNum(cardMaxCount)
for k := 0; k < len(cards); k++ {
if v == int32(cards[k]) {
v = GetRandNum(cardMaxCount)
}
}
cards[i+j*3] = byte(v)
}
}
return cards
}
测试
//生成牌
fmt.Println(c.GetCards(15, 52))
colorName := []string{"方块", "梅花", "红桃", "黑桃"}
cardIndex := c.GetCards(15, 52)
for i := 0; i < len(cardIndex); i++ {
value, color := c.GetPokerValueAndColor(c.GAME_CARD_DATA[cardIndex[i]])
v := fmt.Sprintf("[%v%v],", colorName[color-1], value)
fmt.Print(v)
}