万能麻将和牌算法
时间:2018-10-24 10:51:36 +0800 CST 浏览:1136

该算法通过计算需要的万能牌个数,和已经拥有的万能牌个数对比来判断是否胡牌,算法通用,且效率高,计算胡牌算法每秒钟可以运行三百万次以上。

golang_S1-l6gVIX.jpg

1. 所有合法牌:

0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09  万
0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19  条
0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29  筒
0x31 0x32 0x33 0x34 0x35 0x36 0x37            东西南北中发白

2. 核心算法

将万、条、筒、风各种花色分开,然后分别计算每种花色构成整扑(整扑即三张相同牌或者顺子)还需要多少张万能牌。如:0x01、0x01、0x02、0x03、0x04
构成整扑需要一张万能牌, 0x01、0x03、0x05、0x07构成整扑需要两张万能牌。

计算整扑按照从小到大的顺序一次计算。

首先检查是否是刻子(三张相同的牌)。

然后检测检查是否是对子。

如果是对子,检测它是否符合A2BxCy,xyz为张数,

如果x>0,y>1或者x>1,y>0,则分别检测A成对子和A成顺子需要的万能牌张数。否则,则A组合成对子来计算。

如果是单张牌,则检测其是否可以与后面的牌组合成顺子,如果不能则检测是否能组合成吃牌,例如一万二万,一万三万这种组合,如果还是不能,就是一张单独的牌。

风牌判断比较简单,这里就不赘述了。

计算完成后,依次遍历将牌在万,条,筒,风的情况下,它们构成整扑加将需要的万能牌个数,然后对比拥有的万能牌个数,便可以得出是否可以胡牌。

3. 逻辑代码

判断字牌(万,条,筒)组合成整扑需要的万能牌个数。

func SwitchCardToIndex(card uint8) uint8 {
    return 9 * (card / 16) + card % 16 - 1
}
func SwitchCardsToIndexList(cards []uint8) []uint8 {
    indexList := make([]uint8, 34)
    for _, card := range cards {
        index := SwitchCardToIndex(card)
        indexList[index] ++
    }
    return indexList
}
func CheckZiPaiZhengPu(cardsIndex []uint8) int {
    tmpCardsIndex := cardsIndex
    needMagicNum := 0
    curPos := 0
    for curPos < len(tmpCardsIndex){
        if tmpCardsIndex[curPos] >= 3 {
            tmpCardsIndex[curPos] -= 3
        } else if tmpCardsIndex[curPos] == 2 {
            if tmpCardsIndex[curPos+1] > 0 && tmpCardsIndex[curPos+2] > 0 && (tmpCardsIndex[curPos+1]>1 || tmpCardsIndex[curPos+2]>1){
                shunCardsIndex := make([]uint8, len(tmpCardsIndex))
                copy(shunCardsIndex, tmpCardsIndex)
                shunCardsIndex[curPos] --
                shunCardsIndex[curPos+1] --
                shunCardsIndex[curPos+2] --
                shunNeedNum := CheckZiPaiZhengPu(shunCardsIndex)
                duiCardsIndex := tmpCardsIndex
                duiCardsIndex[curPos] -= 2
                duiNeedNum := CheckZiPaiZhengPu(duiCardsIndex) + 1
                if duiNeedNum > shunNeedNum {
                    return shunNeedNum + needMagicNum
                }
                return duiNeedNum + needMagicNum
            } else {
                needMagicNum ++
                tmpCardsIndex[curPos] -= 2
            }
        } else if tmpCardsIndex[curPos] == 1{
            if tmpCardsIndex[curPos+1] >= 1 && tmpCardsIndex[curPos+2] >= 1 {
                tmpCardsIndex[curPos] --
                tmpCardsIndex[curPos+1] --
                tmpCardsIndex[curPos+2] --
            } else if tmpCardsIndex[curPos+1] >= 1 {
                needMagicNum ++
                tmpCardsIndex[curPos] --
                tmpCardsIndex[curPos+1] --
            } else if tmpCardsIndex[curPos+2] >= 1 {
                needMagicNum ++
                tmpCardsIndex[curPos] --
                tmpCardsIndex[curPos+2] --
            } else {
                needMagicNum += 2
                tmpCardsIndex[curPos] --
            }
        } else {
            curPos ++
        }
    }
    return needMagicNum
}

判断(万,条,筒)组合成整扑需要的万能牌个数。

func CheckFengPaiZhengPu(cardsIndex []uint8) int {
    tmpCardsIndex := cardsIndex
    needMagicNum := 0
    curPos := 0
    for curPos < 7{
        if tmpCardsIndex[curPos] >= 3 {
            tmpCardsIndex[curPos] -= 3
        } else if tmpCardsIndex[curPos] == 2 {
            needMagicNum ++
            tmpCardsIndex[curPos] -= 2
        } else if tmpCardsIndex[curPos] == 1{
            needMagicNum += 2
            tmpCardsIndex[curPos] --
        } else {
            curPos ++
        }
    }
    return needMagicNum
}

