forked from TLeaves/MahjongLogic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogic.lua
More file actions
2412 lines (2179 loc) · 71.5 KB
/
Copy pathLogic.lua
File metadata and controls
2412 lines (2179 loc) · 71.5 KB
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- 更新历史
-- 2014.9.19 小弘 测试了四川麻将各种胡法
-- 2014.9.20 jim 更新了ValidHu()中ValidAA()的限制条件,修复了112233的情况
-- 2014.9.26 小弘 更新了三元牌的检测,用于加番计算。
-- 2014.10.28 小弘 添加广东麻将AI出牌边界限定
-- 2014.10.30 小弘 添加四川麻将听牌检测Beta,未合并重复项(未充分测试)
-- 2014.10.31 小弘 进一步完善四川麻将听牌,已合并重复项
-- 2015.1.23 小弘 把weight评估值从取最小值修改为负数值之和,使评估更完善,从而修复此前AI不能杠的情况。
-- 并且添加AI出牌阶段的分级模式,使其一定概率随意出牌,AI从强到弱依次是0,1,2,3级
-- 2015.1.27 小弘 修复一个因为吃碰杠中因为没有对手牌排序而导致的无法转换出序号的bug,在转换前加了一个排序。
-- 还有把AI出牌里剔除非手牌的操作提到最前。
-- 2015.3.10 小弘 合并大量重复的代码,新增一堆注释,未完全测试
-- 2015.3.11 小弘 修改了暗杠
----------------------- 广东麻将胡法 -------------------
--CheckJH() --鸡胡 什么牌都可以胡,可吃碰杠
--CheckPH() --平胡 全部都是顺子没有刻子
--CheckPPH() --碰碰胡 全部是刻子没有顺子
--CheckHYS() --混一色 整副牌由字牌及另外单一花色(筒、条或万)组成
--CheckQYS() --清一色 整副牌由同一花色组成
--CheckHP() --碰混 混一色 + 碰碰胡
--CheckQP() --清碰 清一色 + 碰碰胡
--CheckHYJ() --混幺九 由幺九牌和字牌组成的牌型
--CheckXSY() --小三元 拿齐中、发、白三种三元牌,但其中一种是将
--CheckXSX() --小四喜 胡牌者完成东、南、西、北其中三组刻子,一组对子
--CheckZYS() --字一色 由字牌组合成的刻子牌型
--CheckQYJ() --清幺九 只由幺九两种牌组成的刻子牌型
--CheckDSX() --大三元 胡牌时,有中、发、白三组刻子
--CheckDSX() --大四喜 胡牌者完成东、南、西、北四组刻子
--CheckJLBD() --九莲宝灯 同种牌形成 1112345678999 ,在摸到该种牌任何一张即可胡牌,不计清一色
--CheckSSY() --十三幺 1 、 9 万筒索,东、南、西、北、中、发、白;以上牌型任意一张牌作将
----------------------- 四川麻将胡法 -------------------
-- CheckPh_SC() --平胡 普通的四个搭子一对将
-- CheckDdz_SC() --大对子 四个搭子均为三张一样的牌
-- CheckQys_SC() --清一色 胡牌时只有一色牌
-- CheckDy_SC() --带幺 所有牌都带1或9
-- CheckAqd_SC() --暗七对 特殊胡牌类型,不遵循四个搭子一对将,胡牌时为7个对子
-- CheckQdd_SC() --清大对 清一色+大对子
-- CheckLqd_SC() --龙七对 暗七对的改进,七对中有两对(或更多)相同,可视作带根的暗七对。
-- CheckQqd_SC() --清七对 清一色+暗七对
-- CheckQdy_SC() --清带幺 清一色+带幺
-- CheckQlqd_SC() --清龙七对 清一色+龙七对
local MJ_WAN = 1 --万
local MJ_TIAO = 2 --条
local MJ_BING = 3 --饼
local MJ_FENG = 4 --东南西北(1357)
local MJ_ZFB = 5 --中发白(135)
local Pai_MING = 0 --明
local Pai_AN = 1 --暗
local Pai_My = 0 --手牌组
local Pai_Chi = 1 --吃牌组
local Pai_Peng = 2 --碰牌组
local Pai_Gang = 3 --杠牌组
local Pai_Ting = 4 --听牌组
math.randomseed(tostring(os.time()):reverse():sub(1,6))
--检查牌的明暗或者空
local function CheckSinglePaiMingAn(pai)
return math.floor(pai%10000/1000)
end
--检查单张牌所属牌组
local function CheckSinglePaiGroup(pai)
return math.floor(pai%1000/100)
end
--检查单张牌的类型,万饼筒条
local function CheckSinglePaiType(pai)
return math.floor(pai%100/10)
end
--检查单张牌的数值
local function CheckSinglePaiNum(pai)
return math.floor(pai%10)
end
--返回标准牌型数值(包括牌型与数字)
local function GetPaiTypeNum(pai)
return math.floor(pai%100)
end
--检测一对
local function CheckAAPai(iValue1,iValue2)
if iValue1 == iValue2 then return true
else return false
end
end
--检测三连张
local function CheckABCPai(iValue1,iValue2,iValue3)
if (iValue1 == iValue2-1)and(iValue2 == iValue3-1) then return true
else return false
end
end
--检测三重张
local function CheckAAAPai(iValue1,iValue2,iValue3)
local p12 = CheckAAPai(iValue1,iValue2)
local p23 = CheckAAPai(iValue2,iValue3)
if p12 and p23 then return true
else return false
end
end
-- 将用户的牌分成 万,条,饼,风,中发白五组并排序返回
local function SortByType(userPai)
local sort_pai = {
["My"] = {
[MJ_WAN] = {},
[MJ_TIAO] = {},
[MJ_BING] = {},
[MJ_FENG] = {},
[MJ_ZFB] = {}
},--手牌组
["Chi"] = {
[MJ_WAN] = {},
[MJ_TIAO] = {},
[MJ_BING] = {},
[MJ_FENG] = {},
[MJ_ZFB] = {}
},--吃牌组
["Peng"] = {
[MJ_WAN] = {},
[MJ_TIAO] = {},
[MJ_BING] = {},
[MJ_FENG] = {},
[MJ_ZFB] = {}
},--碰牌组
["Gang"] = {
[MJ_WAN] = {},
[MJ_TIAO] = {},
[MJ_BING] = {},
[MJ_FENG] = {},
[MJ_ZFB] = {}
},--杠牌组
["Ting"] = {
[MJ_WAN] = {},
[MJ_TIAO] = {},
[MJ_BING] = {},
[MJ_FENG] = {},
[MJ_ZFB] = {}
}--听牌组
}
for i = 1,#userPai,1 do
if CheckSinglePaiGroup(userPai[i]) == Pai_My then
local paiType = CheckSinglePaiType(userPai[i])
table.insert(sort_pai["My"][paiType],userPai[i])
end
if CheckSinglePaiGroup(userPai[i]) == Pai_Chi then
local paiType = CheckSinglePaiType(userPai[i])
table.insert(sort_pai["Chi"][paiType],userPai[i])
end
if CheckSinglePaiGroup(userPai[i]) == Pai_Peng then
local paiType = CheckSinglePaiType(userPai[i])
table.insert(sort_pai["Peng"][paiType],userPai[i])
end
if CheckSinglePaiGroup(userPai[i]) == Pai_Gang then
local paiType = CheckSinglePaiType(userPai[i])
table.insert(sort_pai["Gang"][paiType],userPai[i])
end
if CheckSinglePaiGroup(userPai[i]) == Pai_Ting then
local paiType = CheckSinglePaiType(userPai[i])
table.insert(sort_pai["Ting"][paiType],userPai[i])
end
end
for i = 1,5,1 do
table.sort(sort_pai["My"][i])
table.sort(sort_pai["Chi"][i])
table.sort(sort_pai["Peng"][i])
table.sort(sort_pai["Gang"][i])
table.sort(sort_pai["Ting"][i])
end
return sort_pai
end
--复制一副牌并返回
--提示:由于lua传递table时是传递引用,所以在函数内部修改table会影响到外部的table,因此用此函数拷贝一份table
local function CopyPai(userPai)
local t_pai = {}
for i = 1,#userPai do
table.insert(t_pai,userPai[i])
end
return t_pai
end
--测试胡AA牌(将牌)
local function ValidAA(pai,i,n)
if i + 1 <= n and pai[i] == pai[i+1] then
return true
else
return false
end
end
--测试胡AAA牌(刻子)
local function ValidAAA(pai,i,n)
if i + 2 <= n and pai[i] == pai[i+1] and pai[i] == pai[i+2] then
return true
else
return false
end
end
--测试胡ABC牌(顺子)
local function ValidABC(pai,i,n)
-- 顺子要避开 1 222 3 这种可能组成 123 22 的情况
-- 所以拆成两个列表 (1 2 3)|(22)
-- 然后判断 ValidHu(22)
local t_pai = CopyPai(pai)
-- 只有两张牌,必定没顺
if n - i < 2 then
return false
end
local found_B = false
local found_C = false
for j = i+1,#t_pai,1 do
if found_B == false and t_pai[j] == ( t_pai[i] + 1 ) then
found_B = true
-- 交换两张牌的位置
local t = t_pai[i + 1]
t_pai[i + 1] = t_pai[j]
t_pai[j] = t
end
end
for k = i + 2,#pai,1 do
if found_C== false and t_pai[k] == ( t_pai[i] + 2 ) then
found_C = true
-- 交换两张牌的位置
local t = t_pai[i + 2]
t_pai[i + 2] = t_pai[k]
t_pai[k] = t
end
end
-- 如果能找到顺,判断剩下的牌是否能胡
if found_B == true and found_C == true then
-- 创建待判断列表
local new_list = {}
local k = 1
for j = i + 3,#pai,1 do
new_list[k] = t_pai[j]
k = k + 1
end
-- 对新建数组进行排序
table.sort(new_list)
return true,new_list
else
return false,{nil}
end
end
--测试胡n张牌
--IN:用户牌,检测起点,检测总数
--OUT:是否胡牌,将牌数
local function ValidHu(pai,i,n)
-- 空牌组直接胡
if n == 0 then
return true,0
end
-- 存在两个将牌或少于两个牌,不可能胡
if n%3==1 then
return false,0
end
-- 检测到末尾,胡
if i > n then
return true,0
end
-- 测试 AAA
if ValidAAA(pai,i,n) then
local t = false
local k = 0
t,k = ValidHu(pai,i+3,n,0)
if t == true then return true,k end
end
-- 测试AA
if ValidAA(pai,i,n) then
local t = false
local k = 0
t,k = ValidHu(pai,i+2,n)
if t == true and k == 0 then return true,1 end
end
-- 对万,条,饼测试ABC
if CheckSinglePaiType(pai[1]) ~= MJ_FENG and CheckSinglePaiType(pai[1]) ~= MJ_ZFB then
local new_pai = {}
local t = false
t,new_pai = ValidABC(pai,i,n)
if t == true then
local t2 = false
local k = 0
t2,k = ValidHu(new_pai,1,#new_pai)
if t2 == true then return t2,k end
end
end
return false,0
end
--检测吃牌
--IN:用户牌,上家的牌(只有上家能吃)
--OUT:所有可吃的牌的(!序号!)的table集合
--举例:手牌:11,12,12,14,上家牌:13
-- 输出:{{1,2},{2,4}}
--客户端会把序号重新转换成牌值,至于为什么这里转成序号,那是历史遗留问题。。。
function CheckChiPai(userPai,prePai)
--吃牌,用上家牌与自身牌遍历对比
local paiGroup = SortByType(userPai)
local attribute = {["Chi"]={}}
local paiType = CheckSinglePaiType(prePai)
if(#paiGroup["My"][paiType])
then
for i=1,#(paiGroup["My"][paiType])-1
do
--上家牌在顺子最左
if (paiGroup["My"][paiType][i] == prePai+1) and (paiGroup["My"][paiType][i+1] == prePai+2)
then
local shunzi = {paiGroup["My"][paiType][i],paiGroup["My"][paiType][i+1]}
table.insert(attribute["Chi"],shunzi)
end
--上家牌在顺子中间
if (paiGroup["My"][paiType][i] == prePai-1) and (paiGroup["My"][paiType][i+1] == prePai+1)
then
local shunzi = {paiGroup["My"][paiType][i],paiGroup["My"][paiType][i+1]}
table.insert(attribute["Chi"],shunzi)
--下面这块是用在出现AB**BC时吃B的情况
elseif (paiGroup["My"][paiType][i] ==prePai-1) and (paiGroup["My"][paiType][i+1] == prePai)
then
for k=i+1,#(paiGroup["My"][paiType])
do
if (paiGroup["My"][paiType][k] == prePai+1)
then
local shunzi = {paiGroup["My"][paiType][i],paiGroup["My"][paiType][k]}
table.insert(attribute["Chi"],shunzi)
end
end
end
--上家牌在顺子最右
if (paiGroup["My"][paiType][i] == prePai-2) and (paiGroup["My"][paiType][i+1] == prePai-1)
then
local shunzi = {paiGroup["My"][paiType][i],paiGroup["My"][paiType][i+1]}
table.insert(attribute["Chi"],shunzi)
end
end
end
--转换至userPai的绝对位置
table.sort( userPai, function (a,b) return a<b end )
for i=1,#attribute["Chi"]
do
for j=1,2
do
for k=1,#userPai
do
if attribute["Chi"][i][j] == userPai[k]
then
attribute["Chi"][i][j] = k
end
end
end
end
return attribute["Chi"]
end
--检测碰牌
--IN:用户牌,别人打的牌
--OUT:所有可碰的牌的(!序号!)的table集合
--输出格式与吃牌类似
function CheckPengPai(userPai,prePai)
--碰牌
local paiGroup = SortByType(userPai)
local attribute = {["Peng"]={}}
local paiType = CheckSinglePaiType(prePai)
if(#paiGroup["My"][paiType])
then
for i=1,#(paiGroup["My"][paiType])-1
do
if paiGroup["My"][paiType][i] == prePai and paiGroup["My"][paiType][i+1] == prePai
then
local kezi = {paiGroup["My"][paiType][i],paiGroup["My"][paiType][i+1]}
table.insert(attribute["Peng"],kezi)
end
end
end
-- 转换成对应userPai中的绝对位置
table.sort( userPai, function (a,b) return a<b end )
for i=1,#attribute["Peng"]
do
for k=1,#userPai
do
if attribute["Peng"][i][1] == userPai[k] and attribute["Peng"][i][2] == userPai[k+1]
then
attribute["Peng"][i][1] = k
attribute["Peng"][i][2] = k+1
end
end
end
return attribute["Peng"]
end
--检测杠牌
--IN:用户牌,别人打的牌,是否自摸的标志(为加杠准备)
--OUT:所有可碰的牌的(!序号!)的table集合
--输出格式与吃牌类似
--注意:此处有加杠检测,需要拿碰牌组进行测试
function CheckGangPai(userPai,prePai,isNotZiMo)
--杠牌
local paiGroup = SortByType(userPai)
local attribute = {["Gang"]={}}
local paiType = CheckSinglePaiType(prePai)
--明杠
if #paiGroup["My"][paiType] and isNotZiMo == 1 then
for i=1,#(paiGroup["My"][paiType])-2
do
if (paiGroup["My"][paiType][i] == prePai) and (paiGroup["My"][paiType][i+1] == prePai) and (paiGroup["My"][paiType][i+2] == prePai)
then
local gang = {paiGroup["My"][paiType][i],paiGroup["My"][paiType][i+1],paiGroup["My"][paiType][i+2]}
table.insert(attribute["Gang"],gang)
end
end
end
--暗杠(自摸)
if isNotZiMo == 0 then
local all_pai = CopyPai(userPai)
table.insert(all_pai,prePai)
table.sort( all_pai )
for i=1,#all_pai-2 do
if CheckSinglePaiGroup(all_pai[i]) == Pai_My then
if all_pai[i] == all_pai[i+1] and all_pai[i+1] == all_pai[i+2] and all_pai[i+2] == all_pai[i+3] then
local gang = {all_pai[i],all_pai[i+1],all_pai[i+2]}
table.insert(attribute["Gang"],gang)
end
end
end
end
--加杠
if #paiGroup["Peng"][paiType] and isNotZiMo == 0 then
prePai = GetPaiTypeNum(prePai)
for i=1,#(paiGroup["Peng"][paiType]),3
do
if GetPaiTypeNum(paiGroup["Peng"][paiType][i]) == prePai and GetPaiTypeNum(paiGroup["Peng"][paiType][i+1]) == prePai and GetPaiTypeNum(paiGroup["Peng"][paiType][i+2]) == prePai
then
local gang = {GetPaiTypeNum(paiGroup["Peng"][paiType][i]),GetPaiTypeNum(paiGroup["Peng"][paiType][i+1]),GetPaiTypeNum(paiGroup["Peng"][paiType][i+2])}
table.insert(attribute["Gang"],gang)
end
end
end
--转换成对应userPai中的绝对位置
table.sort( userPai, function (a,b) return a<b end )
for i=1,#attribute["Gang"]
do
for k=1,#userPai
do
if attribute["Gang"][i][1] == userPai[k] and attribute["Gang"][i][2] == userPai[k+1] and attribute["Gang"][i][3] == userPai[k+2]
then
attribute["Gang"][i][1] = k
attribute["Gang"][i][2] = k+1
attribute["Gang"][i][3] = k+2
end
end
end
return attribute["Gang"]
end
--广东麻将听牌,该函数功能未测试,实际项目中已去除听牌,但AI中会调用此方法作评测
--这一块可读性较差,如不启用听牌可以忽略
--IN:用户牌
--OUT:一个多重嵌套的table(不重复),每个基本单元是一个table,格式:{需要丢的牌,听的牌}
function CheckTingPai(userPai)
local ting_list = {}
local found_ting = false
-- 分组
local sort_pai = SortByType(userPai)
--
-- 检查听牌
local pai_info = {
[MJ_WAN] = {false,0},
[MJ_TIAO] = {false,0},
[MJ_BING] = {false,0},
[MJ_FENG] = {false,0},
[MJ_ZFB] = {false,0}}
-- 统计各分组胡牌及将牌情况
for i = 1,#sort_pai["My"] do
pai_info[i][1],pai_info[i][2] = ValidHu(sort_pai["My"][i],1,#sort_pai["My"][i])
end
-- 统计 胡,将总数
local sum_hu = 0
local sum_jiang = 0
for i = 1,5 do
if pai_info[i][1] == true then
sum_hu = sum_hu + 1
end
sum_jiang = sum_jiang + pai_info[i][2]
end
local collection = {
[MJ_WAN] = {11,12,13,14,15,16,17,18,19},
[MJ_TIAO] = {21,22,23,24,25,26,27,28,29},
[MJ_BING] = {31,32,33,34,35,36,37,38,39},
[MJ_FENG] = {41,43,45,47},
[MJ_ZFB] = {51,53,55}}
-- ********** 五组都“胡”,没法听 ****************************************************
--************************************ 检查是否听 九莲宝灯 **********************************************************
if sum_hu >= 3 and found_ting == false then
-- 统计万条饼数目
local num_count = {[MJ_WAN] = 0,[MJ_TIAO] = 0,[MJ_BING] = 0}
for i = 1,#userPai do
local paitype = CheckSinglePaiType(userPai[i])
if paitype <= 3 then
num_count[paitype] = num_count[paitype] + 1
end
end
-- 检查是哪种牌 >= 13
local pai_type = 0
for i = 1,#num_count do
if num_count[i] >= 13 then
pai_type = i
end
end
-- 相同牌数的牌 >= 13
if pai_type ~= 0 then
local count = {[1] = 0,[2] = 0,[3] = 0,[4] = 0,[5] = 0,[6] = 0,[7] = 0,[8] = 0,[9] = 0}
for i = 1,#userPai do
local m_type = CheckSinglePaiType(userPai[i])
local m_num = CheckSinglePaiNum(userPai[i])
if m_type == pai_type then
count[m_num] = count[m_num] + 1
end
end
-- 统计缺少牌的情况
local zero_count = 0
local zero_num = 0
local size2_count= 0 -- 除111 和 999外,数目 >= 2 的组的数目
local size2_num1 = 0 -- 该牌的索引
local size2_num2 = 0
for i = 1,#count do
if count[i] == 0 then
zero_count = zero_count + 1
zero_num = i
end
if i ~= 1 and i ~= 9 then
if count[i] >= 2 then
size2_count = size2_count + 1
if size2_num1 == 0 then
size2_num1 = i
else size2_num2 = i
end
end
end
end
local target = 0
local need = 0
if num_count[pai_type] == 13 then
for i = 1,#userPai do
if CheckSinglePaiType(userPai[i]) ~= pai_type then target = userPai[i] end
end
end
----------讨论13张同花色情况 -----
if zero_count <= 1 and size2_count <= 1 and num_count[pai_type] == 13 then
if zero_count == 1 and count[1] >= 3 and count[9] >= 3 then
found_ting = true
need = zero_num + 10 * pai_type
end
if zero_num == 0 and count[1] == 2 and count[9] >= 3 then
found_ting = true
need = 1 + 10 * pai_type
end
if zero_num == 0 and count[1] >= 3 and count[9] == 2 then
found_ting = true
need = 9 * 10 * pai_type
end
if found_ting == true then
local t_list = {target,need}
table.insert(ting_list,t_list)
end
if zero_num == 0 and count[1] >= 3 and count[9] >= 3 then
found_ting = true
if count[1] ~= 4 then
need = 1 + 10 * pai_type
local t_list = {target,need}
table.insert(ting_list,t_list)
end
if count[9] ~= 4 then
need = 9 + 10 * pai_type
local t_list = {target,need}
table.insert(ting_list,t_list)
end
end
end
-------- 下面讨论14张同花牌的情况
-- 统计 2 ~ 8 的牌数和
if num_count[pai_type] == 14 then
local mid_count = 0
for i = 2,8,1 do
mid_count = mid_count + count[i]
end
-- 0 (2 8 4) or (4 2 8)
if zero_count == 0 and mid_count == 8 and ( count[1] + count[9] == 6 ) and ( count[1] == 2 or count[1] == 4 ) then
found_ting = true
if count[1] == 2 then
target = 9 + 10 * pai_type
need = 1 + 10 * pai_type
else
target = 1 + 10 * pai_type
need = 9 + 10 * pai_type
end
local t_list = {target,need}
table.insert(ting_list,t_list)
end
-- 0 (2 9 3 ) or (3 9 2)
if zero_count == 0 and mid_count == 9 and ( count[1] + count[9] == 5 ) and ( count[1] == 2 or count[1] == 3 ) then
found_ting = true
if count[1] == 2 then
need = 1 + 10 * pai_type
else
need = 9 + 10 * pai_type
end
if size2_count == 1 then
target = size2_num1 + 10 * pai_type
local t_list = {target,need}
table.insert(ting_list,t_list)
elseif size2_count == 2 then
local target1 = size2_num1 + 10 * pai_type
local target2 = size2_num2 + 10 * pai_type
local t_list1 = {target1,need}
local t_list2 = {target2,need}
table.insert(ting_list,t_list1)
table.insert(ting_list,t_list2)
end
end
-- 1 (3 7 4) or (4 7 3)
if zero_count == 1 and mid_count == 7 and ( count[1] + count[9] == 7 ) and ( count[1] == 3 or count[1] == 4 ) then
found_ting = true
if count[1] == 4 then
target1 = 1 + 10 * pai_type
else target1 = 9 + 10 * pai_type end
need = zero_num + 10 * pai_type
local target2 = size2_num1 + 10 * pai_type
local t_list1 = {target1,need}
local t_list2 = {target2,need}
table.insert(ting_list,t_list1)
table.insert(ting_list,t_list2)
end
-- 1 (3 8 3)
if zero_count == 1 and mid_count == 8 and ( count[1] == 3 and count[9] == 3 ) then
found_ting = true
need = zero_num + 10 * pai_type
if size2_count == 1 then
target = size2_num1 + 10 * pai_type
local t_list = {target,need}
table.insert(ting_list,t_list)
elseif size2_count == 2 then
local target1 = size2_num1 + 10 * pai_type
local target2 = size2_num2 + 10 * pai_type
local t_list1 = {target1,need}
local t_list2 = {target2,need}
table.insert(ting_list,t_list1)
table.insert(ting_list,t_list2)
end
end
end
end
-- if sum_hu == 5 then return false,0,0 end
-- ********** 四组“胡”且将 == 0 或 1 ************************************************
if sum_hu == 4 and sum_jiang < 2 and found_ting == false then
-- 剩下的一组所需要的将 为 1 或 0
local jiang_need = 1 - sum_jiang
-- 找出是哪一组没法“胡”
local target = 0
for i = 1,#pai_info do
if pai_info[i][1] == false then target = i end
end
-- 从该组第一张牌开始替换
for i = 1,#sort_pai["My"][target] do
local t_pai = sort_pai["My"][target][i]
for j = 1,#collection[target] do
-- local save_pai = sort_pai["My"][target][i]
sort_pai["My"][target][i] = collection[target][j]
local t = false
local k = 0
t,k = ValidHu(sort_pai["My"][target],1,#sort_pai["My"][target])
-- 胡 且 达到所需将牌数目
-- if t == true and k == jiang_need then return true,t_pai,collection[target][j] end
if t == true and k == jiang_need then
found_ting = true
-- 注意本轮被替换的数实际是
local t_list = {t_pai,collection[target][j]}
table.insert(ting_list,t_list)
end
end
sort_pai["My"][target][i] = t_pai
end
end
-- ********** 三组“胡” 且将 == 0 或 1 ********************************************************
if sum_hu == 3 and sum_jiang < 2 and found_ting == false then
-- 找出是哪两组没法“胡”
local target1 = 0
local target2 = 0
for i = 1,#pai_info do
if pai_info[i][1] == false then
if target1 == 0 then target1 = i
else target2 = i
end
end
end
-- 剩下的两组所需要的将 为 1 或 0
local jiang_need = 1 - sum_jiang
-- 删掉第一组中的牌,往第二组加一张牌
for i = 1,#sort_pai["My"][target1] do
-- 记录下第一组牌中被删除的牌
local t_pai = sort_pai["My"][target1][i]
table.remove(sort_pai["My"][target1],i)
local t1 = false
local k1 = 0
t1,k1 = ValidHu(sort_pai["My"][target1],1,#sort_pai["My"][target1])
-- 删掉第一组牌能胡,给第二组牌添加一张牌,测试 胡 和将
if t1 == true then
for k = 1,#collection[target2] do
local t_pai2 = CopyPai(sort_pai["My"][target2])
table.insert(t_pai2,collection[target2][k])
table.sort(t_pai2)
local t2 = false
local k2 = 0
t2,k2 = ValidHu(t_pai2,1,#t_pai2)
-- if t2 == true and k2 + k1 == jiang_need then return true,t_pai,collection[target2][k] end
if t2 == true and k2 + k1 == jiang_need then
found_ting = true
local t_list = {t_pai,collection[target2][k]}
table.insert(ting_list,t_list)
end
end
end
-- 还原第一组牌
table.insert(sort_pai["My"][target1],i,t_pai)
end
-- 交换下位置
-- 删掉第二组中的牌,往第一组加一张牌
for i = 1,#sort_pai["My"][target2] do
-- 记录下第二组牌中被删除的牌
local t_pai = sort_pai["My"][target2][i]
table.remove(sort_pai["My"][target2],i)
local t1 = false
local k1 = 0
t1,k1 = ValidHu(sort_pai["My"][target2],1,#sort_pai["My"][target2])
-- 第二组牌能胡,往第一组牌中加入牌,测试 胡、将
if t1 == true then
-- 往第一组牌加入一张牌
for k = 1,#collection[target1] do
local t_pai2 = CopyPai(sort_pai["My"][target1])
table.insert(t_pai2,collection[target1][k])
table.sort(t_pai2)
local t2 = false
local k2 = 0
t2,k2 = ValidHu(t_pai2,1,#t_pai2)
-- if t2 == true and k2 + k1 == jiang_need then return true,t_pai,collection[target1][k] end
if t2 == true and k2 + k1 == jiang_need then
found_ting = true
local t_list = {t_pai,collection[target1][k]}
table.insert(ting_list,t_list)
end
end
end
-- 还原第二组牌
table.insert(sort_pai["My"][target2],i,t_pai)
end
end
-- ************************************ 检查是否听 十三幺 **********************************************************
if sum_hu < 3 and found_ting == false then
local ssy_list = {11,19,21,29,31,39,41,43,45,47,51,53,55}
local ssy_count = {
[11] = 0,[19] = 0,[21] = 0,[29] = 0,[31] = 0,[39] = 0,[41] = 0,[43] = 0,[45] = 0,[47] = 0,[51] = 0,
[53] = 0,[55] = 0}
-- 扫描用户牌,统计出现十三幺各牌的个数
for i = 1,#userPai do
for j = 1,#ssy_list do
if userPai[i] == ssy_list[j] then
ssy_count[ssy_list[j]] = ssy_count[ssy_list[j]] + 1
end
end
end
local sum = 0
local zero = 0
for i = 1,#ssy_list do
if ssy_count[ssy_list[i]] == 0 then zero = zero + 1 end
sum = sum + ssy_count[ssy_list[i]]
end
-- 能听的情况只有
-- 1. zero = 0 && sum = 13 : 找出不在ssy_list 中的牌,返回 -- 11,19,21,29,31,39,41,43,45,47,51,53,55,()
-- 2. zero = 1 && sum = 13 : 同 1 11,(18),21,29,31,39,41,43,45,47,51,53,55,55
-- 3. zero = 1 && sum = 14 : 找出牌数大于1的组,可能为两组或一组(3张相同)11,(11),21,29,31,39,41,43,45,47,51,53,55,55
---- 讨论 1,2 合为一种情况 -------------
if ( zero == 0 or zero == 1 ) and sum == 13 then
local target = 0
for i = 1,#userPai do
local found = false
for j = 1,#ssy_list do
if userPai[i] == ssy_list[j] then found = true end
end
if found == false then target = i end
end
-- 这里就只插入一张
found_ting = true
local t_list = {userPai[target],ssy_list[1]}
table.insert(ting_list,t_list)
end
---- 讨论3 ----------------------
if zero == 1 and sum == 14 then
-- 找出欠缺的牌
local pai_need = 0
for i = 1,#ssy_list do
if ssy_count[ssy_list[i]] == 0 then pai_need = ssy_list[i] end
end
-- 找出牌数大于1的组,添加
for i = 1,#ssy_list do
if ssy_count[ssy_list[i]] > 1 then
found_ting = true
local t_list = {ssy_list[i],pai_need}
table.insert(ting_list,t_list)
end
end
end
end
end
-- 过滤掉相同的组
local unique_list = {}
for i = 1,#ting_list do
local found_same = false
for j = 1,#unique_list do
if ting_list[i][1] == unique_list[j][1] and ting_list[i][2] == unique_list[j][2] then found_same = true end
end
if found_same ~= true then table.insert(unique_list,ting_list[i]) end
end
return unique_list
end
--递归检测刻子
--IN:用户手牌,检测起点,待检测牌总数
--OUT:是否全是刻子
local function CheckKe(userPai,i,n)
-- 小于三张不可能刻
if i > n then return true end
if n - i < 2 then return false end
if CheckAAAPai(userPai[i],userPai[i+1],userPai[i+2]) and CheckKe(userPai,i+3,n) then
return true
else
return false end
end
--递归检测顺子
--IN:用户手牌,检测起点,待检测牌总数
--OUT:是否全是顺子
local function CheckShun(userPai,i,n)
-- 小于三张不可能是顺子
if i > n then return true end
if n - i < 2 then return false end
if CheckABCPai(userPai[i],userPai[i+1],userPai[i+2]) and CheckShun(userPai,i+3,n) then
return true
else
return false end
end
-- 删除传进来的牌中的将牌
-- 仅用于删除 平胡或碰碰胡中的将牌 如 11 11 11 12 12 12 13 13 13 14 14 14 |(43 43)
-- 可能会删除 (11 11) 12 12 13 13 慎用
local function Deletejiang(userPai)
local count = 0
local last_pai = 0
for i = 1,#userPai do
local cur_pai = userPai[i]
if cur_pai ~= last_pai then
last_pai = cur_pai
if count == 1 then
table.remove(userPai,i-1)
table.remove(userPai,i-2)
end
count = 0
else
count = count + 1
last_pai = cur_pai
end
end
-- 检测末尾部分
if count == 1 then
table.remove(userPai)
table.remove(userPai)
end
end
-- 检测平胡
local function CheckPH(userPai)
-- 拷贝数组
local t_pai = CopyPai(userPai)
-- 删除将牌
Deletejiang(t_pai)
-- 保证只有一组将牌被删除,多组则返回
if #t_pai ~= ( #userPai - 2 ) then return false end
-- --检查剩下的牌是否只由顺子组成
local sort_pai = SortByType(t_pai)
for i = 1,#sort_pai["My"] do
if #sort_pai["My"][i] ~= 0 then
if CheckShun(sort_pai["My"][i],1,#sort_pai["My"][i]) == false then return false end
end
end
return true
end
-- 检测碰碰胡
local function CheckPPH(userPai)
-- 无顺子即可
-- 拷贝数组
local t_pai = CopyPai(userPai)
-- 删除将牌
Deletejiang(t_pai)
-- 保证只有一组将牌被删除,多组则返回
if #t_pai ~= ( #userPai - 2 ) then return false end
local sort_pai = SortByType(t_pai)
--检查剩下的牌是否只由刻组成
for i = 1,#sort_pai["My"] do
if #sort_pai["My"][i] ~= 0 then
if CheckKe(sort_pai["My"][i],1,#sort_pai["My"][i]) == false then return false end
end
end
return true
end
-- 检测混一色
local function CheckHYS(userPai)
local sort_pai = SortByType(userPai)
local count_wan = #sort_pai["My"][MJ_WAN]
local count_bing = #sort_pai["My"][MJ_BING]
local count_tiao = #sort_pai["My"][MJ_TIAO]
local count_feng = #sort_pai["My"][MJ_FENG]
local count_zfb = #sort_pai["My"][MJ_ZFB]
-- 保证有字牌