This repository has been archived by the owner on Feb 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mjcard.cpp
888 lines (741 loc) · 18.5 KB
/
mjcard.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
#include <iostream>
#include "mjcard.h"
mjcard::mjcard() {
for (int i = 0; i < 17; i++) {
card[i] = -1;
_ocard[i] = -1;
}
for (int i = 0; i < 44; i++) {
card_times[i] = 4;
}
cptr = 0;
_ocptr = 0;
need_status = "NULL";
playernumber = -1;
}
mjcard::mjcard(int *data, int datalen) : mjcard() { //: mjcard() this is can call constructor
for (int i = 0; i < datalen; i++) {
push(data[i]);
}
}
void mjcard::transfer(int data) {
if (!find(data))
throw std::runtime_error("transfer no find match");
cptr--;
int temp_index = find_index(data);
if (temp_index == -1)
throw 123;
swap(temp_index, cptr);
_ocard[_ocptr] = card[cptr];
_ocptr++;
card[cptr] = -1;
}
//return _buf len
int mjcard::card_merge(int *_buf) {
int len = 0;
for (int i = 0; i < cptr; i++) {
_buf[len] = card[i];
len++;
}
for (int i = 0; i < _ocptr; i++) {
_buf[len] = _ocard[i];
len++;
}
return len;
}
//print card to chinese
void mjcard::print_c_card(int *tempcard, int cardlen) {
std::string stack;
std::string dcm = "萬條筒風中財板";
std::string chinesemath = "一二三四五六七八九東南西北紅發白";
std::cout << "\n";
for (int i = 0; i < cardlen; i++) {
char c[3];
if (tempcard[i] < 30) {
c[0] = chinesemath.at((tempcard[i] % 10 - 1) * 2);
c[1] = chinesemath.at((tempcard[i] % 10 - 1) * 2 + 1);
c[2] = '\0';
}
else {
c[0] = chinesemath.at((tempcard[i] % 31) + 18);
c[1] = chinesemath.at((tempcard[i] % 31) + 19);
c[2] = '\0';
}
std::cout << c << " ";
if (tempcard[i] < 10) {
stack.push_back(0);
}
else if (tempcard[i] < 20 && tempcard[i] > 10) {
stack.push_back(2);
}
else if (tempcard[i] < 30 && tempcard[i] > 20) {
stack.push_back(4);
}
else if (tempcard[i] < 38 && tempcard[i] > 30) {
stack.push_back(6);
}
else if (tempcard[i] == 39) {
stack.push_back(8);
}
else if (tempcard[i] == 41) {
stack.push_back(10);
}
else if (tempcard[i] == 43) {
stack.push_back(12);
}
}
std::cout << "\n";
for (int i = 0; i < static_cast<int>(stack.size()); i++) {
char c[3];
c[0] = dcm.at(stack.at(i));
c[1] = dcm.at(stack.at(i) + 1);
c[2] = '\0';
std::cout << c << " ";
}
std::cout << "\n";
}
void mjcard::push(int data) {
card[cptr] = data;
cptr++;
set_mycardtimes(data);
}
int mjcard::pop() {
cptr--;
int temp = card[cptr];
card[cptr] = -1;
set_need_card();
return temp;
}
int mjcard::pop(int index) {
cptr--;
swap(index, cptr);
int temp = card[cptr];
card[cptr] = -1;
return temp;
}
int mjcard::del(int assign) {
for (int i = 0; i < cptr; i++) {
if (card[i] == assign) {
return pop(i);
}
}
return -1;
}
void mjcard::swap(int index_1,int index_2) {
std::swap(card[index_1], card[index_2]);
}
bool mjcard::find(int assign) {
for (int i = 0; i < cptr; i++) {
if (assign == card[i]) {
return true;
}
}
return false;
}
int mjcard::find_index(int assign) {
for (int i = 0; i < 17; i++) {
if (assign == card[i]) {
return i;
}
else if (card[i] <= 0 || card[i] >= 100)
continue;
}
return -1;
}
void mjcard::arrange() {
card_sort(card, cptr, true);
card_grouping();
set_need_card();
}
bool mjcard::check_who() {
return card_check(card, cptr);
}
bool mjcard::check_who(int data) {
int tempcard[17];
if ((cptr + _ocptr) >= 17)
return false;
card_cpy(card, tempcard, cptr);
tempcard[cptr] = data;
return card_check(tempcard, cptr);
}
bool mjcard::check_eat(int data) {
if (card_expect_eat(card, cptr, data))
return true;
return false;
}
bool mjcard::check_pung(int data) {
if (getcardtimes(card, cptr, data) >= 2)
return true;
return false;
}
void mjcard::print() {
for (int i = 0; i < cptr; i++) {
std::cout << card[i] << " ";
}
std::cout << "\n";
}
void mjcard::print_o() {
for (int i = 0; i < _ocptr; i++) {
std::cout << _ocard[i] << " ";
}
std::cout << "\n";
}
void mjcard::print_c() {
print_c_card(card, cptr);
}
void mjcard::print_o_c() {
print_c_card(_ocard, _ocptr);
}
std::vector<int> mjcard::get_card() {
std::vector<int> v;
int tempcard[17];
int len = card_merge(tempcard);
for (int i = 0; i < len; i++) {
v.push_back(tempcard[i]);
}
return v;
}
int mjcard::get_cptr() {
return cptr;
}
int mjcard::get_ocptr() {
return _ocptr;
}
int mjcard::need(int *_buf,int assign) {
std::vector<int> v = need(assign);
if (v.size() >= 2)
return -1;
for (int i = 0; i < static_cast<int>(v.size()); i++) {
_buf[i] = v.at(i);
}
return static_cast<int>(v.size());
}
std::vector<int> mjcard::need(int assign) {
std::vector<int> vc;
need_status.clear();
for (int i = 0; i < static_cast<int>(need_card.size()); i++) {
if (assign == need_card.at(i)) {
vc.push_back(need_card_assist.at(i * 2));
vc.push_back(need_card_assist.at(i * 2 + 1));
if (vc.at(0) == vc.at(1)) {
need_status = "pung";
}
else {
need_status = "eat";
}
return vc;
}
}
need_status = "NULL";
return vc;
}
std::string mjcard::get_need_status() {
return need_status;
}
//吃碰槓時才利用這func 來讓成對的卡轉移,單純拿到牌的話就用push()就好
void mjcard::get(int data1,int data2,int data3) {
transfer(data1);
transfer(data2);
_ocard[_ocptr] = data3;
_ocptr++;
/*
int nofind = -1;
std::vector<int> arr;
arr.push_back(data1);
arr.push_back(data2);
arr.push_back(data3);
for (int i = 0; i < 3; i++) {
if (!find(arr.at(i))) {
if (nofind != -1) {
print();
print_o();
std::cout << data1 << " " << data2 << " " << data3 << "\n";
std::cout << std::endl;
throw false;
}
else
nofind = i;
}
}
for (int i = 0; i < 3; i++) {
if (i == nofind) {
_ocard[_ocptr] = arr.at(i);
_ocptr++;
}
else {
if (nofind == -1 && i == 2) {
_ocard[_ocptr] = arr.at(i);
_ocptr++;
}
else
{
transfer(arr.at(i));
}
}
}
*/
}
int mjcard::out() {
int index = card_price(card, cptr);
int outcard = card[index];
cptr--;
swap(index, cptr);
card[cptr] = -1;
return outcard;
}
#pragma region AI
//估價函式,return index , can throw;
int mjcard::card_price(int *tempcard, int len) {
//這邊不能複製card array 如果用副本來做sort後,回傳的index會是錯誤的
//先sort一遍
card_sort(tempcard, len, true);
//0.-先丟單獨的字牌
for (int i = 0; i < len; i++) {
if (tempcard[i] >= 31) { //如果是字牌
if (getcardtimes(tempcard, len, tempcard[i]) >= 2) { //如果>=2張
continue;
}
else {
return i;
}
}
}
//1.-再丟孤張
for (int i = 0; i < len; i++) {
if (tempcard[i] < 30) { //如果不是字牌 than
if (card_expect(tempcard, len, i) == false) //如果沒有可以等的順組 or 對組 than
return i;
}
}
//2.-真正的估價函式-期望值
int index = card_expect(tempcard, len, card_times);
if (index != -1) {
return index;
}
/*要補作丟別人丟過的牌*/
return 0;
}
//拿出已完成的順對組,return 改變後的len的值
void mjcard::card_grouping() {
std::vector<int> buf[4];
int expect[4];
expect[0] = card_organize(card, cptr, buf[0], true, true);
expect[1] = card_organize(card, cptr, buf[1], true, false);
expect[2] = card_organize(card, cptr, buf[2], false, true);
expect[3] = card_organize(card, cptr, buf[3], false, false);
int max = 0;
for (int i = 0; i < 4; i++) {
if (expect[max] < expect[i]) {
max = i;
}
}
for (int i = 1; i < static_cast<int>(buf[max].size()); i++) {
card[i - 1] = buf[max].at(i);
}
cptr = buf[max].at(0);
for (int i = cptr; i < 17; i++) {
if (card[i] != -1) {
_ocard[_ocptr] = card[i];
card[i] = -1;
_ocptr++;
}
}
}
//card_organize的子函式,用來拿出順and對組到後面,return 總期望值
int mjcard::card_organize(int *tempcard, int len, std::vector<int> &buf, bool smallorder, bool preorder) {
int m, buf_order[17], buf_pair[17], optr = 0, pptr = 0, _buf[17], bptr = 0;
//clean
for (int i = 0; i < 17; i++) {
buf_order[i] = -1;
buf_pair[i] = -1;
_buf[i] = -1;
}
//先排序一遍
card_sort(tempcard, len, smallorder);
//set m 依照排列順序,m要跟著變換
if (smallorder) {
m = 1;
}
else {
m = -1;
}
//決定先取順組還是先取對組
if (preorder) {
optr = card_check_order(tempcard, len, buf_order, m);
pptr = card_check_pair(tempcard, len, buf_order, optr, buf_pair);
}
else {
pptr = card_check_pair(tempcard, len, buf_pair);
optr = card_check_order(tempcard, len, buf_pair, pptr, buf_order, m);
}
buf.push_back(len - optr - pptr); //分隔點
//要找出最後那2張當眼的,並記錄到buf[]裡
for (int i = 0; i < len; i++) {
if (cmp(buf_order, optr, 0, i) == -1 && cmp(buf_pair, pptr, 0, i) == -1) {
buf.push_back(tempcard[i]);
_buf[bptr] = tempcard[i];
bptr++;
}
}
for (int i = 0; i < optr; i++) {
buf.push_back(tempcard[buf_order[i]]);
}
for (int i = 0; i < pptr; i++) {
buf.push_back(tempcard[buf_pair[i]]);
}
return get_card_expect(_buf, bptr);
}
//傳回總期望值
int mjcard::get_card_expect(int *tcard, int len) {
std::vector<int> calculated, expect;
int tempcard[17];
card_cpy(tcard, tempcard, len);
card_sort(tempcard, len, true); //先排序-小到大
//這邊不討論孤張,因為在上一層的function已經判斷過了
for (int i = 0; i < len - 1; i++) {
int temp = 0;
if (!card_level(tempcard[i], tempcard[i + 1])) { //如果不在同個量級的話 than
continue;
}
if (tempcard[i + 1] - tempcard[i] >= 3) {
continue;
}
if (tempcard[i] <= 0) {
continue;
}
if (tempcard[i] == tempcard[i + 1]) { //如果是2張對組的話 than
if (search_vector(calculated, tempcard[i]))
continue;
expect.push_back(card_times[tempcard[i]]);
calculated.push_back(tempcard[i]);
}
else { //不是對組那就是順組了,如果是順組的話 than
if (tempcard[i] > 30)
continue;
if (tempcard[i + 1] - tempcard[i] == 1) { //如果是 34 這類的牌型 than
if (tempcard[i] % 10 == 1) {
if (search_vector(calculated, tempcard[i + 1] + 1))
continue;
expect.push_back(card_times[tempcard[i + 1] + 1]);
calculated.push_back(tempcard[i + 1] + 1);
}
else if (tempcard[i] % 10 == 8) {
if (search_vector(calculated, tempcard[i] - 1))
continue;
expect.push_back(card_times[tempcard[i] - 1]);
calculated.push_back(tempcard[i] - 1);
}
else {
if (!search_vector(calculated, tempcard[i] - 1)) {
temp += card_times[tempcard[i] - 1];
calculated.push_back(tempcard[i] - 1);
}
if (!search_vector(calculated, tempcard[i + 1] + 1)) {
temp += card_times[tempcard[i + 1] + 1];
calculated.push_back(tempcard[i + 1] + 1);
}
expect.push_back(temp);
}
}
else { //如果是 35這類的牌型 than
if (search_vector(calculated, tempcard[i] + 1))
continue;
expect.push_back(card_times[tempcard[i] + 1]);
calculated.push_back(tempcard[i] + 1);
}
}
}
int temp = 0;
for (int i = 0; i < static_cast<int>(expect.size()); i++) {
temp += expect.at(i);
}
return temp;
}
//算出期望值以便丟牌,return 要丟的牌的index
int mjcard::card_expect(int *tempcard, int len, int *cardtimes) {
std::vector<int> buf, calculated, expect;
int times[44]; //看出現次數,來讓出現次數少的被丟出去,心中要有個graph
for (int i = 0; i < 44; i++)
times[i] = 0;
card_sort(tempcard, len, true); //先排序-小到大
//這邊不討論孤張,因為在上一層的function已經判斷過了
for (int i = 0; i < len - 1; i++) {
int temp = 0;
if (!card_level(tempcard[i], tempcard[i + 1])) { //如果不在同個量級的話 than
expect.push_back(99);
continue;
}
if (tempcard[i + 1] - tempcard[i] >= 3) {
expect.push_back(99);
times[tempcard[i]]++;
times[tempcard[i + 1]]++;
continue;
}
if (tempcard[i] == tempcard[i + 1]) { //如果是2張對組的話 than
expect.push_back(cardtimes[tempcard[i]]);
}
else { //不是對組那就是順組了,如果是順組的話 than
if (tempcard[i + 1] - tempcard[i] == 1) { //如果是 34 這類的牌型 than
if (tempcard[i] % 10 == 1) {
expect.push_back(cardtimes[tempcard[i + 1] + 1]);
}
else if (tempcard[i] % 10 == 8) {
expect.push_back(cardtimes[tempcard[i] - 1]);
}
else {
temp += cardtimes[tempcard[i] - 1];
temp += cardtimes[tempcard[i + 1] + 1];
expect.push_back(temp);
}
}
else { //如果是 35這類的牌型 than
expect.push_back(cardtimes[tempcard[i] + 1]);
}
}
//出現次數++
times[tempcard[i]]++;
times[tempcard[i + 1]]++;
}
int minexpect = search_vextor_min(expect); //先取得最小期望值的那個對or順組
int min = search_array_min(times, len); //取得出現次數小的
//丟期望值低+連結次數少
for (int i = 0; i < static_cast<int>(expect.size()); i++) {
//如果期望值最低,而且出現次數也最小,就return index
if (minexpect == -1)
break;
if (times[tempcard[minexpect]] == min)
return minexpect;
if (times[tempcard[minexpect + 1]] == min)
return minexpect + 1;
minexpect = search_vector_index(expect, minexpect + 1, expect.at(minexpect)); //下一個較低期望值的index
}
//丟期望值低的
minexpect = search_vextor_min(expect); //重新取得最小期望值的對組or順組
//丟出較靠1&9的數字
if (abs(5 - tempcard[minexpect]) > abs(5 - tempcard[minexpect + 1])) //ex:abs(5-1)>abs(5-2)
return minexpect;
else if (abs(5 - tempcard[minexpect]) < abs(5 - tempcard[minexpect + 1]))
return minexpect + 1;
return -1;
}
//這張牌,有沒有組成可以等待的順or對 ex:input 5 search for 3 4 5 6 7
//cptr為指定的index
bool mjcard::card_expect(int *tempcard, int len, int cptr) {
std::vector<int> buf;
if (tempcard[cptr] % 10 == 1) {
buf.push_back(tempcard[cptr] + 1);
buf.push_back(tempcard[cptr] + 2);
}
else if (tempcard[cptr] % 10 == 9) {
buf.push_back(tempcard[cptr] - 2);
buf.push_back(tempcard[cptr] - 1);
}
else if (tempcard[cptr] % 10 == 2) {
buf.push_back(tempcard[cptr] - 1);
buf.push_back(tempcard[cptr] + 1);
buf.push_back(tempcard[cptr] + 2);
}
else if (tempcard[cptr] % 10 == 8) {
buf.push_back(tempcard[cptr] - 2);
buf.push_back(tempcard[cptr] - 1);
buf.push_back(tempcard[cptr] + 1);
}
else {
buf.push_back(tempcard[cptr] - 2);
buf.push_back(tempcard[cptr] - 1);
buf.push_back(tempcard[cptr] + 1);
buf.push_back(tempcard[cptr] + 2);
}
//看有沒有可以等的順組
int count = 0;
for (int i = 0; i < static_cast<int>(buf.size()); i++) {
if (cmp(tempcard, len, 0, buf.at(i)) != -1)
count++;
else
count--;
if (count == 2)
return true;
}
//看有沒有可以等的對組
if (getcardtimes(tempcard, len, tempcard[cptr]) >= 2)
return true;
return false;
}
//這張牌,有沒有組成可以等待的順 ex:input 5 search for 3 4 6 7
//data為輸入的數字
bool mjcard::card_expect_eat(int *tempcard, int len, int data) {
std::vector<int> buf;
if (data % 10 == 1) {
buf.push_back(data + 1);
buf.push_back(data + 2);
}
else if (data % 10 == 9) {
buf.push_back(data - 2);
buf.push_back(data - 1);
}
else if (data % 10 == 2) {
buf.push_back(data - 1);
buf.push_back(data + 1);
buf.push_back(data + 2);
}
else if (data % 10 == 8) {
buf.push_back(data - 2);
buf.push_back(data - 1);
buf.push_back(data + 1);
}
else {
buf.push_back(data - 2);
buf.push_back(data - 1);
buf.push_back(data + 1);
buf.push_back(data + 2);
}
//看有沒有可以等的順組
int count = 0;
for (int i = 0; i < static_cast<int>(buf.size()); i++) {
if (cmp(tempcard, len, 0, buf.at(i)) != -1)
count++;
else
count--;
if (count == 2)
return true;
}
return false;
}
//判斷這兩張牌是不是在同個量級
//yes return true not return false
bool mjcard::card_level(int card1, int card2) {
if (card1 < 10 && card1>0 && card2 < 10 && card2>0) { //萬字
return true;
}
if (card1 < 20 && card1>10 && card2 < 20 && card2>10) { //條子
return true;
}
if (card1 < 30 && card1>20 && card2 < 30 && card2>20) { //筒子
return true;
}
if (card1 > 30 && card1 < 44 && card2>30 && card2 < 44) //字牌
return true;
return false;
}
//回傳*data裡的最小值
int mjcard::search_array_min(int *data, int len) {
int min = 1;
for (int i = 0; i < len; i++) {
if (!data[i])
continue;
if (min > data[i])
min = i;
}
return min;
}
//找buf最小值 return index
int mjcard::search_vextor_min(std::vector<int> &buf) {
int min = 0;
for (int i = 0; i < static_cast<int>(buf.size()); i++) {
if (buf.at(min) > buf.at(i))
min = i;
}
return min;
}
//找assgin的值有沒有在buf裡出現過
bool mjcard::search_vector(std::vector<int> &buf, int assign) {
for (int i = 0; i < static_cast<int>(buf.size()); i++) {
if (buf.at(i) == assign)
return true;
}
return false;
}
//找出vector buf裡的指定值,return index
int mjcard::search_vector_index(std::vector<int> &buf, int start, int assign) {
for (int i = start; i < static_cast<int>(buf.size()); i++) {
if (buf.at(i) == assign)
return i;
}
return -1;
}
void mjcard::set_mycardtimes(int *tempcard, int len) {
for (int i = 0; i < len; i++) {
if (tempcard[i] == -1)
continue;
card_times[tempcard[i]]--;
}
}
void mjcard::set_mycardtimes(int tempcard) {
if (!tempcard)
return;
card_times[tempcard]--;
}
void mjcard::set_mycardtimes(std::vector<int> data) {
if (data.size() == 0)
return;
for (std::vector<int>::iterator it = data.begin(); it != data.end(); ++it) {
card_times[*it]--;
}
}
//取得指定數字m牌裡出現幾次,
int mjcard::getcardtimes(int *tempcard, int len, int m) {
int count = 0;
for (int i = 0; i < len; i++) {
if (tempcard[i] == m) {
count++;
}
}
return count;
}
//看缺什麼牌
void mjcard::set_need_card() {
int len = cptr;
int tcard[17];
card_cpy(card, tcard, len);
card_sort(tcard, len, true); //先排序-小到大
need_card.clear();
need_card_assist.clear();
//這邊不討論孤張,因為在上一層的function已經判斷過了
for (int i = 0; i < len - 1; i++) {
int temp = 0;
if (!card_level(tcard[i], tcard[i + 1])) { //如果不在同個量級的話 than
continue;
}
if (tcard[i + 1] - tcard[i] >= 3) { //如果無法成順組 than
continue;
}
if (tcard[i] == tcard[i + 1]) { //如果是2張對組的話 than
need_card.push_back(tcard[i]); //push實際數字
//push assist
need_card_assist.push_back(tcard[i]);
need_card_assist.push_back(tcard[i]);
}
else { //不是對組那就是順組了,如果是順組的話 than
if (tcard[i + 1] - tcard[i] == 1) { //如果是 34 這類的牌型 than
if (tcard[i] % 10 == 1) {
need_card.push_back(tcard[i + 1] + 1);
}
else if (tcard[i] % 10 == 8) {
need_card.push_back(tcard[i] - 1);
}
else {
need_card.push_back(tcard[i] - 1);
need_card.push_back(tcard[i + 1] + 1);
//push assist
need_card_assist.push_back(tcard[i]);
need_card_assist.push_back(tcard[i + 1]);
}
}
else { //如果是 35這類的牌型 than
need_card.push_back(tcard[i] + 1);
}
//push assist
need_card_assist.push_back(tcard[i]);
need_card_assist.push_back(tcard[i + 1]);
}
}
//print
/*
for (int i = 0; i < static_cast<int>(need_card.size()); i++)
std::cout << need_card.at(i) << " ";
std::cout << "\n\n";
for (int i = 0; i < static_cast<int>(need_card_assist.size()); i++)
std::cout << need_card_assist.at(i) << " ";
*/
}
#pragma endregion</spin>