检测是否可以胡牌

func CanHuPai(handCardIndex []uint8, magicCard uint8) bool {
    magicIndex := SwitchCardToIndex(magicCard)
    tmpCardIndex := make([]uint8, len(handCardIndex))
    copy(tmpCardIndex, handCardIndex)
    magickNum := tmpCardIndex[magicIndex]
    if magickNum >= 3 {
        return true
    }
    tmpCardIndex[magicIndex] = 0
    tmpIndexList := make([]uint8, 11)
    //分析万,条,筒
    //ziPaiType := []string{"万", "条", "筒"}
    ziPaiNeedMagicNums := make([]int, 3)
    for i:=0; i<3; i++ {
        copy(tmpIndexList, tmpCardIndex[i*9:(i+1)*9])
        tmpResult := CheckZiPaiZhengPu(tmpIndexList)
        ziPaiNeedMagicNums[i] = tmpResult
    }
    //分析风
    copy(tmpIndexList, tmpCardIndex[27:34])
    fengPaiNeedMagicNum := CheckFengPaiZhengPu(tmpIndexList)
    //jiangType := []string{"万", "条", "筒"}
    totalNeedMagick := ziPaiNeedMagicNums[0] + ziPaiNeedMagicNums[1] + ziPaiNeedMagicNums[2] + fengPaiNeedMagicNum
    //所有牌都可以成整朴,将必定在财神牌上
    if int(magickNum) - totalNeedMagick >= 0 {
        //fmt.Println("将在------",jiangType[ziPaiType])
        return true
    }
    //将在万,条,筒中
    for ziPaiType:=0; ziPaiType<3; ziPaiType++{
        leftMagicNum := int(magickNum) + ziPaiNeedMagicNums[ziPaiType] - totalNeedMagick
        if leftMagicNum >= 0 {
            for eyeIndex:=0; eyeIndex<9; eyeIndex ++ {
                copy(tmpIndexList, tmpCardIndex[ziPaiType * 9:(ziPaiType+1) * 9])
                if tmpIndexList[eyeIndex] >= 2 {
                    tmpIndexList[eyeIndex] -= 2
                    tmpNeedMagic := CheckZiPaiZhengPu(tmpIndexList)
                    if leftMagicNum >= tmpNeedMagic {
                        return true
                    }
                } else if tmpIndexList[eyeIndex] == 1 && leftMagicNum > 0{
                    tmpNeedMagic := 1
                    tmpIndexList[eyeIndex] --
                    tmpNeedMagic += CheckFengPaiZhengPu(tmpIndexList)
                    if leftMagicNum >= tmpNeedMagic {
                        return true
                    }
                }
            }
        }
    }
    //将在风中
    leftMagicNum := int(magickNum) + fengPaiNeedMagicNum - totalNeedMagick
    if leftMagicNum >= 0 {
        for eyeIndex:=0; eyeIndex<7; eyeIndex ++ {
            copy(tmpIndexList, tmpCardIndex[27:34])
            if tmpIndexList[eyeIndex] >= 2 {
                tmpIndexList[eyeIndex] -= 2
                tmpNeedMagic := CheckZiPaiZhengPu(tmpIndexList)
                if leftMagicNum >= tmpNeedMagic {
                    return true
                }
            } else if tmpIndexList[eyeIndex] == 1 && leftMagicNum > 0{
                tmpNeedMagic := 1
                tmpIndexList[eyeIndex] --
                tmpNeedMagic += CheckFengPaiZhengPu(tmpIndexList)
                if leftMagicNum >= tmpNeedMagic {
                    return true
                }
            }
        }
    }
    return false
}

4. 测试代码

func main() {
    if err:=recover(); err != nil {
        fmt.Println(err)
    }
    //cardsIndex := SwitchCardsToIndexList([]uint8{0x11,0x17,0x12,0x11,0x17,0x12,0x13,0x13,0x21,0x21,0x22,0x22,0x22,0x23,0x23,0x26,0x26})
    //cardsIndex := SwitchCardsToIndexList([]uint8{0x12,0x12,0x13,0x13,0x13,0x14,0x15,0x16,0x02,0x02,0x03,0x03,0x03,0x04,0x05,0x06,0x26,0x26})
    cardsIndex := SwitchCardsToIndexList([]uint8{0x11,0x11,0x12,0x13,0x13,0x22,0x22,0x23,0x24,0x26,0x26})
    isHu := CanHuPai(cardsIndex, 0x26)
    fmt.Println(isHu)
}

5. 说明

转载自 https://www.jianshu.com/p/5b824a50c1b8



如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。

本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。


来说两句吧