第 1 条附言 ·
```
package main
import (
"fmt"
)
func main() {
//1-9表示条
//11-19表示饼
//21-29表示万
//31-37表示东南西北中发白
lst := []int{14, 16, 17, 17, 34, 35, 35, 27, 28, 29, 6, 7, 8, 35}
//CheckHu
//第一个参数表示手牌
//第二个参数癞子
//第三个参数是否胡刻子(刻子是格式为三个相同的牌)true
ishu, isbaodiao := CheckHu(lst, 34, false)
fmt.Println(ishu, isbaodiao)
}
func CheckHu(card []int, bao int, kezi bool) (bool, int) {
newCard := make([]int, 0)
baoNumber := 0
for i := 0; i < len(card); i++ {
if card[i] == bao {
baoNumber++
continue
}
newCard = append(newCard, card[i])
}
var handCards [4][10]int
for i := 0; i < len(newCard); i++ {
handCards[newCard[i]/10][newCard[i]%10]++
handCards[newCard[i]/10][0]++
}
a := MahIsHuCardHU(handCards, kezi)
if a == baoNumber {
return true, 1
}
if a < baoNumber {
return true, 2
}
return false, 0
}
func Remove3NCardsHU(cards [4][10]int, kezi bool) ([4][10]int, int, []int) {
num := 0
//! 去掉风牌的刻子
for i := 1; i <= 7; i++ {
if cards[3][i] <= 0 {
continue
}
if cards[3][i] >= 3 {
cards[3][i] -= 3
cards[3][0] -= 3
num++
}
}
FengYun := make([]int, 2)
if !kezi {
if cards[3][5] > 0 { //中发白顺子
tmpCount := cards[3][5]
for k := 0; k < tmpCount; k++ {
if cards[3][6] > 0 && cards[3][7] > 0 {
cards[3][0] -= 3
cards[3][5]--
cards[3][6]--
cards[3][7]--
FengYun[1]++
}
}
}
}
for i := 0; i < 3; i++ {
for j := 1; j <= 7; j++ {
if cards[i][j] <= 0 {
continue
}
//! 去掉刻子
if cards[i][j] >= 3 {
cards[i][j] -= 3
cards[i][0] -= 3
num++
}
if !kezi {
// 先去掉顺子
tmpCount := cards[i][j]
for k := 0; k < tmpCount; k++ {
if j >= 3 && j <= 6 && cards[i][j+1] > 0 && cards[i][j+2] > 0 && cards[i][j-2] == 1 && cards[i][j+3] == 1 {
continue
} else {
if cards[i][j+1] > 0 && cards[i][j+2] > 0 {
cards[i][0] -= 3
cards[i][j]--
cards[i][j+1]--
cards[i][j+2]--
} else {
break
}
}
}
}
}
}
//! 去掉8~9的刻子
for i := 0; i < 3; i++ {
for j := 8; j <= 9; j++ {
if cards[i][j] <= 0 {
continue
}
if cards[i][j] == 3 {
cards[i][0] -= 3
cards[i][j] -= 3
num++
}
}
}
return cards, num, FengYun
}
func MahIsHuCardHU(handCards [4][10]int, kezi bool) int {
relustOne := getResultOne(handCards, kezi)
relustTwo := getResultTwo(handCards, kezi)
if relustOne > relustTwo {
return relustTwo
}
return relustOne
}
func MahIsHuCardQH(handCards [4][10]int, kezi bool) (int, int) {
jiangVec := make([]int, 0)
for i := 0; i < 4; i++ {
for j := 1; j < 10; j++ {
if handCards[i][j] >= 2 {
jiangVec = append(jiangVec, i*10+j)
}
}
}
needNum := 14
jiangCards := 0
for _, card := range jiangVec {
var tmpCards [4][10]int
tmpCards = handCards
tmpCards[card/10][card%10] -= 2
tmpCards[card/10][0] -= 2
tmp, _, _ := Remove3NCardsHU(tmpCards, kezi)
if tmp[0][0]+tmp[1][0]+tmp[2][0]+tmp[3][0] == 0 {
jiangCards = card
return 0, jiangCards
}
relust := 0
for i := 0; i < 4; i++ {
if !kezi {
relust += getBaoNumber(i, tmp[i])
}
if kezi {
relust += getBaoNumberKezi(i, tmp[i])
}
}
if relust < needNum {
jiangCards = card
needNum = relust
}
}
if len(jiangVec) == 0 {
var tmpCards [4][10]int
tmpCards = handCards
tmp, _, _ := Remove3NCardsHU(tmpCards, kezi)
if tmp[0][0]+tmp[1][0]+tmp[2][0]+tmp[3][0] == 0 {
return 0, jiangCards
}
relust := 0
for i := 0; i < 4; i++ {
if !kezi {
relust += getBaoNumber(i, tmp[i])
}
if kezi {
relust += getBaoNumberKezi(i, tmp[i])
}
}
if relust < needNum {
needNum = relust - 1
}
}
return needNum, jiangCards
}
func getBaoNumber(flag int, tmp [10]int) int {
if tmp[0] == 0 {
return 0
}
relust := 0
for i := 1; i < 10; i++ {
if tmp[i] == 0 {
continue
}
if flag == 3 && (i == 1 || i == 2 || i == 3 || i == 4) {
if tmp[i] == 1 {
relust += 2
continue
}
if tmp[i] == 2 {
relust += 1
continue
}
}
if i != 9 && tmp[i] == 1 && tmp[i+1] == 1 {
relust += 1
i++
continue
}
if i < 8 && tmp[i] == 1 && tmp[i+2] >= 1 {
relust += 1
tmp[i+2] -= 1
continue
}
if tmp[i] == 2 {
relust += 1
continue
}
if i != 9 && tmp[i] == 1 && tmp[i+1] == 2 {
relust += 1
tmp[i+1] -= 1
continue
}
relust += 2
}
return relust
}
func getBaoNumberKezi(flag int, tmp [10]int) int {
if tmp[0] == 0 {
return 0
}
relust := 0
for i := 1; i < 10; i++ {
if tmp[i] == 0 {
continue
}
if tmp[i] == 1 {
relust += 2
continue
}
if tmp[i] == 2 {
relust += 1
continue
}
}
return relust
}
func getResultTwo(handCards [4][10]int, kezi bool) int {
jiangVec := make([]int, 0)
for i := 0; i < 4; i++ {
for j := 1; j < 10; j++ {
if handCards[i][j] >= 2 {
jiangVec = append(jiangVec, i*10+j)
}
}
}
needNum := 14
for _, card := range jiangVec {
var tmpCards [4][10]int
tmpCards = handCards
tmpCards[card/10][card%10] -= 2
tmpCards[card/10][0] -= 2
tmp, _, _ := Remove3NCardsHU(tmpCards, kezi)
if tmp[0][0]+tmp[1][0]+tmp[2][0]+tmp[3][0] == 0 {
return 0
}
relust := 0
for i := 0; i < 4; i++ {
if !kezi {
relust += getBaoNumber(i, tmp[i])
}
if kezi {
relust += getBaoNumberKezi(i, tmp[i])
}
}
if relust < needNum {
needNum = relust
}
}
return needNum
}
func getResultOne(handCards [4][10]int, kezi bool) int {
jiangVec := make([]int, 0)
for i := 0; i < 4; i++ {
for j := 1; j < 10; j++ {
if handCards[i][j] == 1 {
jiangVec = append(jiangVec, i*10+j)
}
}
}
needNum := 14
for _, card := range jiangVec {
var tmpCards [4][10]int
tmpCards = handCards
tmpCards[card/10][card%10] -= 1
tmpCards[card/10][0] -= 1
tmp, _, _ := Remove3NCardsHU(tmpCards, kezi)
if tmp[0][0]+tmp[1][0]+tmp[2][0]+tmp[3][0] == 0 {
return 0
}
relust := 0
for i := 0; i < 4; i++ {
if !kezi {
relust += getBaoNumber(i, tmp[i])
}
if kezi {
relust += getBaoNumberKezi(i, tmp[i])
}
}
if relust < needNum {
needNum = relust + 1
}
}
return needNum
}
```