癞子麻将胡牌判断

// 牌值
var cardValue = [
1,2,3,4,5,6,7,8,9,
11,12,13,14,15,16,17,18,19,
21,22,23,24,25,26,27,28,29,
31,41,51,61,71,81,91
]
// 定扑
function isPu(cards, laizinum) {
if (cards.length == 0) {return true;}
// 顺子判断
var index = 1;
if (cards[0] < 30){ index = 1;}
if(cards[0] > 30){ index = 10;}
for (var i = 0; i < 3; i++) {
var first = cards[0] - 2index + iindex
// 顺子第一张牌不会大于7点、无赖子情况下顺子第一张只能用手上的牌
if(first % 10 > 7 || (laizinum == 0 && first < cards[0])){continue;}
// 顺子长度
var shunCount = 0;
for (var j = 0; j < 3; j++) {
if (cards.indexOf(first + jindex) >= 0) {
shunCount++;
}
}
if (shunCount == 3 || shunCount + laizinum >= 3) {
// 西北跟中发不成顺 51,61,71,81
if ((first < 30 && first + 1
index < 30 && first + 2index < 30) ||
(first > 30 && first < 70 && first + 1
index < 70 && first + 2index < 70) ||
(first > 70 && first + 1
index < 100 && first + 2index < 100)) {
var puCards = cards.slice();
var puLaizi = laizinum;
for (var j = 0; j < 3; j++) {
var deleteValue = first + j
index;
var deletePos = puCards.indexOf(deleteValue);
if (deletePos >= 0) {
puCards.splice(deletePos, 1);
}
else {
puLaizi–;
}
}
if (isPu(puCards, puLaizi)) {
// 剩下的牌成扑
return true;
}
}
}
}
// 刻子判断
var keziCount = 1;
var keziCard = cards[0];
if (cards[1] == keziCard) {
keziCount++;
}
if (cards[2] == keziCard) {
keziCount++;
}
if (keziCount == 3 || keziCount + laizinum >= 3) {
var puCards = cards.slice();
var puLaizi = laizinum;
for (var i = 0; i < 3; i++) {
var deletePos = puCards.indexOf(keziCard);
if (deletePos >= 0) {
puCards.splice(deletePos, 1);
}
else {
puLaizi–;
}
}
if (isPu(puCards, puLaizi)) {
return true;
}
}
return false;
}

// 定将
function isJiang(cards, laizinum) {
// 依次删除一对牌做将,其余牌全部成扑则可胡
for (var i = 0; i < cards.length; i++) {
// 和上一次是同样的牌,避免重复计算
if (i > 0 && cards[i] == cards[i - 1]){
continue;
}
// 找到对子、或是用一张癞子拼出的对子
if ((i + 1 < cards.length && cards[i] == cards[i + 1]) || laizinum > 0) {
var puCards = cards.slice();
var puLaizi = laizinum;
puCards.splice(i, 1);
if (puCards[i] == cards[i]) {
puCards.splice(i, 1);
}
else {
puLaizi–;
}
// 删去对子判断剩下的牌是否成扑
if (isPu(puCards, puLaizi)) {
return true;
}
}
}
// 两个癞子做将牌
if (laizinum >= 2 && isPu(cards, laizinum - 2)) {
return true;
}
}

// 判断胡牌(东南西北 中发白成顺)
function isLaiziHu(cards, laizinum) {
// 若牌张数不是2、5、8、11、14则不能胡
if ((cards.length + laizinum + 1) % 3 != 0) {
return false;
}
// 排序
cards.sort(function(a, b) {return a - b;})
var isHu = isJiang(cards, laizinum)
if (isHu){
return true
}
return false;
}

你可能感兴趣的:(js)