凤眠

先行后知,知行合一

导航

斗地主相关算法实现

Posted on 2017-05-12 17:14  凤眠  阅读(1465)  评论(0编辑  收藏  举报
  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Title</title>
  6     <script>
  7         var PokerType = {
  8             danzhang: 1,
  9             duizi: 2,
 10             sanzhang: 3,
 11             sandaiyi: 4,
 12             sandaiyidui: 5,
 13             shunzi: 6,
 14             liandui: 7,
 15             feiji: 8,
 16             sidaier: 9,
 17             sidailiangdui: 10,
 18             zhadan: 11,
 19             wangzha: 12,
 20             error: 13
 21         };
 22 
 23         var Poker = {
 24             sort: function (pokers) {
 25                 pokers.sort(function (prev, next) {
 26                     return next - prev
 27                 })
 28             },
 29 
 30             getPokerValue: function (poker) {
 31                 if (poker == 52) {
 32                     return 16
 33                 }
 34 
 35                 if (poker == 53) {
 36                     return 17
 37                 }
 38 
 39                 return Math.floor(poker / 4) + 3
 40             },
 41 
 42             getPokerType: function (pokers) {
 43                 var len = pokers.length;
 44                 var i = 0;
 45                 var countPoker = 0;
 46                 var duizi = false;
 47                 var sanzhang = false;
 48                 var zhadan = false;
 49                 var tempArray = [];
 50 
 51                 if (len == 1) {
 52                     return PokerType.danzhang
 53                 }
 54 
 55                 if (len == 2) {
 56                     if (pokers[0] == 53 && pokers[1] == 52) {
 57                         return PokeType.wangzha
 58                     }
 59 
 60                     if (this.getPokerValue(pokers[0]) == this.getPokerValue(pokers[1])) {
 61                         return PokerType.duizi
 62                     }
 63                 }
 64 
 65                 if (len == 3 && this.getPokerValue(pokers[0]) == this.getPokerValue(pokers[1]) && this.getPokerValue(pokers[1]) == this.getPokerValue(pokers[2])) {
 66                     return PokerType.sanzhang;
 67                 }
 68 
 69                 if (len == 4) {
 70                     var countFirstPoker = this.getPokerCount(pokers, pokers[0])
 71                     if (countFirstPoker == 4) {
 72                         return PokerType.zhadan
 73                     }
 74 
 75                     if (countFirstPoker == 3 || this.getPokerCount(pokers, pokers[1]) == 3) {
 76                         return PokeType.sandaiyi
 77                     }
 78                 }
 79 
 80                 if (len == 5) {
 81                     sanzhang = false;
 82                     duizi = false;
 83                     for (i = 0; i < len; i++) {
 84                         countPoker = this.getPokerCount(pokers, pokers[i])
 85                         if (countPoker == 3) {
 86                             sanzhang = true
 87                         }
 88 
 89                         if (countPoker == 2) {
 90                             duizi = true
 91                         }
 92                     }
 93 
 94                     if (sanzhang && duizi) {
 95                         return PokerType.sandaiyidui
 96                     }
 97                 }
 98 
 99                 if (len >= 5 && this.shunzi(pokers)) {
100                     return PokerType.shunzi;
101                 }
102 
103                 if (len == 6) {
104                     if (pokers[0] == 53 && pokers[1] == 52) {
105                         return PokerType.error
106                     }
107 
108                     zhadan = false;
109                     for (i = 0; i < len; i++) {
110                         if (this.getPokerCount(pokers, pokers[i]) == 4) {
111                             zhadan = true
112                         }
113                     }
114 
115                     if (zhadan) {
116                         return PokerType.sidaier
117                     }
118                 }
119 
120                 if (len >= 6 && len % 2 == 0) {
121                     duizi = true;
122                     for (i = 0; i < len; i++) {
123                         if (this.getPokerCount(pokers, pokers[i]) != 2) {
124                             duizi = false;
125                             break
126                         }
127                     }
128 
129                     tempArray = [];
130                     if (duizi) {
131                         for (i = 0; i < len / 2; i++) {
132                             tempArray[i] = pokers[i * 2]
133                         }
134 
135                         if (this.shunzi(tempArray)) {
136                             return PokerType.liandui
137                         }
138                     }
139                 }
140 
141                 if (len >= 6 && len % 3 == 0) { // 飞机不带
142                     sanzhang = true;
143                     for (i = 0; i < len; i++) {
144                         if (this.getPokerCount(pokers, pokers[i]) != 3) {
145                             sanzhang = false;
146                             break
147                         }
148                     }
149 
150                     tempArray = [];
151                     if (sanzhang) {
152                         for (i = 0; i < len / 3; i++) {
153                             tempArray[i] = pokers[i * 3]
154                         }
155 
156                         if (this.shunzi(tempArray)) {
157                             return PokerType.feiji
158                         }
159                     }
160                 }
161 
162                 if (len == 8) {
163                     duizi = false;
164                     zhadan = false;
165                     for (i = 0; i < len; i++) {
166                         countPoker = this.getPokerCount(pokers, pokers[i]);
167                         if (countPoker == 2) {
168                             duizi = true;
169                         } else if (countPoker == 4) {
170                             zhadan = true;
171                         } else {
172                             duizi = false;
173                             break
174                         }
175                     }
176 
177                     if (duizi && zhadan) {
178                         return PokerType.sidailiangdui
179                     }
180                 }
181 
182                 if (len >= 8 && len % 4 == 0) {
183                     if (pokers[0] == 53 && pokers[1] == 52) {
184                         return PokerType.error
185                     }
186 
187                     tempArray = [];
188                     for (i = 0; i < len; i++) {
189                         countPoker = this.getPokerCount(pokers, pokers[i]);
190                         if (countPoker == 3) {
191                             tempArray.push(pokers[i])
192                         }
193                     }
194 
195                     if (tempArray.length == len / 4 * 3 && this.getPokerType(tempArray) == PokerType.feiji) {
196                         return PokerType.feiji
197                     }
198                 }
199 
200                 if (len >= 10 && len % 5 == 0) {
201                     duizi = false;
202 
203                     tempArray = [];
204                     for (i = 0; i < len; i++) {
205                         countPoker = this.getPokerCount(pokers, pokers[i]);
206                         if (countPoker == 2) {
207                             duizi = true;
208                         } else if (countPoker == 3) {
209                             tempArray.push(pokers[i])
210                         } else {
211                             duizi = false;
212                             break
213                         }
214                     }
215 
216                     if (duizi && tempArray.length == len / 5 * 3 && this.getPokerType(tempArray) == PokerType.feiji) {
217                         return PokerType.feiji
218                     }
219                 }
220 
221                 return PokerType.error
222             },
223 
224             shunzi: function (pokers) {
225                 var pokeValue = this.getPokerValue(pokers[0]);
226                 if (pokeValue >= 15) { // 2,大、小王
227                     return false
228                 }
229 
230                 for (var i = 1; i < pokers.length; i++) {
231                     var pokeValue2 = this.getPokerValue(pokers[i]);
232                     if (pokeValue - pokeValue2 != 1) {
233                         return false
234                     }
235 
236                     pokeValue = pokeValue2
237                 }
238 
239                 return true
240             },
241 
242             getPokerCount: function (pokers, poker) {
243                 var count = 0;
244                 for (var i = 0; i < pokers.length; i++) {
245                     if (this.getPokerValue(pokers[i]) == this.getPokerValue(poker)) {
246                         count++
247                     }
248                 }
249 
250                 return count
251             },
252         }
253 
254         var pokers = [];
255 
256         // 0 代表方块3 1 代表梅花3 2 代表红桃3 3 代表黑桃3
257         for (var i = 0; i < 54; i++) {
258             pokers[i] = i
259         }
260 
261         pokers = [0, 4, 8, 12, 16]; // 3、4、5、6、7
262         pokers = [0, 1, 2, 4, 5, 6]; // 333、444
263         pokers = [0, 1, 2, 3, 4, 5, 8, 9]; // 3333、 44、55
264         pokers = [0, 1, 2, 3, 4, 5, 6, 7]; // 3333、 4444
265         pokers = [0, 1, 2, 4, 5, 6, 8, 9]; // 333、444、55
266 
267         Poker.sort(pokers);
268 
269         if (Poker.shunzi(pokers)) {
270             console.log("顺子:", pokers)
271         }
272 
273         console.log("牌型:" , Poker.getPokerType(pokers))
274     </script>
275 </head>
276 <body>
277 
278 </body>
279 </html>