编辑代码

//var oHands = [1,1,1,2,3,5,5,6,7,7,15,24,24,25];
// var oHands = [4,5,7,9,9,14,15,16,17,22,22,23,25,25];
 //var oHands = [12,13,15,17,17,21,22,24,24,27,28,28,29];
//var oHands = [6,7,11,12,14,15,16,22,25,25,26,28,29,71];
var oHands = [4,6,7,8,9,13,13,16,17,19,24]
//获取手牌中某个牌的个数
function getCardCount(oCards, card){
    var count = 0;
    for(var i = 0; i < oCards.length; i++){
        if(oCards[i] == card){
            count++;
        }
    }
    return count;
}


//是否有临近关联的牌
function isAssociationCard(oHands,card){
    if(oHands.indexOf(card) == -1 ) return false;
    if(card >= 31) return false;
    if(card % 10  == 1){
        if((oHands.indexOf(card + 1) != -1) || oHands.indexOf(card + 2) != -1)
            return true;
    }
    else if(card % 10  == 9){
        if((oHands.indexOf(card - 1) != -1) || oHands.indexOf(card - 2) != -1)
            return true;
    }
    else{
        if(oHands.indexOf(card + 1) != -1 || oHands.indexOf(card + 2) != -1 || oHands.indexOf(card - 1) != -1 || oHands.indexOf(card - 2) != -1){
            return true;
        }
    }
    
    return false;
}


//判断是否有1、9、凤牌
var dan19FArr = [];
var danPArr = [];//普通的单牌
var huncard = 71;
for(var i = 0; i < oHands.length;i++){
    if(oHands[i] != huncard && getCardCount(oHands,oHands[i]) == 1){
        //字牌没有关联牌
        if(oHands[i] >= 31){
            dan19FArr.push(oHands[i]);
        }
        if(isAssociationCard(oHands,oHands[i])) continue;
        //console.log("单牌=========================1====",oHands[i])
        if(oHands[i] % 10 == 1 || oHands[i] % 10 == 9){
            dan19FArr.push(oHands[i]);
        }
        else{
            danPArr.push(oHands[i]);
        }
    }
}

if(dan19FArr.length > 0){
    console.log("无关联的1、9、凤牌========================1====",dan19FArr)
    //获取张数最少得打
    return;
}

//判断无关联单牌中是否有2、8的单牌
for(var i = 0; i < danPArr.length;i++){
    if(danPArr[i] % 10 == 2 || danPArr[i] % 10 == 8){
        return danPArr[i];
    }
}


//判断顺子牌
//4张连续的顺子2头张数少打哪一张(4,5,6,7万)
var shunZi4L = [];
for(var i = 0; i < oHands.length;i++){
    //console.log("oHands[i]========================2====",oHands[i])
    if(oHands[i] == huncard  || oHands[i] >= 31 || getCardCount(oHands,oHands[i]) > 1) continue;
    //console.log("oHands[i]========================1====",oHands[i])
    if(getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i] -2) == 0 &&
    getCardCount(oHands,oHands[i] + 1) == 1 && getCardCount(oHands,oHands[i] + 2) == 1 && 
    getCardCount(oHands,oHands[i] + 3) == 1 && getCardCount(oHands,oHands[i] + 4) == 0 && 
    getCardCount(oHands,oHands[i] + 5) == 0){
        shunZi4L.push(oHands[i]);
        shunZi4L.push(oHands[i] + 1);
        shunZi4L.push(oHands[i] + 2);
        shunZi4L.push(oHands[i] + 3);
        logger.debug("4张连续顺子========================1====",shunZi4L)
        var maxCount = getCardCount(oHands,shunZi4L[0]);
        var maxCard = shunZi4L[0];
        if(getCardCount(oHands,shunZi4L[3])  > maxCount){
            maxCount = getCardCount(oHands,shunZi4L[3]);
            maxCard = shunZi4L[3];
        }
        return maxCard;
    }  
}
if(shunZi4L.length > 0){
    console.log("4张连续顺子========================1====",shunZi4L)
    return;
}

