
// [[
//     斗地主游戏逻辑
//     2016.6
// ]]

// 扑克类型
var CT_ERROR = 0;                   // 错误类型
var CT_SINGLE = 1;                  // 单牌类型
var CT_DOUBLE = 2;                  // 对牌类型
var CT_THREE = 3;                   // 三条类型
var CT_SINGLE_LINE = 4;             // 单连类型
var CT_DOUBLE_LINE = 5;             // 对连类型
var CT_THREE_LINE = 6;              // 三连类型
var CT_THREE_TAKE_ONE = 7;          // 三带一单
var CT_THREE_TAKE_TWO = 8;          // 三带一对
var CT_FOUR_TAKE_ONE = 9;           // 四带两单
var CT_FOUR_TAKE_TWO = 10;          // 四带两对
var CT_BOMB_CARD = 11;              // 炸弹类型
var CT_MISSILE_CARD = 12;           // 火箭类型

let GameLogicController = {
    _CardData: [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, // 黑桃
        0x4E, 0x4F], // 小王,大王

    // 创建空扑克数组
    emptyCardList: function (count) {
        var tmp = [];
        for (var i = 1; i < count; i++) {
            tmp[i] = 0;
        }
        return tmp
    },

    // 获取整数
    ceil: function (a, b) {
        return Math.ceil(a / b) - 1;
    },

    // 获取牌值(1-15)
    GetCardValue: function (nCardData) {
        return nCardData % 16;
    },

    // 获取花色(1-5)
    GetCardColor: function (nCardData) {
        return nCardData & 0XF0;
    },

    // 逻辑牌值(大小王、2、A、K、Q)
    GetCardLogicValue: function (nCardData) {
        var nCardValue = this.GetCardValue(nCardData);
        var nCardColor = this.GetCardColor(nCardData);
        if (nCardColor == 0x40) {
            return nCardValue + 2;
        }
        return (0 < nCardValue && nCardValue <= 2) ? (nCardValue + 13) : nCardValue;
    },

    // // 获取牌序 0x4F大王 0x4E小王 nil牌背 
    // GetCardIndex: function(nCardData) {
    //     if (nCardData == 0x4E) {
    //         return 53;
    //      } else if (nCardData == 0x4F) {
    //         return 54;
    //      } else if (nCardData == null) {
    //          return 55;
    //      }
    //      var nCardValue = this.GetCardValue(nCardData);
    //      var nCardColor = this.GetCardColor(nCardData);
    //      nCardColor = nCardColor >> 4;
    //      return nCardColor * 13 + nCardValue;
    // },

    //扑克排序
    SortCardList: function (cbCardData, cbCardCount, cbSortType) {
        var cbSortValue = [];
        for (var i = 0; i < cbCardCount; i++) {
            var value = this.GetCardLogicValue(cbCardData[i]);
            cbSortValue[i] = value;
        }
        if (cbSortType == 0) {//大小排序
            for (var i = 0; i < cbCardCount - 1; i++) {
                for (var j = 0; j < cbCardCount - 1; j++) {
                    if ((cbSortValue[j] < cbSortValue[j + 1]) || (cbSortValue[j] == cbSortValue[j + 1] && cbCardData[j] < cbCardData[j + 1])) {
                        var temp = cbSortValue[j];
                        cbSortValue[j] = cbSortValue[j + 1];
                        cbSortValue[j + 1] = temp;
                        var temp2 = cbCardData[j];
                        cbCardData[j] = cbCardData[j + 1];
                        cbCardData[j + 1] = temp2;
                    }
                }
            }
        }
        return cbCardData;
    },

    //分析有序扑克
    AnalysebCardData: function (cbCardData, cbCardCount) {
        //相同个数
        var cbBlockCount = [0, 0, 0, 0, 0];
        //相同的牌
        var cbCardDatas = [[], [], [], [], []];
        var i = 0;
        while (i < cbCardCount) {
            var cbSameCount = 1;
            var cbLogicValue = this.GetCardLogicValue(cbCardData[i]);
            var j = i + 1;
            while (j < cbCardCount) {
                var cbLogicValue2 = this.GetCardLogicValue(cbCardData[j]);
                if (cbLogicValue != cbLogicValue2) {
                    break;
                }
                cbSameCount = cbSameCount + 1;
                j = j + 1;
            }
            if (cbSameCount > 4) {
                cc.log("这儿有错误");
                return;
            }
            cbBlockCount[cbSameCount] = cbBlockCount[cbSameCount] + 1;
            var index = cbBlockCount[cbSameCount] - 1;
            for (var k = 1; k <= cbSameCount; k++) {
                cbCardDatas[cbSameCount][index * cbSameCount + k] = cbCardData[i + k - 1];
            }
            i = i + cbSameCount;
        }
        //分析结构
        var tagAnalyseResult = [0, cbBlockCount, cbCardDatas];
        return tagAnalyseResult;
    },

    //获取类型
    GetCardType: function (cbCardData, cbCardCount) {
        cbCardData = this.SortCardList(cbCardData, cbCardCount, 0);
        //简单牌型
        if (cbCardCount == 0) {                 //空牌
            return CT_ERROR;
        } else if (cbCardCount == 1) {          //单牌
            return CT_SINGLE;
        } else if (cbCardCount == 2) {          //对牌火箭
            if (cbCardData[0] == 0x4F && cbCardData[1] == 0x4E) {
                return CT_MISSILE_CARD;
            }
            if (this.GetCardLogicValue(cbCardData[0]) == this.GetCardLogicValue(cbCardData[1])) {
                return CT_DOUBLE;
            }
        }
        var tagAnalyseResult = {};
        tagAnalyseResult = this.AnalysebCardData(cbCardData, cbCardCount);
        //四牌判断
        if (tagAnalyseResult[1][4] > 0) {
            if (tagAnalyseResult[1][4] == 1 && cbCardCount == 4) {
                return CT_BOMB_CARD;
            }
            if (tagAnalyseResult[1][4] == 1 && cbCardCount == 6) {
                return CT_FOUR_TAKE_ONE;
            }
            if ((tagAnalyseResult[1][4] == 1 && cbCardCount == 8) && tagAnalyseResult[1][2] == 2) {
                return CT_FOUR_TAKE_TWO;
            }
            return CT_ERROR;
        }
        //三牌判断
        if (tagAnalyseResult[1][3] > 0) {
            if (tagAnalyseResult[1][3] > 1) {
                var cbCard = tagAnalyseResult[2][3][1];
                var cbFirstLogicValue = this.GetCardLogicValue(cbCard);
                if (cbFirstLogicValue >= 15) {
                    return CT_ERROR;
                }
                for (var i = 2; i <= tagAnalyseResult[1][3]; i++) {
                    var cbCard = tagAnalyseResult[2][3][(i - 1) * 3 + 1];
                    var cbNextLogicValue = this.GetCardLogicValue(cbCard);
                    if (cbFirstLogicValue != cbNextLogicValue + i - 1) {
                        return CT_ERROR;
                    }
                }
            } else if (cbCardCount == 3) {
                return CT_THREE;
            }
            if (tagAnalyseResult[1][3] * 3 == cbCardCount) {
                return CT_THREE_LINE;
            }
            if (tagAnalyseResult[1][3] * 4 == cbCardCount) {
                return CT_THREE_TAKE_ONE;
            }
            if (tagAnalyseResult[1][3] * 5 == cbCardCount && tagAnalyseResult[1][2] == tagAnalyseResult[1][3]) {
                return CT_THREE_TAKE_TWO;
            }
            return CT_ERROR;
        }
        //两张判断
        if (tagAnalyseResult[1][2] >= 3) {
            var cbCard = tagAnalyseResult[2][2][1];
            var cbFirstLogicValue = this.GetCardLogicValue(cbCard);
            if (cbFirstLogicValue >= 15) {
                return CT_ERROR;
            }
            for (var i = 2; i <= tagAnalyseResult[1][2]; i++) {
                var cbCard = tagAnalyseResult[2][2][(i - 1) * 2 + 1];
                var cbNextLogicValue = this.GetCardLogicValue(cbCard);
                if (cbFirstLogicValue != cbNextLogicValue + i - 1) {
                    return CT_ERROR;
                }
            }
            if (tagAnalyseResult[1][2] * 2 == cbCardCount) {
                return CT_DOUBLE_LINE;
            }
            return CT_ERROR;
        }
        //单张判断
        if (tagAnalyseResult[1][1] >= 5 && tagAnalyseResult[1][1] == cbCardCount) {
            var cbCard = tagAnalyseResult[2][1][1];
            var cbFirstLogicValue = this.GetCardLogicValue(cbCard);
            if (cbFirstLogicValue >= 15) {
                return CT_ERROR;
            }
            for (var i = 2; i <= tagAnalyseResult[1][1]; i++) {
                var cbCard = tagAnalyseResult[2][1][i];
                var cbNextLogicValue = this.GetCardLogicValue(cbCard);
                if (cbFirstLogicValue != cbNextLogicValue + i - 1) {
                    return CT_ERROR;
                }
            }
            return CT_SINGLE_LINE;
        }
        return CT_ERROR;
    },

    //对比扑克
    CompareCard: function (cbFirstCard, cbNextCard) {
        var cbFirstCount = cbFirstCard.length;
        var cbNextCount = cbNextCard.length;
        var cbNextType = this.GetCardType(cbNextCard, cbNextCount);
        var cbFirstType = this.GetCardType(cbFirstCard, cbFirstCount);
        if (cbNextType == CT_ERROR) {
            return false;
        }
        if (cbFirstCount == 0 && cbNextType != CT_ERROR) {
            return true;
        }
        if (cbNextType == CT_MISSILE_CARD) {
            return true;
        }
        if (cbFirstType != CT_BOMB_CARD && cbNextType == CT_BOMB_CARD) {
            return true;
        }
        if (cbFirstType == CT_BOMB_CARD && cbNextType != CT_BOMB_CARD) {
            return false;
        }
        if (cbFirstType != cbNextType || cbFirstCount != cbNextCount) {
            return false;
        }
        //开始对比
        if ((cbNextType == CT_SINGLE) || (cbNextType == CT_DOUBLE) || (cbNextType == CT_THREE) || (cbNextType == CT_SINGLE_LINE) || (cbNextType == CT_DOUBLE_LINE) || (cbNextType == CT_THREE_LINE) || (cbNextType == CT_BOMB_CARD)) {
            var cbNextLogicValue = this.GetCardLogicValue(cbNextCard[0]);
            var cbFirstLogicValue = this.GetCardLogicValue(cbFirstCard[0]);
            return cbNextLogicValue > cbFirstLogicValue;
        } else if ((cbNextType == CT_THREE_TAKE_ONE) || (cbNextType == CT_THREE_TAKE_TWO)) {
            var nextResult = this.AnalysebCardData(cbNextCard, cbNextCount);
            var firstResult = this.AnalysebCardData(cbFirstCard, cbFirstCount);
            var cbNextLogicValue = this.GetCardLogicValue(nextResult[2][3][1]);
            var cbFirstLogicValue = this.GetCardLogicValue(firstResult[2][3][1]);
            return cbNextLogicValue > cbFirstLogicValue;
        } else if ((cbNextType == CT_FOUR_TAKE_ONE) || (cbNextType == CT_FOUR_TAKE_TWO)) {
            var nextResult = this.AnalysebCardData(cbNextCard, cbNextCount);
            var firstResult = this.AnalysebCardData(cbFirstCard, cbFirstCount);
            var cbNextLogicValue = this.GetCardLogicValue(nextResult[2][4][1]);
            var cbFirstLogicValue = this.GetCardLogicValue(firstResult[2][4][1]);
            return cbNextLogicValue > cbFirstLogicValue;
        }
        return false;
    },

    //出牌搜索
    SearchOutCard: function (cbHandCardData, cbTurnCardData) {
        var cbHandCardCount = cbHandCardData.length;
        var cbTurnCardCount = cbTurnCardData.length ? cbTurnCardData.length : 0;
        //结果数目
        var cbResultCount = 1;
        //扑克数目
        var cbResultCardCount = [];
        //结果扑克
        var cbResultCard = [];
        //搜索结果
        var tagSearchCardResult = [null, cbResultCount - 1, cbResultCardCount, cbResultCard];
        //排序扑克
        var cbCardData = this.SortCardList(cbHandCardData, cbHandCardCount, 0);
        var cbCardCount = cbHandCardCount;
        //出牌分析
        var cbTurnOutType = this.GetCardType(cbTurnCardData, cbTurnCardCount);
        if (cbTurnOutType == CT_ERROR) {//错误类型
            //cc.log("上家为空");
            //是否一手出完
            if (this.GetCardType(cbCardData, cbCardCount) != CT_ERROR) {
                cbResultCardCount[cbResultCount] = cbCardCount;
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = [null].concat(cbCardData);
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //如果最小牌不是单牌，则提取
            var cbSameCount = 1;
            if (cbCardCount > 1 && (this.GetCardLogicValue(cbCardData[cbCardCount]) == this.GetCardLogicValue(cbCardData[cbCardCount - 1]))) {
                cbSameCount = 2;
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount][1] = cbCardData[cbCardCount];
                var cbCardValue = this.GetCardLogicValue(cbCardData[cbCardCount]);
                var i = cbCardCount - 1;
                while (i >= 1) {
                    if (this.GetCardLogicValue(cbCardData[i]) == cbCardValue) {
                        cbResultCard[cbResultCount][cbSameCount] = cbCardData[i];
                        cbSameCount = cbSameCount + 1;
                    }
                    i = i - 1;
                }
                cbResultCardCount[cbResultCount] = cbSameCount - 1;
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            // //单牌
            var cbTmpCount = 1;
            if (cbSameCount != 2) {
                //cc.log("单牌Pan");
                var tagSearchCardResult1 = this.SearchSameCard(cbCardData, cbCardCount, 0, 1);
                cbTmpCount = tagSearchCardResult1[1];
                if (cbTmpCount > 0) {
                    cbResultCardCount[cbResultCount] = tagSearchCardResult1[2][1];
                    cbResultCard[cbResultCount] = [];
                    cbResultCard[cbResultCount] = tagSearchCardResult1[3][1];
                    cbResultCount = cbResultCount + 1;
                    tagSearchCardResult[2] = cbResultCardCount;
                    tagSearchCardResult[3] = cbResultCard;
                }
            }
            //对牌
            if (cbSameCount != 3) {
                var tagSearchCardResult1 = this.SearchSameCard(cbCardData, cbCardCount, 0, 2);
                cbTmpCount = tagSearchCardResult1[1];
                if (cbTmpCount > 0) {
                    cbResultCardCount[cbResultCount] = tagSearchCardResult1[2][1];
                    cbResultCard[cbResultCount] = [];
                    cbResultCard[cbResultCount] = tagSearchCardResult1[3][1];
                    cbResultCount = cbResultCount + 1;
                    tagSearchCardResult[2] = cbResultCardCount;
                    tagSearchCardResult[3] = cbResultCard;
                }
            }
            //三条
            if (cbSameCount != 4) {
                var tagSearchCardResult1 = this.SearchSameCard(cbCardData, cbCardCount, 0, 3);
                cbTmpCount = tagSearchCardResult1[1];
                if (cbTmpCount > 0) {
                    cbResultCardCount[cbResultCount] = tagSearchCardResult1[2][1];
                    cbResultCard[cbResultCount] = [];
                    cbResultCard[cbResultCount] = tagSearchCardResult1[3][1];
                    cbResultCount = cbResultCount + 1;
                    tagSearchCardResult[2] = cbResultCardCount;
                    tagSearchCardResult[3] = cbResultCard;
                }
            }
            //三带一单
            var tagSearchCardResult2 = this.SearchTakeCardType(cbCardData, cbCardCount, 0, 3, 1);
            cbTmpCount = tagSearchCardResult2[1];
            if (cbTmpCount > 0) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult2[2][1];
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = tagSearchCardResult2[3][1];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //三带一对
            var tagSearchCardResult3 = this.SearchTakeCardType(cbCardData, cbCardCount, 0, 3, 2);
            cbTmpCount = tagSearchCardResult3[1];
            if (cbTmpCount > 0) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult3[2][1];
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = tagSearchCardResult3[3][1];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //单连
            var tagSearchCardResult4 = this.SearchLineCardType(cbCardData, cbCardCount, 0, 1, 0);
            cbTmpCount = tagSearchCardResult4[1];
            if (cbTmpCount > 0) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult4[2][1];
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = tagSearchCardResult4[3][1];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //连对
            var tagSearchCardResult5 = this.SearchLineCardType(cbCardData, cbCardCount, 0, 2, 0);
            cbTmpCount = tagSearchCardResult5[1];
            if (cbTmpCount > 0) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult5[2][1];
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = tagSearchCardResult5[3][1];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //三连
            var tagSearchCardResult6 = this.SearchLineCardType(cbCardData, cbCardCount, 0, 3, 0);
            cbTmpCount = tagSearchCardResult6[1];
            if (cbTmpCount > 0) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult6[2][1];
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = tagSearchCardResult6[3][1];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //飞机
            var tagSearchCardResult7 = this.SearchThreeTwoLine(cbCardData, cbCardCount);
            cbTmpCount = tagSearchCardResult7[1];
            if (cbTmpCount > 0) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult7[2][1];
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = tagSearchCardResult7[3][1];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            //炸弹
            if (cbSameCount != 5) {
                var tagSearchCardResult1 = this.SearchSameCard(cbCardData, cbCardCount, 0, 4);
                cbTmpCount = tagSearchCardResult1[1];
                if (cbTmpCount > 0) {
                    cbResultCardCount[cbResultCount] = tagSearchCardResult1[2][1];
                    cbResultCard[cbResultCount] = [];
                    cbResultCard[cbResultCount] = tagSearchCardResult1[3][1];
                    cbResultCount = cbResultCount + 1;
                    tagSearchCardResult[2] = cbResultCardCount;
                    tagSearchCardResult[3] = cbResultCard;
                }
            }
            //火箭
            if ((cbCardCount == 2) && (cbCardData[0] == 0x4F && cbCardData[1] == 0x4E)) {
                cbResultCardCount[cbResultCount] = 2;
                cbResultCard[cbResultCount] = [];
                cbResultCard[cbResultCount] = [0, cbCardData[0], cbCardData[1]];
                cbResultCount = cbResultCount + 1;
                tagSearchCardResult[2] = cbResultCardCount;
                tagSearchCardResult[3] = cbResultCard;
            }
            tagSearchCardResult[1] = cbResultCount - 1;
            return tagSearchCardResult;
        } else if (cbTurnOutType == CT_SINGLE || cbTurnOutType == CT_DOUBLE || cbTurnOutType == CT_THREE) {
            //单牌、对牌、三条
            var cbReferCard = cbTurnCardData[0];
            var cbSameCount = 1;
            if (cbTurnOutType == CT_DOUBLE) {
                cbSameCount = 2;
            } else if (cbTurnOutType == CT_THREE) {
                cbSameCount = 3;
            }
            var tagSearchCardResult21 = this.SearchSameCard(cbCardData, cbCardCount, cbReferCard, cbSameCount);
            cbResultCount = tagSearchCardResult21[1];
            cbResultCount = cbResultCount + 1;
            cbResultCardCount = tagSearchCardResult21[2];
            tagSearchCardResult[2] = cbResultCardCount;
            cbResultCard = tagSearchCardResult21[3];
            tagSearchCardResult[3] = cbResultCard;
            tagSearchCardResult[1] = cbResultCount - 1;
        } else if (cbTurnOutType == CT_SINGLE_LINE || cbTurnOutType == CT_DOUBLE_LINE || cbTurnOutType == CT_THREE_LINE) {
            //单连、对连、三连
            var cbBlockCount = 1;
            if (cbTurnOutType == CT_DOUBLE_LINE) {
                cbBlockCount = 2;
            } else if (cbTurnOutType == CT_THREE_LINE) {
                cbBlockCount = 3;
            }
            var cbLineCount = cbTurnCardCount / cbBlockCount;
            var tagSearchCardResult31 = this.SearchLineCardType(cbCardData, cbCardCount, cbTurnCardData[0], cbBlockCount, cbLineCount);
            cbResultCount = tagSearchCardResult31[1];
            cbResultCount = cbResultCount + 1;
            cbResultCardCount = tagSearchCardResult31[2];
            tagSearchCardResult[2] = cbResultCardCount;
            cbResultCard = tagSearchCardResult31[3];
            tagSearchCardResult[3] = cbResultCard;
            tagSearchCardResult[1] = cbResultCount - 1;
        } else if (cbTurnOutType == CT_THREE_TAKE_ONE || cbTurnOutType == CT_THREE_TAKE_TWO) {
            //三带一单、三带一对
            if (cbCardCount >= cbTurnCardCount) {
                if (cbTurnCardCount == 4 || cbTurnCardCount == 5) {
                    var cbTakeCardCount = (cbTurnOutType == CT_THREE_TAKE_ONE) && 1 || 2;
                    var tagSearchCardResult41 = this.SearchTakeCardType(cbCardData, cbCardCount, cbTurnCardData[2], 3, cbTakeCardCount);
                    cbResultCount = tagSearchCardResult41[1];
                    cbResultCount = cbResultCount + 1;
                    cbResultCardCount = tagSearchCardResult41[2];
                    tagSearchCardResult[2] = cbResultCardCount;
                    cbResultCard = tagSearchCardResult41[3];
                    tagSearchCardResult[3] = cbResultCard;
                    tagSearchCardResult[1] = cbResultCount - 1;
                } else {
                    var cbBlockCount = 3;
                    var cbLineCount = cbTurnCardCount / (cbTurnOutType == CT_THREE_TAKE_ONE && 4 || 5);
                    var cbTakeCardCount = cbTurnOutType == CT_THREE_TAKE_ONE && 1 || 2;
                    //搜索连牌
                    var cbTmpTurnCard = cbTurnCardData;
                    cbTmpTurnCard = this.SortOutCardList(cbTmpTurnCard, cbTurnCardCount);
                    var tmpSearchResult = this.SearchLineCardType(cbCardData, cbCardCount, cbTmpTurnCard[1], cbBlockCount, cbLineCount);
                    var cbResultCount2 = tmpSearchResult[1];
                    //提取带牌
                    for (var i = 1; i <= cbResultCount2; i++) {
                        var cbResultIndex = cbResultCount2 - i + 1;
                        var cbTmpCardData = [];
                        for (var j = 1; j <= cbCardData.length; j++) {
                            cbTmpCardData[j] = cbCardData[j];
                        }
                        var cbTmpCardCount = cbCardCount;
                        //删除连牌
                        var removeResult = this.RemoveCard(tmpSearchResult[3][cbResultIndex], tmpSearchResult[2][cbResultIndex], cbTmpCardData, cbTmpCardCount);
                        cbTmpCardData = removeResult[2];
                        cbTmpCardCount = cbTmpCardCount - tmpSearchResult[2][cbResultIndex];
                        //分析牌
                        var TmpResult = this.AnalysebCardData(cbTmpCardData, cbTmpCardCount);
                        //提取牌
                        var cbDistillCard = [];
                        var cbDistillCount = 0;
                        var j = cbTakeCardCount;
                        while (j <= 4) {
                            if (TmpResult[1][j] > 0) {
                                if (j == cbTakeCardCount && TmpResult[1][j] >= cbLineCount) {
                                    var cbTmpBlockCount = TmpResult[1][j];
                                    for (var k = 1; k <= j * cbLineCount; k++) {
                                        cbDistillCard[k] = TmpResult[2][j][(cbTmpBlockCount - cbLineCount) * j + k];
                                    }
                                    cbDistillCount = j * cbLineCount;
                                    break;
                                } else {
                                    var k = 1;
                                    while (k <= TmpResult[1][j]) {
                                        var cbTmpBlockCount = TmpResult[1][j];
                                        for (var l = 1; l <= cbTakeCardCount; l++) {
                                            cbDistillCard[cbDistillCount + l] = TmpResult[2][j][(cbTmpBlockCount - k) * j + l];
                                        }
                                        cbDistillCount = cbDistillCount + cbTakeCardCount;
                                        //提取完成
                                        if (cbDistillCount == cbTakeCardCount * cbLineCount) {
                                            break;
                                        }
                                        k = k + 1;
                                    }
                                }
                            }
                            //提取完成
                            if (cbDistillCount == cbTakeCardCount * cbLineCount) {
                                break;
                            }
                            j = j + 1;
                        }
                        //提取完成
                        if (cbDistillCount == cbTakeCardCount * cbLineCount) {//复制带牌
                            var cbCount = tmpSearchResult[2][cbResultIndex];
                            for (var n = 1; n <= cbDistillCount; n++) {
                                tmpSearchResult[3][cbResultIndex][cbCount + n] = cbDistillCard[n];
                            }
                            tmpSearchResult[2][cbResultIndex] = tmpSearchResult[2][cbResultIndex] + cbDistillCount;
                        } else {//否则删除连牌
                            tmpSearchResult[2][cbResultIndex] = 0;
                        }
                    }
                    //整理组合
                    tmpSearchResult[1] = cbResultCount2;
                    for (var i = 1; i <= tmpSearchResult[1]; i++) {
                        if (tmpSearchResult[2][i] != 0) {
                            tagSearchCardResult[2][cbResultCount] = tmpSearchResult[2][i];
                            tagSearchCardResult[3][cbResultCount] = tmpSearchResult[3][i];
                            cbResultCount = cbResultCount + 1;
                        }
                    }
                    tagSearchCardResult[1] = cbResultCount - 1;
                }
            }
        } else if (cbTurnOutType == CT_FOUR_TAKE_ONE || cbTurnOutType == CT_FOUR_TAKE_TWO) {
            //四带两单、四带两双
            var cbTakeCardCount = (cbTurnOutType == CT_FOUR_TAKE_ONE) && 1 || 2;
            var cbTmpTurnCard = this.SortOutCardList(cbTurnCardData, cbTurnCardCount);
            var tagSearchCardResult51 = this.SearchTakeCardType(cbCardData, cbCardCount, cbTmpTurnCard[1], 4, cbTakeCardCount);
            cbResultCount = tagSearchCardResult51[1];
            cbResultCount = cbResultCount + 1;
            cbResultCardCount = tagSearchCardResult51[2];
            tagSearchCardResult[2] = cbResultCardCount;
            cbResultCard = tagSearchCardResult51[3];
            tagSearchCardResult[3] = cbResultCard;
            tagSearchCardResult[1] = cbResultCount - 1;
        }
        //搜索炸弹
        if (cbCardCount >= 4 && cbTurnOutType != CT_MISSILE_CARD) {
            var cbReferCard = 0;
            if (cbTurnOutType == CT_BOMB_CARD) {
                cbReferCard = cbTurnCardData[0];
            }
            //搜索炸弹
            var tagSearchCardResult61 = this.SearchSameCard(cbCardData, cbCardCount, cbReferCard, 4);
            var cbTmpResultCount = tagSearchCardResult61[1];
            for (var i = 1; i <= cbTmpResultCount; i++) {
                cbResultCardCount[cbResultCount] = tagSearchCardResult61[2][i];
                tagSearchCardResult[2] = cbResultCardCount;
                cbResultCard[cbResultCount] = tagSearchCardResult61[3][i];
                tagSearchCardResult[3] = cbResultCard;
                cbResultCount = cbResultCount + 1;
            }
            tagSearchCardResult[1] = cbResultCount - 1;
        }
        //搜索火箭
        if ((cbTurnOutType != CT_MISSILE_CARD) && (cbCardCount >= 2) && (cbCardData[0] == 0x4F && cbCardData[1] == 0x4E)) {
            cbResultCardCount[cbResultCount] = 2;
            cbResultCard[cbResultCount] = [0, cbCardData[0], cbCardData[1]];
            cbResultCount = cbResultCount + 1;
            tagSearchCardResult[2] = cbResultCardCount;
            tagSearchCardResult[3] = cbResultCard;
            tagSearchCardResult[1] = cbResultCount - 1;
        }
        return tagSearchCardResult;
    },

    //同牌搜索
    SearchSameCard: function (cbHandCardData, cbHandCardCount, cbReferCard, cbSameCardCount) {
        //结果数目
        var cbResultCount = 1;
        //扑克数目
        var cbResultCardCount = [];
        //结果扑克
        var cbResultCard = [];
        //搜索结果
        var tagSearchCardResult = [null, cbResultCount - 1, cbResultCardCount, cbResultCard];
        //排序扑克
        var cbCardData = this.SortCardList(cbHandCardData, cbHandCardCount, 0);
        var cbCardCount = cbHandCardCount;
        //分析结构
        var tagAnalyseResult = this.AnalysebCardData(cbCardData, cbCardCount);
        var cbReferLogicValue = (cbReferCard == 0 && 0 || this.GetCardLogicValue(cbReferCard));
        var cbBlockIndex = cbSameCardCount;
        while (cbBlockIndex <= 4) {
            for (var i = 1; i <= tagAnalyseResult[1][cbBlockIndex]; i++) {
                var cbIndex = (tagAnalyseResult[1][cbBlockIndex] - i) * cbBlockIndex + 1;
                var cbNowLogicValue = this.GetCardLogicValue(tagAnalyseResult[2][cbBlockIndex][cbIndex]);
                if (cbNowLogicValue > cbReferLogicValue) {
                    cbResultCardCount[cbResultCount] = cbSameCardCount;
                    tagSearchCardResult[2] = cbResultCardCount;
                    cbResultCard[cbResultCount] = [];
                    cbResultCard[cbResultCount][1] = tagAnalyseResult[2][cbBlockIndex][cbIndex];
                    for (var j = 2; j <= cbBlockIndex; j++) {
                        cbResultCard[cbResultCount][j] = tagAnalyseResult[2][cbBlockIndex][cbIndex + j - 1];
                    } //此处修改
                    tagSearchCardResult[3] = cbResultCard;
                    cbResultCount = cbResultCount + 1;
                }
            }
            cbBlockIndex = cbBlockIndex + 1;
        }
        tagSearchCardResult[1] = cbResultCount - 1;
        return tagSearchCardResult;
    },

    //带牌类型搜索(三带一，四带一等)
    SearchTakeCardType: function (cbHandCardData, cbHandCardCount, cbReferCard, cbSameCount, cbTakeCardCount) {
        //结果数目
        var cbResultCount = 1;
        //扑克数目
        var cbResultCardCount = [];
        //结果扑克
        var cbResultCard = [];
        //搜索结果
        var tagSearchCardResult = [null, cbResultCount - 1, cbResultCardCount, cbResultCard];
        if (cbSameCount != 3 && cbSameCount != 4) {
            return tagSearchCardResult;
        }
        if (cbTakeCardCount != 1 && cbTakeCardCount != 2) {
            return tagSearchCardResult;
        }
        if ((cbSameCount == 4) && (cbHandCardCount < cbSameCount + cbTakeCardCount * 2 || cbHandCardCount < cbSameCount + cbTakeCardCount)) {
            return tagSearchCardResult;
        }
        //排序扑克
        var cbCardData = this.SortCardList(cbHandCardData, cbHandCardCount, 0);
        var cbCardCount = cbHandCardCount;
        var sameCardResult = [];
        sameCardResult = this.SearchSameCard(cbCardData, cbCardCount, cbReferCard, cbSameCount);
        var cbSameCardResultCount = sameCardResult[1];
        if (cbSameCardResultCount > 0) {
            //分析结构
            var tagAnalyseResult = this.AnalysebCardData(cbCardData, cbCardCount);
            //需要牌数
            var cbNeedCount = cbSameCount + cbTakeCardCount;
            if (cbSameCount == 4) {
                cbNeedCount = cbNeedCount + cbTakeCardCount;
            }
            //提取带牌
            for (var i = 1; i <= cbSameCardResultCount; i++) {
                var bMere = false;
                var j = cbTakeCardCount;
                while (j <= 4) {
                    var k = 1;
                    while (k <= tagAnalyseResult[1][j]) {
                        //从小到大
                        var cbIndex = (tagAnalyseResult[1][j] - k) * j + 1;
                        if (this.GetCardLogicValue(sameCardResult[3][i][1]) != this.GetCardLogicValue(tagAnalyseResult[2][j][cbIndex])) {
                            //复制带牌
                            var cbCount = sameCardResult[2][i];
                            for (var l = 1; l <= cbTakeCardCount; l++) {
                                sameCardResult[3][i][cbCount + l] = tagAnalyseResult[2][j][cbIndex + l - 1];
                            }
                            sameCardResult[2][i] = sameCardResult[2][i] + cbTakeCardCount;
                            if (sameCardResult[2][i] >= cbNeedCount) {
                                //复制结果
                                cbResultCardCount[cbResultCount] = sameCardResult[2][i];
                                tagSearchCardResult[2] = cbResultCardCount;
                                cbResultCard[cbResultCount] = [];
                                cbResultCard[cbResultCount] = sameCardResult[3][i];
                                tagSearchCardResult[3] = cbResultCard;
                                cbResultCount = cbResultCount + 1;
                                tagSearchCardResult[1] = cbResultCount - 1;
                                bMere = true;
                                //下一组合
                                break;
                            }
                        }
                        k = k + 1;
                    }
                    if (bMere == true) {
                        break;
                    }
                    j = j + 1;
                }
            }
        }
        tagSearchCardResult[1] = cbResultCount - 1;
        return tagSearchCardResult;
    },

    //连牌搜索
    SearchLineCardType: function (cbHandCardData, cbHandCardCount, cbReferCard, cbBlockCount, cbLineCount) {
        //结果数目
        var cbResultCount = 1;
        //扑克数目
        var cbResultCardCount = [];
        //结果扑克
        var cbResultCard = [];
        //搜索结果
        var tagSearchCardResult = [null, cbResultCount - 1, cbResultCardCount, cbResultCard];
        //排序扑克
        var cbCardData = this.SortCardList(cbHandCardData, cbHandCardCount, 0);
        var cbCardCount = cbHandCardCount;
        //连牌最少数
        var cbLessLineCount = 0;
        if (cbLineCount == 0) {
            if (cbBlockCount == 1) {
                cbLessLineCount = 5;
            } else if (cbBlockCount == 2) {
                cbLessLineCount = 3;
            } else {
                cbLessLineCount = 2;
            }
        } else {
            cbLessLineCount = cbLineCount;
        }
        //cc.log("连牌最少数 " .. cbLessLineCount)
        var cbReferIndex = 3;
        if (cbReferCard != 0) {
            if ((this.GetCardLogicValue(cbReferCard) - cbLessLineCount) >= 2) {
                cbReferIndex = this.GetCardLogicValue(cbReferCard) - cbLessLineCount + 1 + 1;
            }
        }
        //超过A
        if (cbReferIndex + cbLessLineCount > 15) {
            return tagSearchCardResult;
        }
        //长度判断
        if (cbHandCardCount < cbLessLineCount * cbBlockCount) {
            return tagSearchCardResult;
        }
        //cc.log("搜索顺子开始点 " .. cbReferIndex);
        var Distributing = this.AnalysebDistributing(cbCardData, cbCardCount);
        //搜索顺子
        var cbTmpLinkCount = 0;
        var cbValueIndex = cbReferIndex;
        var flag = false;
        while (cbValueIndex <= 13) {
            if (cbResultCard[cbResultCount] == null) {
                cbResultCard[cbResultCount] = [];
            }
            if (Distributing[2][cbValueIndex][6] < cbBlockCount) {
                if (cbTmpLinkCount < cbLessLineCount) {
                    cbTmpLinkCount = 0;
                    flag = false;
                } else {
                    cbValueIndex = cbValueIndex - 1;
                    flag = true;
                }
            } else {
                cbTmpLinkCount = cbTmpLinkCount + 1;
                if (cbLineCount == 0) {
                    flag = false;
                } else {
                    flag = true;
                }
            }
            if (flag == true) {
                flag = false;
                if (cbTmpLinkCount >= cbLessLineCount) {
                    //复制扑克
                    var cbCount = 0;
                    var cbIndex = (cbValueIndex - cbTmpLinkCount + 1);
                    while (cbIndex <= cbValueIndex) {
                        var cbTmpCount = 0;
                        var cbColorIndex = 1;
                        while (cbColorIndex <= 4) {//在四色中取一个
                            var cbColorCount = 1;
                            while (cbColorCount <= Distributing[2][cbIndex][5 - cbColorIndex]) {
                                cbCount = cbCount + 1;
                                cbResultCard[cbResultCount][cbCount] = this.MakeCardData(cbIndex, 5 - cbColorIndex - 1);
                                tagSearchCardResult[3][cbResultCount] = cbResultCard[cbResultCount];
                                cbTmpCount = cbTmpCount + 1;
                                if (cbTmpCount == cbBlockCount) {
                                    break;
                                }
                                cbColorCount = cbColorCount + 1;
                            }
                            if (cbTmpCount == cbBlockCount) {
                                break;
                            }
                            cbColorIndex = cbColorIndex + 1;
                        }
                        cbIndex = cbIndex + 1;
                    }
                    tagSearchCardResult[2][cbResultCount] = cbCount;
                    cbResultCount = cbResultCount + 1;
                    if (cbLineCount != 0) {
                        cbTmpLinkCount = cbTmpLinkCount - 1;
                    } else {
                        cbTmpLinkCount = 0;
                    }
                }
            }
            cbValueIndex = cbValueIndex + 1;
        }
        //特殊顺子(寻找A)
        if (cbTmpLinkCount >= cbLessLineCount - 1 && cbValueIndex == 14) {
            //cc.log("特殊顺子(寻找A)");
            if ((Distributing[2][1][6] >= cbBlockCount) || (cbTmpLinkCount >= cbLessLineCount)) {
                if (cbResultCard[cbResultCount] == null) {
                    cbResultCard[cbResultCount] = {};
                }
                //复制扑克
                var cbCount = 0;
                var cbIndex = (cbValueIndex - cbTmpLinkCount);
                while (cbIndex <= 13) {
                    var cbTmpCount = 0;
                    var cbColorIndex = 1;
                    while (cbColorIndex <= 4) {//在四色中取一个
                        var cbColorCount = 1;
                        while (cbColorCount <= Distributing[2][cbIndex][5 - cbColorIndex]) {
                            cbCount = cbCount + 1;
                            cbResultCard[cbResultCount][cbCount] = this.MakeCardData(cbIndex, 5 - cbColorIndex - 1);
                            tagSearchCardResult[3][cbResultCount] = cbResultCard[cbResultCount];
                            cbTmpCount = cbTmpCount + 1;
                            if (cbTmpCount == cbBlockCount) {
                                break;
                            }
                            cbColorCount = cbColorCount + 1;
                        }
                        if (cbTmpCount == cbBlockCount) {
                            break;
                        }
                        cbColorIndex = cbColorIndex + 1;
                    }
                    cbIndex = cbIndex + 1;
                }
                //复制A
                if (Distributing[2][1][6] >= cbBlockCount) {
                    var cbTmpCount = 0;
                    var cbColorIndex = 1;
                    while (cbColorIndex <= 4) {//在四色中取一个
                        var cbColorCount = 1;
                        while (cbColorCount <= Distributing[2][1][5 - cbColorIndex]) {
                            cbCount = cbCount + 1;
                            cbResultCard[cbResultCount][cbCount] = this.MakeCardData(1, 5 - cbColorIndex - 1);
                            tagSearchCardResult[3][cbResultCount] = cbResultCard[cbResultCount];
                            cbTmpCount = cbTmpCount + 1;
                            if (cbTmpCount == cbBlockCount) {
                                break;
                            }
                            cbColorCount = cbColorCount + 1;
                        }
                        if (cbTmpCount == cbBlockCount) {
                            break;
                        }
                        cbColorIndex = cbColorIndex + 1;
                    }
                }
                tagSearchCardResult[2][cbResultCount] = cbCount;
                cbResultCount = cbResultCount + 1;
            }
        }
        tagSearchCardResult[1] = cbResultCount - 1;
        return tagSearchCardResult;
    },

    //飞机搜索
    SearchThreeTwoLine: function (cbHandCardData, cbHandCardCount) {
        //cc.log("飞机搜索");
        //结果数目
        var cbSearchCount = 0;
        //扑克数目
        var cbResultCardCount = [];
        //结果扑克
        var cbResultCard = [];
        //搜索结果
        var tagSearchCardResult = [null, cbSearchCount, cbResultCardCount, cbResultCard];
        var tmpSingleWing = [null, cbSearchCount, cbResultCardCount, cbResultCard];
        var tmpDoubleWing = [null, cbSearchCount, cbResultCardCount, cbResultCard];
        var cbTmpResultCount = 0;
        //搜索连牌
        var tmpSearchResult = this.SearchLineCardType(cbHandCardData, cbHandCardCount, 0, 3, 0);
        cbTmpResultCount = tmpSearchResult[1];
        if (cbTmpResultCount > 0) {
            //提取带牌
            var i = 1;
            while (i <= cbTmpResultCount) {
                var flag = true;
                var cbTmpCardData = [];
                var cbTmpCardCount = cbHandCardCount;
                //不够牌
                if (cbHandCardCount - tmpSearchResult[2][i] < tmpSearchResult[2][i] / 3) {
                    var cbNeedDelCount = 3;
                    while ((cbHandCardCount + cbNeedDelCount - tmpSearchResult[2][i]) < (tmpSearchResult[2][i] - cbNeedDelCount) / 3) {
                        cbNeedDelCount = cbNeedDelCount + 3;
                    }
                    //不够连牌
                    if ((tmpSearchResult[2][i] - cbNeedDelCount) / 3 < 2) {
                        flag = false;
                    } else {
                        flag = true;
                    }
                    if (flag == true) {
                        //拆分连牌
                        var removeResult = this.RemoveCard(tmpSearchResult[3][i], cbNeedDelCount, tmpSearchResult[3][i], tmpSearchResult[2][i]);
                        tmpSearchResult[3][i] = removeResult[2];
                        tmpSearchResult[2][i] = tmpSearchResult[2][i] - cbNeedDelCount;
                    }
                }
                if (flag == true) {
                    flag = false;
                    //删除连牌
                    for (var temp = 1; temp <= cbHandCardData.length; temp++) {
                        cbTmpCardData[temp] = cbHandCardData[temp - 1];
                    }
                    var removeResult1 = this.RemoveCard(tmpSearchResult[3][i], tmpSearchResult[2][i], cbTmpCardData, cbTmpCardCount);
                    cbTmpCardData = removeResult1[2];
                    cbTmpCardCount = cbTmpCardCount - tmpSearchResult[2][i];
                    //组合飞机
                    var cbNeedCount = tmpSearchResult[2][i] / 3;
                    var cbResultCount = tmpSingleWing[1] + 1;
                    tmpSingleWing[3][cbResultCount] = tmpSearchResult[3][i];
                    for (var j = 1; j <= cbNeedCount; j++) {
                        tmpSingleWing[3][cbResultCount][tmpSearchResult[2][i] + j] = cbTmpCardData[cbTmpCardCount - cbNeedCount + j];
                    }
                    tmpSingleWing[2][i] = tmpSearchResult[2][i] + cbNeedCount;
                    tmpSingleWing[1] = tmpSingleWing[1] + 1;
                    var flag2 = true;
                    //不够带翅膀
                    if (cbTmpCardCount < tmpSearchResult[2][i] / 3 * 2) {
                        var cbNeedDelCount = 3;
                        while ((cbTmpCardCount + cbNeedDelCount - tmpSearchResult[2][i]) < (tmpSearchResult[2][i] - cbNeedDelCount) / 3 * 2) {
                            cbNeedDelCount = cbNeedDelCount + 3;
                        }
                        //不够连牌
                        if ((tmpSearchResult[2][i] - cbNeedDelCount) / 3 < 2) {
                            flag2 = false;
                        } else {
                            flag2 = true;
                        }
                        if (flag2 == true) {
                            //拆分连牌
                            var removeResult = this.RemoveCard(tmpSearchResult[3][i], cbNeedDelCount, tmpSearchResult[3][i], tmpSearchResult[2][i]);
                            tmpSearchResult[3][i] = removeResult[2];
                            tmpSearchResult[2][i] = tmpSearchResult[2][i] - cbNeedDelCount;
                            //重新删除连牌
                            for (var temp = 1; temp <= cbHandCardData.length; temp++) {
                                cbTmpCardData[temp] = cbHandCardData[temp];
                            }
                            var removeResult2 = this.RemoveCard(tmpSearchResult[3][i], tmpSearchResult[2][i], cbTmpCardData, cbTmpCardCount);
                            cbTmpCardData = removeResult2[2];
                            cbTmpCardCount = cbTmpCardCount - tmpSearchResult[2][i];
                        }
                    }
                    if (flag2 == true) {
                        flag2 = false;
                        //分析牌
                        var TmpResult = [];
                        TmpResult = this.AnalysebCardData(cbTmpCardData, cbTmpCardCount);
                        //提取翅膀
                        var cbDistillCard = [];
                        var cbDistillCount = 0;
                        var cbLineCount = tmpSearchResult[2][i] / 3;
                        var j = 2;
                        while (j <= 4) {
                            if (TmpResult[1][j] > 0) {
                                if ((j + 1 == 3) && TmpResult[1][j] >= cbLineCount) {
                                    var cbTmpBlockCount = TmpResult[1][j];
                                    for (var k = 1; k <= j * cbLineCount; k++) {
                                        cbDistillCard[k] = TmpResult[2][j][(cbTmpBlockCount - cbLineCount) * j + k];
                                    }
                                    cbDistillCount = j * cbLineCount;
                                } else {
                                    var k = 1;
                                    while (k <= TmpResult[1][j]) {
                                        var cbTmpBlockCount = TmpResult[1][j];
                                        for (var l = 1; l <= 2; l++) {
                                            cbDistillCard[cbDistillCount + l] = TmpResult[2][j][(cbTmpBlockCount - k) * j + l];
                                        }
                                        cbDistillCount = cbDistillCount + 2;
                                        //提取完成
                                        if (cbDistillCount == 2 * cbLineCount) {
                                            break;
                                        }
                                        k = k + 1;
                                    }
                                }
                            }
                            //提取完成
                            if (cbDistillCount == 2 * cbLineCount) {
                                break;
                            }
                            j = j + 1;
                        }
                        //提取完成
                        if (cbDistillCount == 2 * cbLineCount) {
                            //cc.log("复制两对");
                            //复制翅膀
                            tmpDoubleWing[1] = tmpDoubleWing[1] + 1;
                            cbResultCount = tmpDoubleWing[1];
                            tmpDoubleWing[3][cbResultCount] = tmpSearchResult[3][i];
                            for (var n = 1; n <= cbDistillCount; n++) {
                                tmpDoubleWing[3][cbResultCount][tmpSearchResult[2][i] + n] = cbDistillCard[n];
                            }
                            tmpDoubleWing[2][cbResultCount] = tmpSearchResult[2][i] + cbDistillCount;
                        }
                    }
                }
                i = i + 1;
            }
            //复制结果
            for (var m = 1; m <= tmpDoubleWing[1]; m++) {
                tagSearchCardResult[1] = tagSearchCardResult[1] + 1;
                var cbResultCount = tagSearchCardResult[1];
                tagSearchCardResult[3][cbResultCount] = tmpDoubleWing[3][m];
                tagSearchCardResult[2][cbResultCount] = tmpDoubleWing[2][m];
            }
            for (var m = 1; m <= tmpSingleWing[1]; m++) {
                tagSearchCardResult[1] = tagSearchCardResult[1] + 1;
                var cbResultCount = tagSearchCardResult[1];
                tagSearchCardResult[3][cbResultCount] = tmpSingleWing[3][m];
                tagSearchCardResult[2][cbResultCount] = tmpSingleWing[2][m];
            }
        }
        return tagSearchCardResult;
    },

    //分析分布
    AnalysebDistributing: function (cbCardData, cbCardCount) {
        var cbCardCount1 = 0;
        var cbDistributing = [];
        for (var i = 1; i <= 15; i++) {
            var distributing = [];
            for (var j = 1; j <= 6; j++) {
                distributing[j] = 0;
            }
            cbDistributing[i] = distributing;
        }
        var Distributing = [cbCardCount1, cbDistributing];
        for (var i = 1; i <= cbCardCount; i++) {
            if (cbCardData[i - 1] != 0) {
                var cbCardColor = this.GetCardColor(cbCardData[i - 1]);
                var cbCardValue = this.GetCardValue(cbCardData[i - 1]);
                //分布信息
                cbCardCount1 = cbCardCount1 + 1;
                cbDistributing[cbCardValue][5 + 1] = cbDistributing[cbCardValue][6] + 1;
                var color = (cbCardColor >> 4) + 1;
                cbDistributing[cbCardValue][color] = cbDistributing[cbCardValue][color] + 1;
            }
        }
        Distributing[1] = cbCardCount1;
        Distributing[2] = cbDistributing;
        return Distributing;
    },

    //构造扑克
    MakeCardData: function (cbValueIndex, cbColorIndex) {
        //print("构造扑克 " ..bit:_or(bit:_lshift(cbColorIndex,4),cbValueIndex)..",".. GetCardLogicValue(bit:_or(bit:_lshift(cbColorIndex,4),cbValueIndex)))
        return (cbColorIndex << 4) | cbValueIndex;
    },

    //-删除扑克
    RemoveCard: function (cbRemoveCard, cbRemoveCount, cbCardData, cbCardCount) {
        var cbDeleteCount = 0;
        var cbTempCardData = [];
        for (var i = 1; i <= cbCardData.length; i++) {
            cbTempCardData[i] = cbCardData[i - 1];
        }
        var result = [null, false, cbCardData];
        //置零扑克
        var i = 1;
        while (i <= cbRemoveCount) {
            var j = 1;
            while (j < cbCardCount) {
                if (cbRemoveCard[i] == cbTempCardData[j]) {
                    cbDeleteCount = cbDeleteCount + 1;
                    cbTempCardData[j] = 0;
                    break;
                }
                j = j + 1;
            }
            i = i + 1;
        }
        if (cbDeleteCount != cbRemoveCount) {
            return result;
        }
        //清理扑克
        var cbCardPos = 1;
        var datas = [];
        for (var i = 1; i <= cbCardCount; i++) {
            if (cbTempCardData[i] != 0) {
                datas[cbCardPos] = cbTempCardData[i];
                cbCardPos = cbCardPos + 1;
            }
        }
        result = [null, true, datas];
        return result;
    },

    //排列扑克
    SortOutCardList: function (cbCardData, cbCardCount) {
        var resultCardData = [];
        var resultCardCount = 0;
        //获取牌型
        var cbCardType = this.GetCardType(cbCardData, cbCardCount);
        if (cbCardType == CT_THREE_TAKE_ONE || cbCardType == CT_THREE_TAKE_TWO) {
            //分析牌
            var AnalyseResult = [];
            AnalyseResult = this.AnalysebCardData(cbCardData, cbCardCount);
            resultCardCount = AnalyseResult[1][3] * 3;
            resultCardData = AnalyseResult[2][3];
            for (var i = 4; i <= 1; i--) {
                if (i != 3) {
                    if (AnalyseResult[1][i] > 0) {
                        for (var j = 1; j <= AnalyseResult[1][i] * i; j++) {
                            resultCardData[resultCardCount + j] = AnalyseResult[2][i][j];
                        }
                        resultCardCount = resultCardCount + AnalyseResult[1][i] * i;
                    }
                }
            }
        } else if (cbCardType == CT_FOUR_TAKE_ONE || cbCardType == CT_FOUR_TAKE_TWO) {
            //分析牌
            var AnalyseResult = {};
            AnalyseResult = this.AnalysebCardData(cbCardData, cbCardCount);
            resultCardCount = AnalyseResult[1][4] * 4;
            resultCardData = AnalyseResult[2][4];
            for (var i = 4; i <= 1; i--) {
                if (i != 3) {
                    if (AnalyseResult[1][i] > 0) {
                        for (var j = 1; j <= AnalyseResult[1][i] * i; j++) {
                            resultCardData[resultCardCount + j] = AnalyseResult[2][i][j];
                        }
                        resultCardCount = resultCardCount + AnalyseResult[1][i] * i;
                    }
                }
            }
        }
        return resultCardData;
    },
};


export default GameLogicController;