-
Notifications
You must be signed in to change notification settings - Fork 12
/
zjh.cpp
848 lines (797 loc) · 20.8 KB
/
zjh.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
//
// 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 "cfg.h"
#include "zjh.h"
namespace ZJH {
//一副扑克(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 SanPai: return "SanPai";
case DuiZi: return "DuiZi";
case ShunZi: return "ShunZi";
case JinHua: return "JinHua";
case ShunJin: return "ShunJin";
case BaoZi: return "BaoZi";
case TeShu235: return "TeShu235";
}
return "";
}
//打印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");
}
//拆分字符串"♦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"->0x01
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();
}
//比较散牌大小
int CGameLogic::CompareSanPai(uint8_t *cards1, uint8_t *cards2)
{
//牌型相同按顺序比点
for (int i = 0; i < 3; ++i) {
uint8_t p0 = CGameLogic::GetCardPoint(cards1[i]);
uint8_t p1 = CGameLogic::GetCardPoint(cards2[i]);
if (p0 != p1) {
return p0 - p1;
}
}
//点数相同按顺序比花色
for (int i = 0; i < 3; ++i) {
uint8_t c0 = GetCardColor(cards1[i]);
uint8_t c1 = GetCardColor(cards2[i]);
if (c0 != c1) {
return c0 - c1;
}
}
return 0;
}
//比较对子大小
int CGameLogic::CompareDuiZi(uint8_t *cards1, uint8_t *cards2)
{
//先比对牌点数
uint8_t p0 = CGameLogic::GetCardPoint(cards1[1]);
uint8_t p1 = CGameLogic::GetCardPoint(cards2[1]);
if (p0 != p1) {
return p0 - p1;
}
//对牌点数相同,比单牌点数
uint8_t sp0 = CGameLogic::GetCardPoint(cards1[0]);
if (sp0 == p0) {
sp0 = CGameLogic::GetCardPoint(cards1[2]);
}
uint8_t sp1 = CGameLogic::GetCardPoint(cards2[0]);
if (sp1 == p1) {
sp1 = CGameLogic::GetCardPoint(cards2[2]);
}
if (sp0 != sp1) {
return sp0 - sp1;
}
//单牌点数相同,比对牌里面最大的花色
uint8_t c0, c1;
if (sp0 == p0) {
c0 = GetCardColor(cards1[0]);
}
else {
c0 = GetCardColor(cards1[1]);
}
if (sp1 == p1) {
c1 = GetCardColor(cards2[0]);
}
else {
c1 = GetCardColor(cards2[1]);
}
return c0 - c1;
}
//比较顺子大小
int CGameLogic::CompareShunZi(uint8_t *cards1, uint8_t *cards2)
{
//牌型相同按顺序比点
for (int i = 0; i < 3; ++i) {
uint8_t p0 = CGameLogic::GetCardPoint(cards1[i]);
uint8_t p1 = CGameLogic::GetCardPoint(cards2[i]);
if (p0 != p1) {
return p0 - p1;
}
}
//点数相同按顺序比花色
for (int i = 0; i < 3; ++i) {
uint8_t c0 = GetCardColor(cards1[i]);
uint8_t c1 = GetCardColor(cards2[i]);
if (c0 != c1) {
return c0 - c1;
}
}
return 0;
}
//比较金花大小
int CGameLogic::CompareJinHua(uint8_t *cards1, uint8_t *cards2)
{
//牌型相同按顺序比点
for (int i = 0; i < 3; ++i) {
uint8_t p0 = CGameLogic::GetCardPoint(cards1[i]);
uint8_t p1 = CGameLogic::GetCardPoint(cards2[i]);
if (p0 != p1) {
return p0 - p1;
}
}
//点数相同按顺序比花色
uint8_t c0 = GetCardColor(cards1[0]);
uint8_t c1 = GetCardColor(cards2[0]);
return c0 - c1;
}
//比较顺金大小
int CGameLogic::CompareShunJin(uint8_t *cards1, uint8_t *cards2)
{
//牌型相同按顺序比点
for (int i = 0; i < 3; ++i) {
uint8_t p0 = CGameLogic::GetCardPoint(cards1[i]);
uint8_t p1 = CGameLogic::GetCardPoint(cards2[i]);
if (p0 != p1) {
return p0 - p1;
}
}
//点数相同按顺序比花色
uint8_t c0 = GetCardColor(cards1[0]);
uint8_t c1 = GetCardColor(cards2[0]);
return c0 - c1;
}
//比较豹子大小
int CGameLogic::CompareBaoZi(uint8_t *cards1, uint8_t *cards2)
{
//炸弹比较点数
uint8_t p0 = CGameLogic::GetCardPoint(cards1[0]);
uint8_t p1 = CGameLogic::GetCardPoint(cards2[0]);
return p0 - p1;
}
//玩家手牌类型
HandTy CGameLogic::GetHandCardsType(uint8_t *cards) {
//手牌按牌点从大到小排序(AK...32)
SortCards(cards, MaxCount, false, false, false);
uint8_t p0 = GetCardPoint(cards[0]);
uint8_t p1 = GetCardPoint(cards[1]);
uint8_t p2 = GetCardPoint(cards[2]);
if (p0 == p1) {
if (p1 == p2) {
//豹子(炸弹):三张值相同的牌
return BaoZi;
}
//对子:二张值相同的牌
return DuiZi;
}
if (p1 == p2) {
//对子:二张值相同的牌
return DuiZi;
}
//三张连续牌
if ((p1 == p2 + 1) && (p0 == p1 + 1 || p0 == p2 + 12)) {
uint8_t c0 = GetCardColor(cards[0]);
uint8_t c1 = GetCardColor(cards[1]);
uint8_t c2 = GetCardColor(cards[2]);
if (p0 == p2 + 12) {
//手牌按牌值从大到小排序 A32->32A
SortCards(cards, MaxCount, true, false, false);
}
if (c0 == c1 && c1 == c2) {
//顺金(同花顺):花色相同的顺子
return ShunJin;
}
//顺子:花色不同的顺子
return ShunZi;
}
uint8_t c0 = GetCardColor(cards[0]);
uint8_t c1 = GetCardColor(cards[1]);
uint8_t c2 = GetCardColor(cards[2]);
if (c0 == c1 && c1 == c2) {
//金花(同花):花色相同,非顺子
return JinHua;
}
if (p0 == 0x05 && p1 == 0x03 && p2 == 0x02) {
//特殊:散牌中的235
return TeShu235;
}
//散牌(高牌/单张):三张牌不组成任何类型的牌
return SanPai;
}
//比较手牌大小 >0-cards1大 <0-cards2大
int CGameLogic::CompareHandCards(uint8_t *cards1, uint8_t *cards2)
{
HandTy t0 = GetHandCardsType(cards1);
HandTy t1 = GetHandCardsType(cards2);
if (t0 == t1) {
//牌型相同情况
if (t0 == TeShu235) {
t0 = t1 = SanPai;
}
label:
switch (t0)
{
case SanPai:
{
return CompareSanPai(cards1, cards2);
}
case DuiZi:
{
return CompareDuiZi(cards1, cards2);
}
case ShunZi:
{
return CompareShunZi(cards1, cards2);
}
case JinHua:
{
return CompareJinHua(cards1, cards2);
}
case ShunJin:
{
return CompareShunJin(cards1, cards2);
}
case BaoZi:
{
return CompareBaoZi(cards1, cards2);
}
}
}
else /*if (t0 != t1)*/ {
//牌型不同情况
if (t0 == TeShu235) {
if (t1 == BaoZi) {
return t0 - t1;
}
t0 = SanPai;
} else if (t1 == TeShu235) {
if (t0 == BaoZi) {
return t1 - t0;
}
t1 = SanPai;
}
if (t0 == t1) {
goto label;
}
//牌型不同比较
return t0 - t1;
}
}
//比较手牌大小 >0-cards1大 <0-cards2大
bool CGameLogic::GreaterHandCards(boost::shared_ptr<uint8_t>& cards1, boost::shared_ptr<uint8_t>& cards2)
{
return CompareHandCards(boost::get_pointer(cards1), boost::get_pointer(cards2)) > 0;
}
};