You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.2 KiB
137 lines
3.2 KiB
|
1 year ago
|
package game
|
||
|
|
|
||
|
|
import (
|
||
|
|
"math/rand"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
ColorDiamonds = iota
|
||
|
|
ColorClubs
|
||
|
|
ColorHearts
|
||
|
|
ColorSpades
|
||
|
|
)
|
||
|
|
|
||
|
|
// 牌库
|
||
|
|
type CardLevelType int
|
||
|
|
|
||
|
|
const (
|
||
|
|
CardLevelA CardLevelType = iota + 1
|
||
|
|
CardLevelB
|
||
|
|
CardLevelS
|
||
|
|
)
|
||
|
|
|
||
|
|
var (
|
||
|
|
AllNormalCards = [52]int{
|
||
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // 方片
|
||
|
|
|
||
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // 梅花
|
||
|
|
|
||
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, // 红桃
|
||
|
|
|
||
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, // 黑桃
|
||
|
|
}
|
||
|
|
Jokers = []int{70, 71} // 两张joker牌
|
||
|
|
// 包含一张王
|
||
|
|
AllFullCards = [53]int{
|
||
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // 方片
|
||
|
|
|
||
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // 梅花
|
||
|
|
|
||
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, // 红桃
|
||
|
|
|
||
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, // 黑桃
|
||
|
|
|
||
|
|
70, // 王
|
||
|
|
}
|
||
|
|
// 包含两张王
|
||
|
|
AllFullGhostCards = [54]int{
|
||
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // 方片
|
||
|
|
|
||
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // 梅花
|
||
|
|
|
||
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, // 红桃
|
||
|
|
|
||
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, // 黑桃
|
||
|
|
|
||
|
|
70, 71, // 王
|
||
|
|
}
|
||
|
|
AllTwoFullCards = [106]int{
|
||
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // 方片
|
||
|
|
|
||
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // 梅花
|
||
|
|
|
||
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, // 红桃
|
||
|
|
|
||
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, // 黑桃
|
||
|
|
|
||
|
|
70, // 王
|
||
|
|
|
||
|
|
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // 方片
|
||
|
|
|
||
|
|
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, // 梅花
|
||
|
|
|
||
|
|
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, // 红桃
|
||
|
|
|
||
|
|
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, // 黑桃
|
||
|
|
|
||
|
|
71, // 王
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func IsValidCard(c int) bool {
|
||
|
|
for _, v := range AllFullGhostCards {
|
||
|
|
if v == c {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetRandomCard(num int) []int {
|
||
|
|
ret := []int{}
|
||
|
|
seq := rand.Perm(len(AllNormalCards))
|
||
|
|
for i := 0; i < num; i++ {
|
||
|
|
ret = append(ret, AllNormalCards[seq[i]])
|
||
|
|
}
|
||
|
|
return ret
|
||
|
|
}
|
||
|
|
|
||
|
|
func CompaireTwoCard(a, b int) bool {
|
||
|
|
valA := GetCardCompaireValue(a)
|
||
|
|
valB := GetCardCompaireValue(b)
|
||
|
|
if valA > valB {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
if valA == valB {
|
||
|
|
colA := GetCardColor(a)
|
||
|
|
colB := GetCardColor(b)
|
||
|
|
return colA > colB
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func ShuffleCards(cards []int) []int {
|
||
|
|
indexs := rand.Perm(len(cards))
|
||
|
|
c := make([]int, len(cards))
|
||
|
|
for i, v := range indexs {
|
||
|
|
c[i] = cards[v]
|
||
|
|
}
|
||
|
|
return c
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetCardValue(card int) int {
|
||
|
|
return card & 0x0F
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetCardCompaireValue(card int) int {
|
||
|
|
v := GetCardValue(card)
|
||
|
|
if v == 1 {
|
||
|
|
v = 14
|
||
|
|
}
|
||
|
|
return v
|
||
|
|
}
|
||
|
|
|
||
|
|
func GetCardColor(card int) int {
|
||
|
|
return card / 16
|
||
|
|
}
|