一.首先模拟我的自定义的扑克牌的数据结构:
1 //扑克牌结构 2 function Poker(id,value,type,order,color){ 3 this.id = id; 4 this.value = value;//value 1-13代表 A-K 14代表大小王 5 this.type = type;//type 1,2,3,4 分别代表红桃、方块、黑桃、梅花四种花色 6 this.order = order;//排序和大小比较标志 7 this.color = color;//颜色:0红色 1黑色 8 //name属性表示名称 规则用不到 9 switch(value){ 10 case 1: 11 this.name = "A"; 12 break; 13 case 2: 14 this.name = "2"; 15 break; 16 case 3: 17 this.name = "3"; 18 break; 19 case 4: 20 this.name = "4"; 21 break; 22 case 5: 23 this.name = "5"; 24 break; 25 case 6: 26 this.name = "6"; 27 break; 28 case 7: 29 this.name = "7"; 30 break; 31 case 8: 32 this.name = "8"; 33 break; 34 case 9: 35 this.name = "9"; 36 break; 37 case 10: 38 this.name = "10"; 39 break; 40 case 11: 41 this.name = "J"; 42 break; 43 case 12: 44 this.name = "Q"; 45 break; 46 case 13: 47 this.name = "K"; 48 break; 49 case 14: 50 this.name = "JOKER"; 51 break; 52 default: 53 break; 54 } 55 }
二.模拟一副扑克牌
1 function getPokers(){ 2 var value,type,order,name,color,pokers = []; 3 for(var i=0;i<54;i++){ 4 value = Math.floor(i/4)+1; 5 type = i%4+1; 6 if([1,2,14].indexOf(value) >=0){ 7 order = value + 13; 8 } 9 else 10 order = value; 11 color = Math.ceil(type/2) - 1; 12 if([52,53].indexOf(i) >=0){ 13 type = null;//大小王不需要类型 用颜色标识 14 color = i%2; 15 } 16 pokers.push(new Poker(i,value,type,order,color)); 17 } 18 for(var index in pokers){ 19 var item = pokers[index]; 20 var t; 21 switch(item.type){ 22 case 1: 23 t = "a"; 24 break; 25 case 2: 26 t = "b"; 27 break; 28 case 3: 29 t = "c"; 30 break; 31 case 4: 32 t = "d"; 33 break; 34 default: 35 t = "w"; 36 break; 37 } 38 if(item.value === 14) 39 window[t+item.value+item.color] = item; 40 else 41 window[t+item.value] = item; 42 } 43 //以下是测试数据 方便测试规则用 44 _1san = [a3,c3,b3], 45 _2san = [a1,d1,b1], 46 _1san1 = [a3,c3,b3,a2], 47 _2san1 = [a1,d1,b1,c4], 48 _1san2 = [a3,c3,b3,a2,b2], 49 _2san2 = [a1,d1,b1,c4,b4], 50 _1si2 = [a3,c3,b3,b3,a2,b2], 51 _2si2 = [a1,d1,b1,c1,c4,b4], 52 _1zha = [a3,c3,b3,b3], 53 _2zha = [a1,d1,b1,c1], 54 _1ld = [a3,c3,b4,c4,a5,d5], 55 _2ld = [a12,c12,b13,c13,a1,d1], 56 _1dui = [a3,c3], 57 _2dui = [a1,d1], 58 _1sz = [a3,b4,d5,a6,a7], 59 _2sz = [a10,b11,d12,a13,a1], 60 _1fj = [a3,c3,b3,a2,a4,c4,b4,a6], 61 _2fj = [a13,c13,b13,a3,a1,c1,b1,a4], 62 _3fj = [a3,c3,b3,a2,c2,a4,c4,b4,a6,b6], 63 _4fj = [a13,c13,b13,a3,c3,a1,c1,b1,a4,b4], 64 _5fj = [a3,c3,b3,a2,c2,a4,c4,b4,a6,a5,b5,d5,a13,c13,b6], 65 _6fj = [a13,a12,c12,d12,a11,d11,c13,b13,a3,c3,a1,c1,b1,a4,b4], 66 _1dan = [c3], 67 _2dan = [c1]; 68 }
【注】:为了方便测试我将一副扑克牌放到了全局变量a1-a13,b1-b13,c1-c13,d1-d13 + w140+w141 a表示红桃 b表示方块 c表示黑桃 d表示梅花 w表示大小王
三.公共方法编写及介绍:
1.扩展Array对象,添加移除方法(可以没有代码功能需要自己实现):
1 //自定义数组移除方法 2 Object.defineProperties(Array.prototype,{ 3 'removeAt':{ 4 value:function(index){ 5 return this.splice(index,1); 6 }, 7 enumerable:false, 8 writable:false, 9 configurable:false 10 }, 11 'remove':{ 12 value:function(item){ 13 var index = this.indexOf(item); 14 if(index >= 0) 15 return this.removeAt(index); 16 }, 17 enumerable:false, 18 writable:false, 19 configurable:false 20 } 21 })
2.扑克牌排序(按照order字段和花色type字段排序)
1 /**