扑克判断是不是顺子

扑克的每一张牌都对应一个这样的card对象

for (var i = 0; i < 54; i++)
    PAIID2CARD[i] = new Card(i);
// 每个牌 都对应一个如此的对象,服务器传过来个number可以通过这个找到这个牌多对应的每一张单牌
var Card = function (number) {
    var type = Math.floor(Math.floor(number / 13) + 0.5); //前13张牌为type1, 红桃 方片 梅花 黑桃 大小王 五种类型, (赖子另算)
    var type1 = type + 1;
    var value = number - type * 13 + 1;
    var weight = value;
    if (type1 == 5)
        weight = value + 15;
    else if (value <= 2)
        weight = value + 13;
    var voice = weight - 3;
    return {
        type: type1,
        value: value,
        number: number,
        name: type + "_" + value,
        weight: weight,
        voice: voice,
        changeToNumber: number % 13,
        isLaizi: false
    };
};

1.首先对数组进行排序,从大到小,或者从小到大都可以,

list.sort(function (a, b) {
    return a.weight - b.weight
});

2.然后判断数组的元素是不是都是单个元素,

var getMax = function (list) {
    var card_index = [];

    for (var i = 0; i < 4; i++)
        card_index[i] = [];

    var count = [];// 1-13各算一种,王算第14种
    for (var i = 0; i < 14; i++)
        count[i] = 0;

    for (var i = 0; i < list.length; i++) {
        if (list[i].type == 5) {
            count[13]++;
        } else {
            var v = list[i].value;
            if (v <= 2) {
                v += 13;
            }
            count[v - 3]++;
        }
    }

    for (var i = 0; i < 14; i++) {
        switch (count[i]) {
            case 1:
                card_index[0].push(i + 3);
                break;
            case 2:
                card_index[1].push(i + 3);
                break;
            case 3:
                card_index[2].push(i + 3);
                break;
            case 4:
                card_index[3].push(i + 3);
                break;
        }
    }

    return card_index;
};
var ci = getMax(list);

var arr0 = ci[0] || []; //单张牌的shu zu
var arr1 = ci[1] || [];
var arr2 = ci[2] || [];
var arr3 = ci[3] || [];
 if (arr0.length == list.length && isSequenceArr(arr0))   return 顺子;// 顺子

3.然后判断是不是顺子,第一元素的值减去最后一个元素的值 等雨数组的长度-1 就是顺子了

var isSequenceArr = function (list) {
    var result = false;
    var count = list.length;
    if (count != 0) {
        var first = list[0];
        var last = list[count - 1];
        if (Math.abs(first - last) == count - 1 && (last < 15) && first < 15) {
            result = true;
        }
    }
    return result;
};

 
 

你可能感兴趣的:(cocos2d-js)