//3张间断的顺子
    for(var i = 0; i < oHands.length;i++){
        //console.log("oHands[i]========================2====",oHands[i])
       if(oHands[i] == huncard  || oHands[i] >= 31 || getCardCount(oHands,oHands[i]) > 1) continue;
       // if(getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i] -2) == 0 &&
       //     getCardCount(oHands,oHands[i] + 1) == 1 && getCardCount(oHands,oHands[i] + 2) == 0 && 
       //     getCardCount(oHands,oHands[i] + 3) == 1 && getCardCount(oHands,oHands[i] + 4) == 0 && 
       //     (getCardCount(oHands,oHands[i] + 5) == 0 || getCardCount(oHands,oHands[i] + 5) == 2)){
       //     var shunziArr = [oHands[i],oHands[i] + 1,oHands[i] + 3]
       //     shunZi3.push(shunziArr);
       // }
   
       // if(getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i] -2) == 0 && 
       //     getCardCount(oHands,oHands[i] + 1) == 0 && getCardCount(oHands,oHands[i] + 2) == 1 &&
       //     getCardCount(oHands,oHands[i] + 3) == 1 && getCardCount(oHands,oHands[i] + 4) == 0 && 
       //     (getCardCount(oHands,oHands[i] + 5) == 0 || getCardCount(oHands,oHands[i] + 5) == 2)){
       //     var shunziArr = [oHands[i],oHands[i] + 2,oHands[i] + 3]
       //     shunZi3.push(shunziArr);
       // }
        if(getCardCount(oHands,oHands[i] - 2) == 0 && getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i]) == 1 && getCardCount(oHands,oHands[i] + 1) == 1 && getCardCount(oHands,oHands[i] + 2) == 0 && (getCardCount(oHands,oHands[i] + 3) == 1  || getCardCount(oHands,oHands[i] + 3) == 2) &&
        getCardCount(oHands,oHands[i] + 4) == 0  && getCardCount(oHands,oHands[i] + 5) == 0){
            if(oHands[i] % 10 == 1){
                logger.debug("3张间断的顺子============222============333===",oHands[i])
                return oHands[i];       // 1 2 4打1
            }
            else{
                if(getCardCount(oHands,oHands[i] + 3) == 1){
                    logger.debug("3张间断的顺子============222============444===",oHands[i] + 3)
                    return oHands[i] + 3;
                }  
            }      
        }

        if(getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i]) == 1 && getCardCount(oHands,oHands[i] + 1) == 0 && getCardCount(oHands,oHands[i] + 2) == 1 && getCardCount(oHands,oHands[i] + 3) == 1 ){
            logger.debug("3张间断的顺子============222============1====",oHands[i])
            return oHands[i]; 
        }
       
    }

    //顺子中带对子
    for(var i = 0; i < oHands.length;i++){
        if(oHands[i] == huncard  || oHands[i] >= 31) continue;
        //  if(getCardCount(oHands,oHands[i]) == 2){
        //      console.log(" oHands[i]===============66666=========1====", oHands[i])
        //  }
            
        if(getCardCount(oHands,oHands[i]) == 2 && getCardCount(oHands,oHands[i] +1) == 1 && getCardCount(oHands,oHands[i] +2) == 1 &&
            getCardCount(oHands,oHands[i] +3) == 0 &&  getCardCount(oHands,oHands[i] +4) == 0
        ){
            logger.debug(" 顺子中带对子===============222=========1====", oHands[i])
            return oHands[i];
        }

        if(getCardCount(oHands,oHands[i] - 2) == 0 &&  getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i]) == 1 && getCardCount(oHands,oHands[i] +1) == 2 && getCardCount(oHands,oHands[i] +2) == 1 && 
            (getCardCount(oHands,oHands[i] +3) == 0 || getCardCount(oHands,oHands[i] +3) == 2)){
                logger.debug(" 顺子中带对子===============222=========2====", oHands[i] +1)
            return oHands[i] + 1;
        }
        //7899万不能有56
        if(getCardCount(oHands,oHands[i] - 1) == 0 && getCardCount(oHands,oHands[i] - 2) == 0 && getCardCount(oHands,oHands[i]) == 1 && 
            getCardCount(oHands,oHands[i] +1) == 1 && getCardCount(oHands,oHands[i] +2) == 2 && getCardCount(oHands,oHands[i] +3) == 0){
                logger.debug(" 顺子中带对子===============222=========3====", oHands[i] +2)
            return oHands[i] + 2;
        }
    }

    //5连顺子 加一个间隔牌 12345 7
    for(var i = 0; i < oHands.length;i++){
        if(oHands[i] == huncard  || oHands[i] >= 31) continue;
        if(getCardCount(oHands,oHands[i]) == 1 && getCardCount(oHands,oHands[i] + 1) == 1 && getCardCount(oHands,oHands[i] + 2) == 1 &&
            getCardCount(oHands,oHands[i] + 3) == 1 &&  getCardCount(oHands,oHands[i] + 4) == 1 && getCardCount(oHands,oHands[i] + 5) == 0 &&
            getCardCount(oHands,oHands[i] + 6) == 1 && getCardCount(oHands,oHands[i] + 7) == 0 && getCardCount(oHands,oHands[i] + 8) == 0
        ){
            return oHands[i] + 6;
        }
    }
    if(danPArr.length > 0){
        logger.debug("无关联的普通单牌========================1====",danPArr)
        //有无关联的单牌先出牌
        var maxCount = getCardCount(oHands,danPArr[0]);
        var maxCard = danPArr[0];
        if(danPArr.length > 1){
            for(var i = 0; i < danPArr.length; i++){
                if(getCardCount(oHands,danPArr[i]) > maxCount){
                    maxCount = getCardCount(oHands,danPArr[i]);
                    maxCard = danPArr[i];
                }
            }
        }
        return maxCard;
    }