forked from dannisliang/QPAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths13s.cpp
6725 lines (6569 loc) · 217 KB
/
s13s.cpp
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
//
// Created by [email protected]
// 5/26/2019
//
#include <time.h>
#include <algorithm>
#include "math.h"
#include <iostream>
#include <memory>
#include <utility>
#include <sys/types.h>
#include <sstream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <map>
#include "cfg.h"
#include "funcC.h"
#include "s13s.h"
#include "weights.h"
//protobuf测试
#include "s13s.Message.pb.h"
#include "pb2Json.h"
namespace S13S {
//一副扑克(52张)
uint8_t s_CardListData[MaxCardTotal] =
{
0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D, // 方块 A - K
0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,0x2D, // 梅花 A - K
0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,0x3D, // 红心 A - K
0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,0x4D, // 黑桃 A - K
};
//构造函数
CGameLogic::CGameLogic()
{
index_ = 0;
memset(cardsData_, 0, sizeof(uint8_t)*MaxCardTotal);
}
//析构函数
CGameLogic::~CGameLogic()
{
}
//初始化扑克牌数据
void CGameLogic::InitCards()
{
//printf("--- *** 初始化一副扑克...\n");
memcpy(cardsData_, s_CardListData, sizeof(uint8_t)*MaxCardTotal);
}
//debug打印
void CGameLogic::DebugListCards() {
//手牌按花色升序(方块到黑桃),同花色按牌值从小到大排序
SortCardsColor(cardsData_, MaxCardTotal, true, true, true);
for (int i = 0; i < MaxCardTotal; ++i) {
printf("%02X %s\n", cardsData_[i], StringCard(cardsData_[i]).c_str());
}
}
//剩余牌数
int8_t CGameLogic::Remaining() {
return int8_t(MaxCardTotal - index_);
}
//洗牌
void CGameLogic::ShuffleCards()
{
//printf("-- *** 洗牌...\n");
static uint32_t seed = (uint32_t)time(NULL);
//int c = rand() % 20 + 5;
int c = rand_r(&seed) % 20 + 5;
for (int k = 0; k < c; ++k) {
for (int i = 0; i < MaxCardTotal; ++i) {
//int j = rand() % MaxCardTotal;
int j = rand_r(&seed) % MaxCardTotal;
if (i != j) {
std::swap(cardsData_[i], cardsData_[j]);
}
}
}
index_ = 0;
}
//发牌,生成n张玩家手牌
void CGameLogic::DealCards(int8_t n, uint8_t *cards)
{
//printf("-- *** %d张余牌,发牌 ...\n", Remaining());
if (cards == NULL) {
return;
}
if (n > Remaining()) {
return;
}
int k = 0;
for (int i = index_; i < index_ + n; ++i) {
assert(i < MaxCardTotal);
cards[k++] = cardsData_[i];
}
index_ += n;
}
//花色:黑>红>梅>方
uint8_t CGameLogic::GetCardColor(uint8_t card) {
return (card & 0xF0);
}
//牌值:A<2<3<4<5<6<7<8<9<10<J<Q<K
uint8_t CGameLogic::GetCardValue(uint8_t card) {
return (card & 0x0F);
}
//点数:2<3<4<5<6<7<8<9<10<J<Q<K<A
uint8_t CGameLogic::GetCardPoint(uint8_t card) {
uint8_t value = GetCardValue(card);
return value == 0x01 ? 0x0E : value;
}
//花色和牌值构造单牌
uint8_t CGameLogic::MakeCardWith(uint8_t color, uint8_t value) {
return (GetCardColor(color) | GetCardValue(value));
}
//牌值大小:A<2<3<4<5<6<7<8<9<10<J<Q<K
static bool byCardValueColorGG(uint8_t card1, uint8_t card2) {
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
if (v0 != v1) {
//牌值不同比大小
return v0 > v1;
}
//牌值相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 > c1;
}
static bool byCardValueColorGL(uint8_t card1, uint8_t card2) {
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
if (v0 != v1) {
//牌值不同比大小
return v0 > v1;
}
//牌值相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 < c1;
}
static bool byCardValueColorLG(uint8_t card1, uint8_t card2) {
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
if (v0 != v1) {
//牌值不同比大小
return v0 < v1;
}
//牌值相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 > c1;
}
static bool byCardValueColorLL(uint8_t card1, uint8_t card2) {
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
if (v0 != v1) {
//牌值不同比大小
return v0 < v1;
}
//牌值相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 < c1;
}
//牌点大小:2<3<4<5<6<7<8<9<10<J<Q<K<A
static bool byCardPointColorGG(uint8_t card1, uint8_t card2) {
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
if (p0 != p1) {
//牌点不同比大小
return p0 > p1;
}
//牌点相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 > c1;
}
static bool byCardPointColorGL(uint8_t card1, uint8_t card2) {
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
if (p0 != p1) {
//牌点不同比大小
return p0 > p1;
}
//牌点相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 < c1;
}
static bool byCardPointColorLG(uint8_t card1, uint8_t card2) {
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
if (p0 != p1) {
//牌点不同比大小
return p0 < p1;
}
//牌点相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 > c1;
}
static bool byCardPointColorLL(uint8_t card1, uint8_t card2) {
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
if (p0 != p1) {
//牌点不同比大小
return p0 < p1;
}
//牌点相同比花色
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
return c0 < c1;
}
//手牌排序(默认按牌点降序排列),先比牌值/点数,再比花色
//byValue bool false->按牌点 true->按牌值
//ascend bool false->降序排列(即从大到小排列) true->升序排列(即从小到大排列)
//clrAscend bool false->花色降序(即黑桃到方块) true->花色升序(即从方块到黑桃)
void CGameLogic::SortCards(uint8_t *cards, int n, bool byValue, bool ascend, bool clrAscend)
{
if (byValue) {
if (ascend) {
if (clrAscend) {
//LL
std::sort(cards, cards + n, byCardValueColorLL);
}
else {
//LG
std::sort(cards, cards + n, byCardValueColorLG);
}
}
else {
if (clrAscend) {
//GL
std::sort(cards, cards + n, byCardValueColorGL);
}
else {
//GG
std::sort(cards, cards + n, byCardValueColorGG);
}
}
}
else {
if (ascend) {
if (clrAscend) {
//LL
std::sort(cards, cards + n, byCardPointColorLL);
}
else {
//LG
std::sort(cards, cards + n, byCardPointColorLG);
}
}
else {
if (clrAscend) {
//GL
std::sort(cards, cards + n, byCardPointColorGL);
}
else {
//GG
std::sort(cards, cards + n, byCardPointColorGG);
}
}
}
}
//牌值大小:A<2<3<4<5<6<7<8<9<10<J<Q<K
static bool byCardColorValueGG(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 > c1;
}
//花色相同比牌值
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
return v0 > v1;
}
static bool byCardColorValueGL(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 > c1;
}
//花色相同比牌值
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
return v0 < v1;
}
static bool byCardColorValueLG(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 < c1;
}
//花色相同比牌值
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
return v0 > v1;
}
static bool byCardColorValueLL(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 < c1;
}
//花色相同比牌值
uint8_t v0 = CGameLogic::GetCardValue(card1);
uint8_t v1 = CGameLogic::GetCardValue(card2);
return v0 < v1;
}
//牌点大小:2<3<4<5<6<7<8<9<10<J<Q<K<A
static bool byCardColorPointGG(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 > c1;
}
//花色相同比牌点
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
return p0 > p1;
}
static bool byCardColorPointGL(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 > c1;
}
//花色相同比牌点
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
return p0 < p1;
}
static bool byCardColorPointLG(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 < c1;
}
//花色相同比牌点
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
return p0 > p1;
}
static bool byCardColorPointLL(uint8_t card1, uint8_t card2) {
uint8_t c0 = CGameLogic::GetCardColor(card1);
uint8_t c1 = CGameLogic::GetCardColor(card2);
if (c0 != c1) {
//花色不同比花色
return c0 < c1;
}
//花色相同比牌点
uint8_t p0 = CGameLogic::GetCardPoint(card1);
uint8_t p1 = CGameLogic::GetCardPoint(card2);
return p0 < p1;
}
//手牌排序(默认按牌点降序排列),先比花色,再比牌值/点数
//clrAscend bool false->花色降序(即黑桃到方块) true->花色升序(即从方块到黑桃)
//byValue bool false->按牌点 true->按牌值
//ascend bool false->降序排列(即从大到小排列) true->升序排列(即从小到大排列)
void CGameLogic::SortCardsColor(uint8_t *cards, int n, bool clrAscend, bool byValue, bool ascend)
{
if (byValue) {
if (ascend) {
if (clrAscend) {
//LL
std::sort(cards, cards + n, byCardColorValueLL);
}
else {
//GL
std::sort(cards, cards + n, byCardColorValueGL);
}
}
else {
if (clrAscend) {
//LG
std::sort(cards, cards + n, byCardColorValueLG);
}
else {
//GG
std::sort(cards, cards + n, byCardColorValueGG);
}
}
}
else {
if (ascend) {
if (clrAscend) {
//LL
std::sort(cards, cards + n, byCardColorPointLL);
}
else {
//GL
std::sort(cards, cards + n, byCardColorPointGL);
}
}
else {
if (clrAscend) {
//LG
std::sort(cards, cards + n, byCardColorPointLG);
}
else {
//GG
std::sort(cards, cards + n, byCardColorPointGG);
}
}
}
}
//牌值字符串
std::string CGameLogic::StringCardValue(uint8_t value)
{
if (0 == value) {
return "?";
}
switch (value)
{
case A: return "A";
case J: return "J";
case Q: return "Q";
case K: return "K";
}
char ch[3] = { 0 };
sprintf(ch, "%d", value);
return ch;
}
//花色字符串
std::string CGameLogic::StringCardColor(uint8_t color)
{
switch (color)
{
case Spade: return "♠";
case Heart: return "♥";
case Club: return "♣";
case Diamond: return "♦";
}
return "?";
}
//单牌字符串
std::string CGameLogic::StringCard(uint8_t card) {
std::string s(StringCardColor(GetCardColor(card)));
s += StringCardValue(GetCardValue(card));
return s;
}
//牌型字符串
std::string CGameLogic::StringHandTy(HandTy ty) {
switch (ty)
{
case Tysp: return "Tysp";
case Ty20: return "Ty20";
case Ty22: return "Ty22";
case Ty30: return "Ty30";
case Ty123: return "Ty123";
case Tysc: return "Tysc";
case Ty32: return "Ty32";
case Ty40: return "Ty40";
case Ty123sc: return "Ty123sc";
////// 特殊牌型
case TyThreesc: return "TyThreesc";
case TyThree123: return "TyThree123";
case TySix20: return "TySix20";
case TyFive2030: return "TyFive2030";
case TyFour30: return "TyFour30";
case TyTwo3220: return "TyTwo3220";
case TyAllOneColor: return "TyAllOneColor";
case TyAllSmall: return "TyAllSmall";
case TyAllBig: return "TyAllBig";
case TyThree40: return "TyThree40";
case TyThree123sc: return "TyThree123sc";
case Ty12Royal: return "Ty12Royal";
case TyOneDragon: return "TyOneDragon";
case TyZZQDragon: return "TyZZQDragon";
}
assert(false);
return "";
}
//手牌字符串
std::string CGameLogic::StringCards(uint8_t const* cards, int n) {
std::string strcards;
for (int i = 0; i < n; ++i) {
if (i == 0) {
strcards += StringCard(cards[i]);
}
else {
strcards += " " + StringCard(cards[i]);
}
}
return strcards;
}
//打印n张牌
void CGameLogic::PrintCardList(uint8_t const* cards, int n, bool hide) {
for (int i = 0; i < n; ++i) {
if (cards[i] == 0 && hide) {
continue;
}
printf("%s ", StringCard(cards[i]).c_str());
}
printf("\n");
}
//获取牌有效列数
//cards uint8_t const* 相同牌值n张牌(n<=4)
//n int 黑/红/梅/方4张牌
uint8_t CGameLogic::get_card_c(uint8_t const* cards, int n) {
assert(n <= 4);
for (int i = 0; i < n; ++i) {
if (cards[i] == 0) {
return i;
}
}
return n;
}
//返回指定花色牌列号
//cards uint8_t const* 相同牌值n张牌(n<=4)
//n int 黑/红/梅/方4张牌
//clr CardColor 指定花色
uint8_t CGameLogic::get_card_colorcol(uint8_t const* cards, int n, CardColor clr) {
assert(n <= 4);
//int c = get_card_c(cards, n);
for (int i = 0; i < n/*c*/; ++i) {
if (clr == GetCardColor(cards[i])) {
return i;
}
}
return 0xFF;
}
//拆分字符串"♦A ♦3 ♥3 ♥4 ♦5 ♣5 ♥5 ♥6 ♣7 ♥7 ♣9 ♣10 ♣J"
void CGameLogic::CardsBy(std::string const& strcards, std::vector<std::string>& vec) {
std::string str(strcards);
while (true) {
std::string::size_type s = str.find_first_of(' ');
if (-1 == s) {
break;
}
vec.push_back(str.substr(0, s));
str = str.substr(s + 1);
}
if (!str.empty()) {
vec.push_back(str.substr(0, -1));
}
}
//字串构造牌"♦A"->0x11
uint8_t CGameLogic::MakeCardBy(std::string const& name) {
uint8_t color = 0, value = 0;
if (0 == strncmp(name.c_str(), "♠", 3)) {
color = Spade;
std::string str(name.substr(3, -1));
switch (str.front())
{
case 'J': value = J; break;
case 'Q': value = Q; break;
case 'K': value = K; break;
case 'A': value = A; break;
case 'T': value = T; break;
default: {
value = atoi(str.c_str());
break;
}
}
}
else if (0 == strncmp(name.c_str(), "♥", 3)) {
color = Heart;
std::string str(name.substr(3, -1));
switch (str.front())
{
case 'J': value = J; break;
case 'Q': value = Q; break;
case 'K': value = K; break;
case 'A': value = A; break;
case 'T': value = T; break;
default: {
value = atoi(str.c_str());
break;
}
}
}
else if (0 == strncmp(name.c_str(), "♣", 3)) {
color = Club;
std::string str(name.substr(3, -1));
switch (str.front())
{
case 'J': value = J; break;
case 'Q': value = Q; break;
case 'K': value = K; break;
case 'A': value = A; break;
case 'T': value = T; break;
default: {
value = atoi(str.c_str());
break;
}
}
}
else if (0 == strncmp(name.c_str(), "♦", 3)) {
color = Diamond;
std::string str(name.substr(3, -1));
switch (str.front())
{
case 'J': value = J; break;
case 'Q': value = Q; break;
case 'K': value = K; break;
case 'A': value = A; break;
case 'T': value = T; break;
default: {
value = atoi(str.c_str());
break;
}
}
}
assert(value != 0);
return value ? MakeCardWith(color, value) : 0;
}
//生成n张牌<-"♦A ♦3 ♥3 ♥4 ♦5 ♣5 ♥5 ♥6 ♣7 ♥7 ♣9 ♣10 ♣J"
void CGameLogic::MakeCardList(std::vector<std::string> const& vec, uint8_t *cards, int size) {
int c = 0;
for (std::vector<std::string>::const_iterator it = vec.begin();
it != vec.end(); ++it) {
cards[c++] = MakeCardBy(it->c_str());
}
}
//生成n张牌<-"♦A ♦3 ♥3 ♥4 ♦5 ♣5 ♥5 ♥6 ♣7 ♥7 ♣9 ♣10 ♣J"
int CGameLogic::MakeCardList(std::string const& strcards, uint8_t *cards, int size) {
std::vector<std::string> vec;
CardsBy(strcards, vec);
MakeCardList(vec, cards, size);
return (int)vec.size();
}
//返回去重后的余牌/散牌
//src uint8_t const* 牌源
//pdst uint8_t(**const)[4] 衔接dst4/dst3/dst2
//dst4 uint8_t(*)[4] 存放所有四张牌型,c4 四张牌型数
//dst3 uint8_t(*)[4] 存放所有三张牌型,c3 三张牌型数
//dst2 uint8_t(*)[4] 存放所有对子牌型,c2 对子牌型数
//cpy uint8_t* 去重后的余牌/散牌(除去四张/三张/对子)///////
int CGameLogic::RemoveRepeatCards(uint8_t const* src, int len,
uint8_t(**const pdst)[4],
uint8_t(*dst4)[4], int& c4, uint8_t(*dst3)[4], int& c3,
uint8_t(*dst2)[4], int& c2, uint8_t *cpy, int& cpylen) {
uint8_t const* psrc = src;
int lensrc = len;
uint8_t cpysrc[MaxSZ] = { 0 };
//枚举所有重复四张牌型
int const size4 = 3;
//uint8_t dst4[size4][4] = { 0 };
c4 = EnumRepeatCards(psrc, lensrc, 4, dst4, size4, cpy, cpylen);
if (c4 > 0) {
memcpy(cpysrc, cpy, cpylen);//cpysrc
lensrc = cpylen;//lensrc
psrc = cpysrc;//psrc
}
//枚举所有重复三张牌型
int const size3 = 4;
//uint8_t dst3[size3][4] = { 0 };
c3 = EnumRepeatCards(psrc, lensrc, 3, dst3, size3, cpy, cpylen);
if (c3 > 0) {
memcpy(cpysrc, cpy, cpylen);//cpysrc
lensrc = cpylen;//lensrc
psrc = cpysrc;//psrc
}
//枚举所有重复二张牌型
int const size2 = 6;
//uint8_t dst2[size2][4] = { 0 };
c2 = EnumRepeatCards(psrc, lensrc, 2, dst2, size2, cpy, cpylen);
if (c2 == 0) {
memcpy(cpy, psrc, lensrc);//cpy
cpylen = lensrc;//cpylen
}
//用psrc衔接dst4/dst3/dst2 //////
//uint8_t(*pdst[6])[4] = { 0 };
//typedef uint8_t(*Ptr)[4];
//Ptr pdst[6] = { 0 };
int c = 0;
//所有重复四张牌型
for (int i = 0; i < c4; ++i) {
pdst[c++] = &dst4[i];
}
//所有重复三张牌型
for (int i = 0; i < c3; ++i) {
pdst[c++] = &dst3[i];
}
//所有重复二张牌型
for (int i = 0; i < c2; ++i) {
pdst[c++] = &dst2[i];
}
return c;
}
#if 0
//求组合C(n,1)*C(n,1)...*C(n,1)
//f(k)=C(n,1)
//Multi(k)=f(1)*f(2)...*f(k)
//n int 访问广度 由c4,c3,c2计算得到(4/3/2/1)
//k int 访问深度
//clr bool true->区分花色的所有组合
//clr bool false->不区分花色的所有组合
//深度优先遍历,由浅到深,广度遍历,由里向外
int CGameLogic::DepthVisit(int c4, int c3, int c2,
int k,
uint8_t(*const*const psrc)[4],
std::vector<std::vector<short>>& ctx,
std::vector<std::vector<uint8_t>>& dst,
std::vector<int> const& vec, bool clr) {
int c = 0;
static int const KDEPTH = MaxSZ;
int e[KDEPTH + 1] = { 0 };
e[0] = k;
//dst.clear();
//ctx.clear();
return DepthC(c4, c3, c2, k, e, c, psrc, ctx, dst, vec, clr);
}
//递归求组合C(n,1)*C(n,1)...*C(n,1)
//f(k)=C(n,1)
//Multi(k)=f(1)*f(2)...*f(k)
//n int 访问广度 由c4,c3,c2计算得到(4/3/2/1)
//k int 访问深度
//clr bool true->区分花色的所有组合
//clr bool false->不区分花色的所有组合
//深度优先遍历,由浅到深,广度遍历,由里向外
int CGameLogic::DepthC(int c4, int c3, int c2,
int k, int *e, int& c,
uint8_t(*const*const psrc)[4],
std::vector<std::vector<short>>& ctx,
std::vector<std::vector<uint8_t>>& dst,
std::vector<int> const& vec, bool clr) {
//c4,c3,c2计算n(4/3/2/1)
int n = clr ? 4 : 1;
if (vec[k - 1] < c4) {
n = clr ? 4 : 1;
}
else if (vec[k - 1] < (c4 + c3)) {
n = clr ? 4/*3*/ : 1;
}
else {
n = clr ? 4/*2*/ : 1;
}
//不区分花色取一张就够了
for (int i = n; i > 0; --i) {
if ((*psrc[vec[k - 1]])[i - 1] == 0) {
continue;
}
assert(k > 0);
//psrc第vec[j - 1]组第e[j] - 1张(0/1/2/3)
//psrc第vec[j - 1]组第e[j] - 1张(0/1/2)
//psrc第vec[j - 1]组第e[j] - 1张(0/1)
e[k] = i;
if (k > 1) {
DepthC(c4, c3, c2, k - 1, e, c, psrc, ctx, dst, vec, clr);
}
else {
++c;
std::vector<uint8_t> v;
std::vector<short> w;
//深度1...e[0]
for (int j = e[0]; j > 0; --j) {
//printf("%d", e[j]);
//printf("%s", StringCard((*psrc[vec[j - 1]])[e[j] - 1]).c_str());
v.push_back((*psrc[vec[j - 1]])[e[j] - 1]);
{
//psrc第r组/牌数c ///
uint8_t r = vec[j - 1];
uint8_t c = 0;
while (c < 4 && (*psrc[r])[c] > 0) {
++c;
}
//当前组合位于psrc的ctx信息
short ctx = (short)(((0xFF & r) << 8) | (0xFF & c));
w.push_back(ctx);
}
}
//printf(",");
dst.push_back(v);
//v.clear();
ctx.push_back(w);
//w.clear();
}
}
return c;
}
#endif
static bool byValueG(const uint8_t(*const a)[4], const uint8_t(*const b)[4]) {
uint8_t v0 = CGameLogic::GetCardValue(a[0][0]);
uint8_t v1 = CGameLogic::GetCardValue(b[0][0]);
return v0 > v1;
}
static bool byValueL(const uint8_t(*const a)[4], const uint8_t(*const b)[4]) {
uint8_t v0 = CGameLogic::GetCardValue(a[0][0]);
uint8_t v1 = CGameLogic::GetCardValue(b[0][0]);
return v0 < v1;
}
static bool byPointG(const uint8_t(*const a)[4], const uint8_t(*const b)[4]) {
uint8_t p0 = CGameLogic::GetCardPoint(a[0][0]);
uint8_t p1 = CGameLogic::GetCardPoint(b[0][0]);
return p0 > p1;
}
static bool byPointL(const uint8_t(*const a)[4], const uint8_t(*const b)[4]) {
uint8_t p0 = CGameLogic::GetCardPoint(a[0][0]);
uint8_t p1 = CGameLogic::GetCardPoint(b[0][0]);
return p0 < p1;
}
static void SortCards_src(uint8_t(**const psrc)[4], int n, bool byValue, bool ascend) {
if (byValue) {
if (ascend) {
std::sort(psrc, psrc + n, byValueL);
}
else {
std::sort(psrc, psrc + n, byValueG);
}
}
else {
if (ascend) {
std::sort(psrc, psrc + n, byPointL);
}
else {
std::sort(psrc, psrc + n, byPointG);
}
}
}
#if 0
//枚举单张组合牌(分别从四张/三张/对子中任抽一张组合牌)
//src4 uint8_t(*const)[4] 所有四张牌型牌源,c4 四张牌型数
//src3 uint8_t(*const)[4] 所有三张牌型牌源,c3 三张牌型数
//src2 uint8_t(*const)[4] 所有对子牌型牌源,c2 对子牌型数
//dst std::vector<std::vector<uint8_t>>& 存放单张组合牌//////
//clr bool true->区分花色的所有组合 false->不区分花色的所有组合
int CGameLogic::EnumCombineCards(
uint8_t(**const psrc)[4],
uint8_t(*const src4)[4], int const c4,
uint8_t(*const src3)[4], int const c3,
uint8_t(*const src2)[4], int const c2,
std::vector<std::vector<short>>& ctx,
std::vector<std::vector<uint8_t>>& dst, bool clr) {
//uint8_t(*src[6])[4] = { 0 };
//typedef uint8_t(*Ptr)[4];
//Ptr psrc[6] = { 0 };
int c = 0;
dst.clear();
ctx.clear();
//所有重复四张牌型
for (int i = 0; i < c4; ++i) {
psrc[c++] = &src4[i];
}
//所有重复三张牌型
for (int i = 0; i < c3; ++i) {
psrc[c++] = &src3[i];
}
//所有重复二张牌型
for (int i = 0; i < c2; ++i) {
psrc[c++] = &src2[i];
}
int n = c;
CFuncC fnC;
std::vector<std::vector<int>> vec;
//psrc组与组之间按牌值升序排列(从小到大)
SortCards_src(psrc, n, true, true);
//////从n组里面选取任意1/2/3...k...n组的组合数 //////
//C(n,1),C(n,2),C(n,3)...C(n,k)...C(n,n)
for (int k = 1; k <= n; ++k) {
int c = fnC.FuncC(n, k, vec);
//printf("\n--- *** ------------------- C(%d,%d)=%d\n", n, k, c);
for (std::vector<std::vector<int>>::iterator it = vec.begin();
it != vec.end(); ++it) {
assert(k == it->size());
//printf("\n--- *** start\n");
#if 0
for (std::vector<int>::iterator ir = it->begin();
ir != it->end(); ++ir) {
printf("%d", *ir);
}
printf("\n---\n");
#endif
// for (std::vector<int>::iterator ir = it->begin();
// ir != it->end(); ++ir) {
// if (*ir < c4) {
// PrintCardList(*psrc[*ir], 4);
// }
// else if (*ir < (c4 + c3)) {
// PrintCardList(*psrc[*ir], 3);
// }
// else {
// PrintCardList(*psrc[*ir], 2);
// }
// }
//printf("---\n");
//f(k)=C(n,1)
//Multi(k)=f(1)*f(2)...*f(k)
//////从选取的k组中分别任取一张牌的组合数 //////
int c = DepthVisit(c4, c3, c2, k, psrc, ctx, dst, *it, clr);
//printf("\n--- *** end c=%d\n", c);
}
}
return dst.size();
}
static bool ExistVec(std::vector<std::vector<uint8_t>>& dst, std::vector<uint8_t>& v) {
for (std::vector<std::vector<uint8_t>>::iterator it = dst.begin();
it != dst.end(); ++it) {
if (it->size() == v.size()) {
int i = 0;
for (; i != it->size(); ++i) {
if (v[i] != (*it)[i]) {
break;
}
}
if (i == it->size())
return true;
}
}
return false;
}
//从补位合并后的单张组合牌中枚举连续牌型///////
//src uint8_t const* 单张组合牌源,去重后的余牌/散牌与单张组合牌补位合并
//n int 抽取n张(3/5/13)
//dst1 std::vector<std::vector<uint8_t>>& 存放所有连续牌型(非同花)
//dst2 std::vector<std::vector<uint8_t>>& 存放所有连续牌型(同花)
int CGameLogic::EnumConsecCardsMarkLoc(uint8_t const* src, int len, int n,
std::vector<short>& ctx,
std::vector<std::vector<short>>& dstctx,
std::vector<std::vector<uint8_t>>& dst) {
assert(len > 0);
int i = 0, j = 0/*, k = 0*/, s;
uint8_t v_src_pre = 0;
next:
while (i < len) {
s = i++;
v_src_pre = GetCardValue(src[s]);
if (v_src_pre > 0) {
break;
}
}
if (s + n <= len) {
for (; i < len; ++i) {
//src中当前的牌值
uint8_t v_src_cur = GetCardValue(src[i]);
//牌位有值则连续
if (v_src_cur > 0) {
//收集到n张牌后返回
if (i - s + 1 == n) {
//缓存符合要求的牌型
std::vector<uint8_t> v(&src[s], &src[s] + n);
if (!ExistVec(dst, v)) {
dst.push_back(v);
dstctx.push_back(ctx);
//printf("--- *** dst s:%d i:%d\n", s, i);
PrintCardList(&v.front(), n);
}
++s;
}
}
//牌位无值不连续
else {