-
Notifications
You must be signed in to change notification settings - Fork 1
/
yaku.js
609 lines (548 loc) · 15 KB
/
yaku.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/**
* 门前清自摸和, Menzen(chin) tsumo, All concealed
*
*
* Must be concealed : yes
* Han : 1
*/
function menzentsumo(handInfo) {
if (handInfo.menzen && handInfo.tsumo) {
handInfo.yaku.push({menzentsumo: 1});
}
}
/**
* 立直, Riichi, Ready Hand
*
*
* Must be concealed : yes
* Han : 1
*/
function riichi(handInfo) {
if (handInfo.riichi) {
handInfo.yaku.push({riichi: 1});
}
}
/**
* 断幺九, Tanyaochuu, All simples
* Consisting only of suit tiles 2-8 (without terminal or honor tiles)
*
* Must be concealed : no (some rules say yes)
* Han : 1
*/
function tanyaochuu(handInfo) {
let re_wo = /[19z]/;
if (handInfo.winHand.join('').match(re_wo) == null) {
handInfo.yaku.push({tanyaochuu: 1});
}
}
/**
* 役牌, Yaku hai, Value triplet set
* having a pon or kan of round wind/seat wind/dragons
*
* Must be concealed : no
* Han : 1 for each
*/
function yakuhai(handInfo) {
let re1 = new RegExp("(" + sangen.join('|') + "){3}", "g");
let yaku_sangen = handInfo.winHand.join('').match(re1);
for (let e in yaku_sangen) {
let sangenName = "yakuhai: " + yaku_sangen[e].substr(0,2);
handInfo.yaku.push({[sangenName]: 1});
}
let re2 = new RegExp("(" + handInfo.bakaze + "){3}", "g");
let yaku_bakaze = handInfo.winHand.join('').match(re2);
for (let e in yaku_bakaze) {
let bakazeName = "yakuhai: " + yaku_bakaze[e].substr(0,2);
handInfo.yaku.push({[bakazeName]: 1});
}
let re3 = new RegExp("(" + handInfo.jikaze + "){3}", "g");
let yaku_jikaze = handInfo.winHand.join('').match(re3);
for (let e in yaku_jikaze) {
let jikazeName = "yakuhai: " + yaku_jikaze[e].substr(0,2);
handInfo.yaku.push({[jikazeName]: 1});
}
}
/**
* 清老头, Chinroutou, All termainals
* Consisting only of terminal tiles (1 and 9)
*
* Must be concealed : no
* Han : yakuman
*/
function chinroutou(handInfo) {
let re = /[19]/g;
let digit = handInfo.winHand.join('').match(re);
if (digit !== null && digit.length >= 14) {
handInfo.yakuman.push({chinroutou: "yakuman"});
}
}
/**
* 混老头, Honroutou, All terminals and honors
* Consisting only of terminal tiles (1 and 9) and honors
*
* Must be concealed : no
* Han : 2
*/
function honroutou(handInfo) {
let re_wo = /[2-8][mps]/g;
let re_w = /z/g;
if (handInfo.winHand.join('').match(re_wo) == null &&
handInfo.winHand.join('').match(re_w) !== null) {
handInfo.yaku.push({honroutou: 2});
}
}
/**
* 纯全带幺九, Jun chan(tai yaochu), Terminal in each set
*
*
* Must be concealed : no
* Han : 3 (concealed) / 2 (open)
*/
function junchan(handInfo) {
let re1 = /[19]/g;
let re_shuntsu = /[28]/g;
let junchan = false;
for (let i = 0; i < 5; i++) {
if (handInfo.winHand[i].match(re1) == null) {
junchan = false;
break;
}
junchan = true;
}
if (junchan && handInfo.winHand.join('').match(re_shuntsu) !== null) {
handInfo.yaku.push({junchan: 3}); //fuuro -1
}
}
/**
* 混全带幺九, (Hon) chantai (yaochu), Terminal or honor in each set
*
*
* Must be concealed : no
* Han : 2 (concealed) / 1 (open)
*/
function chantai(handInfo) {
let re1 = /[19]/g;
let rez = /z/g;
let re_shuntsu = /[28]/g;
let chantai = false;
// 手牌数组里必须含有字和一组顺子, 且数组里的每个元素: 不含19就含字
if (handInfo.winHand.join('').match(rez) !== null &&
handInfo.winHand.join('').match(re_shuntsu) !== null) {
for (let i = 0; i < 5; i++) {
if (handInfo.winHand[i].match(re1) == null) {
if (handInfo.winHand[i].match(rez) == null) {
chantai = false;
break;
} else {
chantai = true;
}
} else {
chantai = true;
}
}
}
if (chantai) {
handInfo.yaku.push({chantai: 2}); //fuuro -1
}
}
/**
* 小三元, Shousangen, Little three dragons
* Having two kous/pons/kans of dragons, plus one pair of the third.
*
* Must be concealed : no
* Han : 2
*/
function shousangen(handInfo) {
let re = /5z|6z|7z/g;
let digit = handInfo.winHand.join('').match(re);
if (digit !== null && digit.length == 8 ) { //没考虑杠
handInfo.yaku.push({shousangen: 2});
}
}
/**
* 大三元, Daisangen, Big three dragons
* Having one kou/pon/kan of each dragon tile.
*
* Must be concealed : no
* Han : yakuman
*/
function daisangen(handInfo) {
let re = /5z|6z|7z/g;
let digit = handInfo.winHand.join('').match(re);
if (digit !== null && digit.length == 9 ) { //没考虑杠
handInfo.yakuman.push({daisangen: "yakuman"});
}
}
/**
* 小四喜, Shousuushii, Little four winds
* Having three kous/pons/kans of winds, plus a pair of the fourth.
*
* Must be concealed : no
* Han : yakuman
*/
function shousuushii(handInfo) {
let re = /1z|2z|3z|4z/g;
let digit = handInfo.winHand.join('').match(re);
if (digit !== null && digit.length == 11 ) { //没考虑杠
handInfo.yakuman.push({shousuushii: "yakuman"});
}
}
/**
* 大四喜, Daisuushii, Big four winds
* Having one kou/pon/kan of each wind tile.
*
* Must be concealed : no
* Han : yakuman
*/
function daisuushii(handInfo) {
let re = /1z|2z|3z|4z/g;
let digit = handInfo.winHand.join('').match(re);
if (digit !== null && digit.length == 12 ) { //没考虑杠
handInfo.yakuman.push({daisuushii: "yakuman"});
}
}
/**
* 平和, Pinfu, Flat hand
*
* Must be concealed : yes
* Han : 1
*/
function pinfu(handInfo) {
let yakuhai = sangen.concat(handInfo.bakaze, handInfo.jikaze);
let re_yaku = new RegExp(yakuhai.join('|'), "g");
let pinfu = false;
if (handInfo.winHand.join('').match(re_yaku) == null && handInfo.menzen == true) {
for (let i=0; i<4; i++) {
let digit = handInfo.winHand[i].match(/\d/g);
if (digit[0] == digit[2]) {
pinfu = false;
break;
}
pinfu = true;
}
}
if (pinfu) {
handInfo.yaku.push({pinfu: 1});
}
}
/**
* 三色同顺, Sanshoku doujun, Triple sequence
*
* Must be concealed : no
* Han : 2 (concealed) / 1 (open)
*/
function sanshokudoujun(handInfo) {
let storedShuntsu = [];
let numOfDoujun = 0;
let winHand = handInfo.winHand;
for (let i=0; i<winHand.length; i++) {
winHand[i] = winHand[i].replace(/0/g, '5');
}
for (let i=0; i<4; i++) {
let mentsu = winHand[i];
let digit = mentsu.match(/\d/g);
if (digit[0] !== digit[2]) {
for (let j=0; j<storedShuntsu.length; j++) {
if (storedShuntsu[j] !== mentsu &&
storedShuntsu[j].match(/\d/g).join('') == digit.join('')) {
numOfDoujun += 1;
}
}
if (storedShuntsu.indexOf(mentsu) < 0) {
storedShuntsu.push(mentsu);
}
}
}
if (numOfDoujun >= 3) {
handInfo.yaku.push({sanshokudoujun: 2}); // fuuro -1
}
}
/** 一杯口, Iipeikou, Double sequence
*
* Must be concealed : yes
* Han : 1
*
* 二杯口, ryanpeikou, Twice double sequence
*
* Must be concealed : yes
* Han : 3
*/
function peikou(handInfo) {
let peikou = 0;
let winHand = handInfo.winHand;
for (let i=0; i<winHand.length; i++) {
winHand[i] = winHand[i].replace(/0/g, '5');
}
let countedMentsu = winHand.reduce(function (acc, cur) {
acc[cur] = (acc[cur] + 1) || 1; // (undefined + 1 || 1) 结果为1
return acc;
}, {});
for (let e in countedMentsu) {
if (countedMentsu[e] == 2) {
peikou += 1;
}
}
if (peikou == 1) {
handInfo.yaku.push({iipeikou: 1}); //menzen
}
if (peikou == 2) {
handInfo.yaku.push({ryanpeikou: 3}); //menzen
}
}
/**
* 一气通贯, Ikkitsuukan (/ Ikki / Ittsuu), Straight
*
* Must be concealed : no
* Han : 2 (concealed) / 1 (open)
*/
function ikki(handInfo) {
let ikki = false;
let winHand = handInfo.winHand;
for (let i=0; i<winHand.length; i++) {
winHand[i] = winHand[i].replace(/0/g, '5');
}
// 按花色对面子分类
let sortedMentsu = winHand.reduce(function (acc, cur) {
let digit = cur.match(/\d/g).join('');
let type = cur.match(/[mps]/);
if (!acc[type]) { // !undefined 结果为true
acc[type] = [];
}
acc[type].push(digit);
return acc;
}, {});
for (let e in sortedMentsu) {
if (sortedMentsu[e].indexOf('123') >= 0 &&
sortedMentsu[e].indexOf('456') >= 0 &&
sortedMentsu[e].indexOf('789') >= 0) {
ikki = true;
}
}
if (ikki) {
handInfo.yaku.push({ikkitsuukan: 2}); //fuuro -1
}
}
/**
* 清一色, Chinitsu, Pure flush
*
* Must be concealed : no
* Han : 6 (concealed) / 5 (open)
*/
function chinitsu(handInfo) {
let winHand = handInfo.winHand.join('');
if (winHand.match(/[mpz]/g) == null ||
winHand.match(/[msz]/g) == null ||
winHand.match(/[spz]/g) == null) {
handInfo.yaku.push({chinitsu: 6}); //fuuro -1
}
}
/**
* 混一色, Honitsu, Mixed flush
*
* Must be concealed : no
* Han : 3 (concealed) / 2 (open)
*/
function honitsu(handInfo) {
let winHand = handInfo.winHand.join('');
if (winHand.match(/z/g) !== null && (
winHand.match(/[mp]/g) == null ||
winHand.match(/[ms]/g) == null ||
winHand.match(/[sp]/g) == null)) {
handInfo.yaku.push({honitsu: 3}); //fuuro -1
}
}
/**
* 字一色, Tsuuiisou, All honor
*
* Must be concealed : no
* Han : yakuman
*/
function tsuuiisou(handInfo) {
let re = /z/g;
let digit = handInfo.winHand.join('').match(re);
if (digit !== null && digit.length >= 14) {
handInfo.yakuman.push({tsuuiisou: "yakuman"});
}
}
/**
* 绿一色, Ryuuiisou, All green
*
* Must be concealed : no
* Han : yakuman
*/
function ryuuiisou(handInfo) {
let re_wo = /[^23468]s|[^6]z|\dm|\dp/g;
if (handInfo.winHand.join('').match(re_wo) == null) {
handInfo.yakuman.push({ryuuiisou: "yakuman"});
}
}
/**
* 九莲宝灯, Chuuren poutou, Nine gates
*
* Must be concealed : yes
* Han : yakuman
*/
function chuurenpoutou(handInfo) {
let chuurenpoutou = false;
let base = "1112345678999";
let winHand = handInfo.winHand;
for (let i=0; i<winHand.length; i++) {
winHand[i] = winHand[i].replace(/0/g, '5');
}
let digit = winHand.join('').match(/\d/g).sort().join('');
if (winHand.join('').match(/[mpz]/g) == null ||
winHand.join('').match(/[msz]/g) == null ||
winHand.join('').match(/[spz]/g) == null) {
for (let i=1; i<10; i++) {
if (base.concat(i.toString()).split('').sort().join('') == digit) {
chuurenpoutou = true;
break;
}
chuurenpoutou = false;
}
}
if (chuurenpoutou) {
handInfo.yakuman.push({chuurenpoutou: "yakuman"}); // menzen
}
}
/**
* 对对和, Toitoi (hou), All triplet
*
* Must be concealed : must be open
* Han : 2
*/
function toitoi(handInfo) {
let toitoi = false;
for (let i=0; i<4; i++) {
let digit = handInfo.winHand[i].match(/\d/g);
if (digit[0] !== digit[2]) {
toitoi = false;
break;
}
toitoi = true;
}
if (toitoi && handInfo.fuuro) {
handInfo.yaku.push({toitoi: 2});
}
}
/**
* 四暗刻, Suuankou, Four concealed triplets
*
* Must be concealed : yes
* Han : yakuman
*/
function suuankou(handInfo) {
let toitoi = false;
for (let i=0; i<4; i++) {
let digit = handInfo.winHand[i].match(/\d/g);
if (digit[0] !== digit[2]) {
toitoi = false;
break;
}
toitoi = true;
}
if (toitoi && handInfo.menzen) {
handInfo.yakuman.push({suuankou: "yakuman"});
}
}
/**
* 三色同刻, Sanshoku doukou, Three colour triplets
*
* Must be concealed : no
* Han : 2
*/
function sanshokudoukou(handInfo) {
let winHand = handInfo.winHand;
let storedDoukou = winHand.reduce(function (acc, cur) {
let digit = cur.match(/\d/g);
let type = cur.match(/[mps]/);
if (type !== null && digit[0] == digit[2]) {
if (acc.length == 0 || acc.indexOf(digit.join('')) >= 0) {
acc.push(digit.join(''));
}
}
return acc;
}, []);
if (storedDoukou.length == 3) {
handInfo.yaku.push({sanshokudoukou: 2});
}
}
/**
* 三暗刻, Sanankou, Three concealed triplets
*
* Must be concealed : no
* Han : 2
*/
// function sanankou(handInfo) {
// let sanankou = false;
// let winHand = handInfo.winHand;
// for (let i=0; i<winHand.length; i++) {
// winHand[i] = winHand[i].replace(/0/g, '5');
// }
// if (handInfo.fuuroAt !== null && handInfo.fuuroAt.length < 2) {
// let storedKoutsu = winHand.reduce(function (acc, cur, ind) {
// let digit = cur.match(/\d/g);
// if (digit[0] == digit[2]) {
// acc[ind] = cur;
// }
// return acc;
// }, {});
// for (let e in storedKoutsu) {
// if (Object.keys(storedKoutsu).length < 3 ||
// handInfo.fuuroAt.indexOf(parseInt(e)) >= 0) {
// sanankou = false;
// break;
// }
// sanankou = true;
// }
// }
// if (sanankou) {
// handInfo.yaku.push({sanankou: 2});
// }
// }
/**
* 特殊和形: 七对子, 国士无双
*
* 特殊和形为直接生成, 不通过四面子一雀头随机选取, 因此翻种则直接给出, 不需要进行判断.
* 这里仍补充检验函数, 仅作为参考.
*/
/**
* 七对子, Chiitoitsu, Seven pairs
*
* Must be concealed : yes
* Han : 2
* Fu : 25
*/
function chiitoitsu(handInfo) {
let winHand = handInfo.winHand.join('').replace(/0/g, '5').match(/\d[mpsz]/g).sort();
// 按牌张分类, value为出现个数
let countedTiles = winHand.reduce(function (acc, cur) {
acc[cur] = (acc[cur] + 1) || 1; // (undefined + 1 || 1) 结果为1
return acc;
}, {});
// 但是不能为两杯口 pending
if (Object.values(countedTiles).join('') == '2222222') { // 分类过的牌张value为7个2
handInfo.yaku.push({chiitoitsu: 2});
}
}
/**
* 国士无双, Kokushi musou, Thirteen orphans
*
* Must be concealed : yes
* Han : yakuman
*/
function kokushimusou(handInfo) {
let kokushimusou = false;
let base = ["1m","9m","1p","9p","1s","9s","1z","2z","3z","4z","5z","6z","7z"];
let tiles = handInfo.winHand.join('').match(/\d[mpsz]/g).sort();
for (let i=0; i<14; i++) {
if (base.concat(base[i]).sort().join('') == tiles.join('')) {
kokushimusou = true;
break;
}
kokushimusou = false;
}
if (kokushimusou) {
handInfo.yakuman.push({kokushimusou: "yakuman"});
}
}