github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/game/poker/cards.go (about) 1 package poker 2 3 import ( 4 "fmt" 5 "math" 6 ) 7 8 const ( 9 _ = iota 10 DiamondCard // 方块 11 ClubCard // 梅花 12 HeartCard // 红桃 13 SpadeCard // 黑桃 14 JokerCard // 王 15 ) 16 17 // 游戏结果 18 const ( 19 ResultLose = iota // 0 失败 20 ResultWin // 1 胜利 21 ResultDraw // 2 流局 22 ) 23 24 var ( 25 Diamonds = []int{0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48} // 方块3到方块A 26 Clubs = []int{1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49} // 梅花3到梅花A 27 Hearts = []int{2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50} // 红桃3到红桃A 28 Spades = []int{3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51} // 黑桃3到黑桃A 29 Jokers = []int{52, 53} // 小王、大王 30 31 CardType = []int{ 32 DiamondCard, ClubCard, HeartCard, SpadeCard, DiamondCard, ClubCard, HeartCard, SpadeCard, 33 DiamondCard, ClubCard, HeartCard, SpadeCard, DiamondCard, ClubCard, HeartCard, SpadeCard, 34 DiamondCard, ClubCard, HeartCard, SpadeCard, DiamondCard, ClubCard, HeartCard, SpadeCard, 35 DiamondCard, ClubCard, HeartCard, SpadeCard, DiamondCard, ClubCard, HeartCard, SpadeCard, 36 DiamondCard, ClubCard, HeartCard, SpadeCard, DiamondCard, ClubCard, HeartCard, SpadeCard, 37 DiamondCard, ClubCard, HeartCard, SpadeCard, DiamondCard, ClubCard, HeartCard, SpadeCard, 38 DiamondCard, ClubCard, HeartCard, SpadeCard, JokerCard, JokerCard, 39 } 40 CardString = []string{ 41 "方块3", "梅花3", "红桃3", "黑桃3", "方块4", "梅花4", "红桃4", "黑桃4", 42 "方块5", "梅花5", "红桃5", "黑桃5", "方块6", "梅花6", "红桃6", "黑桃6", 43 "方块7", "梅花7", "红桃7", "黑桃7", "方块8", "梅花8", "红桃8", "黑桃8", 44 "方块9", "梅花9", "红桃9", "黑桃9", "方块10", "梅花10", "红桃10", "黑桃10", 45 "方块J", "梅花J", "红桃J", "黑桃J", "方块Q", "梅花Q", "红桃Q", "黑桃Q", 46 "方块K", "梅花K", "红桃K", "黑桃K", "方块A", "梅花A", "红桃A", "黑桃A", 47 "方块2", "梅花2", "红桃2", "黑桃2", "小王", "大王", 48 } 49 ) 50 51 func CardValue(card int) int { 52 switch card { 53 case 52: 54 return 16 55 case 53: 56 return 17 57 } 58 return int(math.Floor(float64(card/4)) + 3) 59 } 60 61 func MeldKey(meld []int) string { 62 return fmt.Sprintf("%d:%d", meld[0], len(meld)) 63 } 64 65 func CountCardValue(cards []int, card int) int { 66 count, value := 0, CardValue(card) 67 for _, v := range cards { 68 if CardValue(v) == value { 69 count++ 70 } 71 } 72 return count 73 } 74 75 // cards 从大到小 76 func GetCardsByValue(cards []int, value int) []int { 77 a := []int{} 78 for _, card := range cards { 79 value2 := CardValue(card) 80 if value == value2 { 81 a = append(a, card) 82 } else if value > value2 { 83 break 84 } 85 } 86 return a 87 } 88 89 func GetCardsMap(cards []int, m map[int]bool) map[int]bool { 90 for _, card := range cards { 91 m[card] = true 92 } 93 return m 94 } 95 96 func GetMeldsCardsMap(melds [][]int, m map[int]bool) map[int]bool { 97 for _, meld := range melds { 98 m = GetCardsMap(meld, m) 99 } 100 return m 101 } 102 103 func GetCardValueMap(cards []int) map[int]bool { 104 m := make(map[int]bool) 105 for _, card := range cards { 106 m[CardValue(card)] = true 107 } 108 return m 109 } 110 111 func RemoveCardByValue(cards []int, valueMap map[int]bool) []int { 112 a := []int{} 113 for _, card := range cards { 114 if !valueMap[CardValue(card)] { 115 a = append(a, card) 116 } 117 } 118 return a 119 } 120 121 func GetMeldByValue(melds [][]int, value int) []int { 122 for _, meld := range melds { 123 valueMap := GetCardValueMap(meld) 124 if valueMap[value] { 125 return meld 126 } 127 } 128 return []int{} 129 } 130 131 func ToCardsString(cards []int) []string { 132 var s []string 133 for _, v := range cards { 134 s = append(s, CardString[v]) 135 } 136 return s 137 } 138 139 func ToMeldsString(melds [][]int) [][]string { 140 s := [][]string{} 141 for _, v := range melds { 142 s = append(s, ToCardsString(v)) 143 } 144 return s 145 } 146 147 // cards 从大到小 148 func Sequence(cards []int) bool { 149 value := CardValue(cards[0]) 150 cardsLen := len(cards) 151 for i := 1; i < cardsLen; i++ { 152 value2 := CardValue(cards[i]) 153 if value-value2 == 1 { 154 value = value2 155 } else { 156 return false 157 } 158 } 159 return true 160 } 161 162 // 全是对子且连续 163 func AllPair(cards []int) (bool, bool) { 164 cardsLen := len(cards) 165 if cardsLen == 0 || cardsLen%2 > 0 { 166 return false, false 167 } 168 allPair, temp := true, []int{} 169 for i := 0; i < cardsLen/2; i++ { 170 if CountCardValue(cards[i*2:i*2+2], cards[i*2]) == 2 { 171 temp = append(temp, cards[i*2]) 172 } else { 173 allPair = false 174 break 175 } 176 } 177 if allPair { 178 return true, Sequence(temp) 179 } 180 return false, false 181 } 182 183 // 全是三张且连续 184 func AllTrio(cards []int) (bool, bool) { 185 cardsLen := len(cards) 186 if cardsLen == 0 || cardsLen%3 > 0 { 187 return false, false 188 } 189 allTrio, temp := true, []int{} 190 for i := 0; i < cardsLen/3; i++ { 191 if CountCardValue(cards[i*3:i*3+3], cards[i*3]) == 3 { 192 temp = append(temp, cards[i*3]) 193 } else { 194 allTrio = false 195 break 196 } 197 } 198 if allTrio { 199 return true, Sequence(temp) 200 } 201 return false, false 202 }