牌型大小:
五小牛 > 五花牛 > 炸弹 > 银牛 > 牛牛 > 有牛(牛987654321) > 没牛,K > Q > J ……2 > A, 黑桃♠️ > 红桃♥️ > 梅花♣️ > 方块♦️,同样的牌型比大小,同样的手牌比花色.
1 /** 2 * 在cocoscreator里导入为插件使用 3 * 插件不支持es6,注意需要按照es5规则写 4 */ 5 "use strict"; 6 7 /** 8 * 定义手牌类型(按照大小从小到大排序) 9 * @type {{TYPE_NONE: number, TYPE_NIU_1: number, TYPE_NIU_2: number, 10 * TYPE_NIU_3: number, TYPE_NIU_4: number, TYPE_NIU_5: number, 11 * TYPE_NIU_6: number, TYPE_NIU_7: number, TYPE_NIU_8: number, 12 * TYPE_NIU_9: number, TYPE_NIUNIU: number, TYPE_SILVER: number, 13 * TYPE_BOOM: number, TYPE_FLOWER: number, TYPE_FIVES: number}} 14 */ 15 var HandsType = { 16 TYPE_NONE: 0, // 没牛, 任意三张牌的和都不是10的倍数 17 TYPE_NIU_1: 1, // 牛1 18 TYPE_NIU_2: 2, 19 TYPE_NIU_3: 3, 20 TYPE_NIU_4: 4, 21 TYPE_NIU_5: 5, 22 TYPE_NIU_6: 6, 23 TYPE_NIU_7: 7, 24 TYPE_NIU_8: 8, 25 TYPE_NIU_9: 9, 26 TYPE_NIUNIU: 10, // 牛牛, 任意3张和为10的倍数,剩余2张和也为10的倍数 27 TYPE_SILVER: 11, // 银牛, 五张全是10、J、Q、K 28 TYPE_BOOM: 12, // 炸弹, 四张点数相同 29 TYPE_FLOWER: 13, // 5花牛, 五张全是J、Q、K 30 TYPE_FIVES: 14 // 5小牛(5张加起来小于等于10) 31 }; 32 33 /** 34 * 35 * @param p{Number} 1-13 (A-K) 36 * @param s{Number} 1-4 (1方块diamond、2梅花club、3红桃heart、4黑桃spade) 37 * @constructor 38 */ 39 function CardObj(p, s) { 40 this.point = p; // 牌面点数 41 42 this.suit = s; // 牌面花色 43 } 44 45 /** 46 * 手牌类型返回对象 47 * @param type{Number} 手牌类型 48 * @param mCard{CardObj} 手里的最大牌 49 * @param ncards{Array} 组成牛的三张牌, 手牌分成两组方便展示 50 * @param pcards{Array} 决定点数的两张牌 51 * @constructor 52 */ 53 function TypeReturn(type, mCard, ncards, pcards) { 54 this.handsType = type; // 手牌类型 55 56 this.maxCard = mCard; // 最大牌 57 58 this.nCards = ncards; // 组成牛的牌 59 60 this.pCards = pcards; // 决定点数的牌 61 } 62 63 /** 64 * 创建一副牌,牌面A-K 65 * 默认已经洗牌 66 * @return {Array} 67 */ 68 function create1pairPoker(isShuffle) { 69 var cards = []; 70 71 for (var i = 1; i <= 13; i++) { 72 for (var j = 1; j <= 4; j++) { 73 cards.push(new CardObj(i, j)); 74 } 75 } 76 77 if (isShuffle) { 78 cards = shuffle(cards); 79 } 80 81 return cards; 82 } 83 /** 84 * 洗牌 85 * @param arr{Array} 86 * @return {*} 87 */ 88 function shuffle(arr) { 89 var i, j, temp; 90 91 for (i = arr.length - 1; i > 0; i--) { 92 j = Math.floor(Math.random() * (i + 1)); 93 temp = arr[i]; 94 arr[i] = arr[j]; 95 arr[j] = temp; 96 } 97 98 return arr; 99 } 100 101 /*** 102 * 从大到小排序手牌 103 * @param cardsArr{Array} 手牌数组 104 */ 105 function sortBig2Samll(cardsArr) { 106 cardsArr.sort(function (c1, c2) { 107 return c2.point - c1.point; 108 }); 109 return cardsArr; 110 } 111 112 /** 113 * 判定手牌类型 114 * @param cardsArr{Array} 要判定的手牌信息数组 115 * @return {TypeReturn} 116 */ 117 function getHandsType(cardsArr) { 118 var len = cardsArr.length; 119 if (!cardsArr || len < 1 || len > 5) return new TypeReturn(HandsType.TYPE_NONE, cardsArr[0], cardsArr, []); 120 sortBig2Samll(cardsArr); 121 var totalPoint = 0; 122 var realTotalPoint = 0; 123 var bigJ = true; 124 var big10 = true; 125 126 cardsArr.forEach((card)=>{ 127 totalPoint += card.point <= 10 ? card.point : 10; 128 realTotalPoint += card.point; 129 if (card.point < 11){ 130 bigJ = false; 131 } 132 if (card.point < 10){ 133 big10 = false; 134 } 135 }); 136 137 // 判断牌型,判断顺序不能变,依次从大到小判断5小牛、5花牛、炸弹、银牛、牛牛、有牛、没牛 138 if (totalPoint <= 10) { 139 console.log("五小牛"); 140 return new TypeReturn(HandsType.TYPE_FIVES, cardsArr[0], cardsArr, []); 141 } 142 143 if (bigJ) { 144 console.log("五花牛"); 145 return new TypeReturn(HandsType.TYPE_FLOWER, cardsArr[0], cardsArr, []); 146 } 147 // 牌型是4炸的话最大牌取炸弹牌,比如5555J取5,方便比较大小 148 if (realTotalPoint - cardsArr[len - 1].point === cardsArr[0].point * 4) { 149 console.log("炸弹"); 150 return new TypeReturn(HandsType.TYPE_BOOM, cardsArr[0], cardsArr, []); 151 } else if (realTotalPoint - cardsArr[0].point === cardsArr[len - 1].point * 4) { 152 console.log("炸弹"); 153 return new TypeReturn(HandsType.TYPE_BOOM, cardsArr[len - 1], cardsArr, []); 154 } 155 156 if (big10) { 157 console.log("银牛"); 158 return new TypeReturn(HandsType.TYPE_SILVER, cardsArr[0], cardsArr, []); 159 } 160 161 var lave = totalPoint % 10; 162 163 for (var i = 0; i < len - 1; i++) { 164 var ret = 0; 165 166 for (var j = i + 1; j < len; j++) { 167 ret = (cardsArr[i].point <= 10 ? cardsArr[i].point : 10) + (cardsArr[j].point <= 10 ? cardsArr[j].point : 10); 168 169 if (ret % 10 === lave) { 170 var cardPre = []; 171 var cardSuf = []; 172 173 for (var k = 0; k < len; k++) { 174 if (k != i && k != j) { 175 cardPre.push(cardsArr[k]); 176 } else { 177 cardSuf.push(cardsArr[k]); 178 } 179 } 180 181 if (lave === 0) { 182 console.log("牛牛"); 183 return new TypeReturn(HandsType.TYPE_NIUNIU, cardsArr[0], cardsArr, []); 184 } 185 186 console.log("牛", lave); 187 return new TypeReturn(HandsType["TYPE_NIU_" + lave], cardsArr[0], cardPre, cardSuf); 188 } 189 } 190 } 191 192 console.log("没牛."); 193 return new TypeReturn(HandsType.TYPE_NONE, cardsArr[0], cardsArr, []); 194 } 195 196 /** 197 * 比较两组手牌大小 198 * @param cards1{Array} 199 * @param cards2{Array} 200 * @return {Boolean} true 表示 cards1 大于 cards2 201 */ 202 function compareCards(cards1, cards2) { 203 var typeReturn1 = getHandsType(cards1); 204 var typeReturn2 = getHandsType(cards2); 205 return compareHandsReturn(typeReturn1, typeReturn2); 206 } 207 208 /** 209 * 比较两个手牌类型大小 210 * @param typeReturn1{TypeReturn} 211 * @param typeReturn2{TypeReturn} 212 */ 213 function compareHandsReturn(typeReturn1, typeReturn2) { 214 if (typeReturn1.handsType !== typeReturn2.handsType) { 215 return typeReturn1.handsType > typeReturn2.handsType; 216 } else { 217 if (typeReturn1.maxCard.point !== typeReturn2.maxCard.point) { 218 return typeReturn1.maxCard.point > typeReturn2.maxCard.point; 219 } else { 220 return typeReturn1.maxCard.suit > typeReturn2.maxCard.suit; 221 } 222 } 223 }