-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deck.cpp
875 lines (820 loc) · 22.3 KB
/
Deck.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
/*=============================================================================
* TarotClub - Deck.cpp
*=============================================================================
* A deck composed of a (dynamic) list of cards.
*=============================================================================
* TarotClub ( http://www.tarotclub.fr ) - This file is part of TarotClub
* Copyright (C) 2003-2999 - Anthony Rabine
*
* TarotClub is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TarotClub is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TarotClub. If not, see <http://www.gnu.org/licenses/>.
*
*=============================================================================
*/
#include <algorithm>
#include <random>
#include <array>
#include "Deck.h"
const std::string Deck::Sorter::cDefault = "TCSDH";
/*****************************************************************************/
Deck::Deck()
{
}
/*****************************************************************************/
Deck::Deck(const std::string &cards)
{
SetCards(cards);
}
/*****************************************************************************/
void Deck::CreateTarotDeck()
{
Clear();
// The 4 suits
for (std::uint8_t i = 0U; i < 4U; i++)
{
// From ace to the king, 14 cards
for (std::uint8_t j = 1; j <= 14; j++)
{
mDeck.push_back(Card(j, i));
}
}
// The 22 trumps, including the fool
for (std::uint8_t i = 0U; i <= 21U; i++)
{
mDeck.push_back(Card(i, Card::TRUMPS));
}
}
/*****************************************************************************/
/**
* @brief Deck::RemoveDuplicates
*
* This method removes all the cards of the current deck that are common
* with the deck passed in parameters.
*
* @param deck
* @return The number of duplicate cards removed
*/
std::uint32_t Deck::RemoveDuplicates(const Deck &deck)
{
std::uint32_t cardsRemoved = 0U;
for (const auto &c : deck)
{
if (HasCard(c))
{
Remove(c);
cardsRemoved++;
}
}
return cardsRemoved;
}
/*****************************************************************************/
void Deck::Append(const Deck &deck)
{
for (const auto &c : deck)
{
Append(c);
}
}
/*****************************************************************************/
/**
* @brief Deck::Mid
* Creates a temporary deck contains all the cards from pos to the end of the
* deck.
*
* @param from_pos Starting position
* @return the new deck
*/
Deck Deck::Mid(std::uint32_t from_pos)
{
return Mid(from_pos, Size() - from_pos);
}
/*****************************************************************************/
/**
* @brief Deck::Mid
*
* Creates a temporary deck contains "size" cards from a starting position
*
* @param from_pos Starting position
* @param size The number of elements to get
* @return the new deck
*/
Deck Deck::Mid(std::uint32_t from_pos, std::uint32_t size)
{
Deck deck;
std::uint32_t counter = 0U;
// Protection regarding the starting position
if (from_pos >= Size())
{
from_pos = 0U;
}
// Protection regarding the number of elements
if (size > Size())
{
size = Size() - from_pos;
}
// Calculate the last position
std::uint32_t to_pos = from_pos + size;
for (const auto &c : mDeck)
{
if ((counter >= from_pos) &&
(counter < to_pos))
{
deck.Append(c);
}
counter++;
}
return deck;
}
/*****************************************************************************/
Card Deck::At(uint32_t pos)
{
Card card;
std::uint32_t counter = 0U;
for (const auto &c : mDeck)
{
if (pos == counter)
{
card = c;
break;
}
counter++;
}
return card;
}
/*****************************************************************************/
/**
* @brief Deck::Remove
* @param c
* @return The number of cards removed from the deck
*/
std::uint32_t Deck::Remove(const Card &card)
{
std::uint32_t counter = Count(card);
mDeck.erase(std::remove(mDeck.begin(), mDeck.end(), card), mDeck.end());
return counter;
}
/*****************************************************************************/
/**
* @brief Deck::Count
*
* Counts the number of the same cards in the deck
*
* @param c
* @return
*/
std::uint32_t Deck::Count(const Card &card) const
{
std::uint32_t counter = 0U;
for (const auto &c : mDeck)
{
if (c == card)
{
counter++;
}
}
return counter;
}
/*****************************************************************************/
std::string Deck::ToString() const
{
std::string list;
bool first = true;
for (const auto &c : mDeck)
{
if (first)
{
first = false;
}
else
{
list += ";";
}
list += c.ToString();
}
return list;
}
/*****************************************************************************/
void Deck::Shuffle(int seed)
{
std::default_random_engine generator(seed);
// Since the STL random does not work on lists, we have to copy the data
// into a vector, shuffle the vector, and copy it back into a list.
std::vector<Card> myVector(Size());
std::copy(begin(), end(), myVector.begin());
// Actually shuffle the cards
std::shuffle(myVector.begin(), myVector.end(), generator);
// Copy back the intermediate working vector into the Deck
mDeck.assign(myVector.begin(), myVector.end());
}
/*****************************************************************************/
Card Deck::GetCard(const std::string &i_name)
{
Card card;
for (const auto &c : mDeck)
{
if (c.ToString() == i_name)
{
card = c;
}
}
return card;
}
/*****************************************************************************/
bool Deck::HasCard(const Card &card) const
{
bool ret = false;
for (const auto &c : mDeck)
{
if (card == c)
{
ret = true;
}
}
return ret;
}
/*****************************************************************************/
bool Deck::HasOneOfTrump() const
{
bool ret = false;
for (const auto &c : mDeck)
{
if ((c.GetSuit() == Card::TRUMPS) &&
(c.GetValue() == 1U))
{
ret = true;
break;
}
}
return ret;
}
/*****************************************************************************/
bool Deck::HasOnlyOneOfTrump() const
{
bool ret = false;
if (HighestTrump().GetValue() == 1U)
{
if (!HasFool())
{
ret = true;
}
}
return ret;
}
/*****************************************************************************/
bool Deck::HasFool() const
{
bool ret = false;
for (const auto &c : mDeck)
{
if ((c.GetSuit() == Card::TRUMPS) &&
(c.GetValue() == 0U))
{
ret = true;
break;
}
}
return ret;
}
/*****************************************************************************/
/**
* @brief Deck::HighestTrump
*
* This algorithm voluntary eliminates the fool, which as a value of zero.
* It is not considered as the highest trump, even if it is alone in the deck.
*
* @return The highest trump in the deck
*/
Card Deck::HighestTrump() const
{
Card card;
std::uint32_t value = 0U;
for (const auto &c : mDeck)
{
if ((c.GetSuit() == Card::TRUMPS) &&
(c.GetValue() > value))
{
value = c.GetValue();
card = c;
}
}
return card;
}
/*****************************************************************************/
/**
* @brief Deck::HighestSuit
*
* This algorithm take into account that the first card played lead the color
*
* @return the card leader, null if not found
*/
Card Deck::HighestSuit() const
{
Card card;
std::uint32_t value = 0U;
std::uint8_t suit; // leading suit
bool hasLead = false;
for (const auto &c : mDeck)
{
if ((c.GetSuit() != Card::TRUMPS) &&
(c.GetValue() > value))
{
if (!hasLead)
{
hasLead = true;
suit = c.GetSuit();
value = c.GetValue();
card = c;
}
else if (c.GetSuit() == suit)
{
value = c.GetValue();
card = c;
}
}
}
return card;
}
/*****************************************************************************/
Deck Deck::AutoDiscard(const Deck &dog, std::uint8_t nbPlayers)
{
Deck discard;
// We add all the dog cards to the player's deck
Append(dog);
// We're looking valid discard cards to put in the discard
for (const auto &c : mDeck)
{
if ((c.GetSuit() != Card::TRUMPS) && (c.GetValue() != 14U))
{
discard.Append(c);
if (discard.Size() == Tarot::NumberOfDogCards(nbPlayers))
{
// enough cards!
break;
}
}
}
return discard;
}
/*****************************************************************************/
/**
* @brief Player::canPlayCard
*
* Test if the card cVerif can be played depending of the already played cards
* and the cards in the player's hand.
*
* @param mainDeck
* @param cVerif
* @param gameCounter
* @param nbPlayers
* @return true if the card can be played
*/
bool Deck::CanPlayCard(const Card &card, Deck &trick)
{
std::uint8_t suit; // required suit
bool ret = false;
Stats stats;
// Check if the player has the card in hand
if (!HasCard(card))
{
return false;
}
// The player is the first of the trick, he can play any card
if (trick.Size() == 0)
{
return true;
}
// Simple use case, the excuse can always be played
if (card.IsFool())
{
return true;
}
// We retreive the requested suit by looking at the first card played
Card c = trick.At(0);
if (c.IsFool())
{
// The first card is a Excuse...
if (trick.Size() == 1)
{
// ...the player can play everything he wants
return true;
}
// If we are here, it means than we have two or more cards in the trick
// The requested suit is the second card
c = trick.At(1);
}
suit = c.GetSuit();
// Some indications about previous played cards
for (const auto &c : trick)
{
if (c.GetSuit() == Card::TRUMPS)
{
stats.previousTrump = true;
if (c.GetValue() > stats.maxPreviousTrump)
{
stats.maxPreviousTrump = c.GetValue();
}
}
}
// Some indications on the player cards in hand
for (const auto &c : mDeck)
{
if (c.GetSuit() == Card::TRUMPS)
{
stats.hasTrump = true;
if (c.GetValue() > stats.highestTrumpValue)
{
stats.highestTrumpValue = c.GetValue();
}
}
else
{
if (c.GetSuit() == suit)
{
stats.hasSuit = true;
}
}
}
// Card type requested is a trump
if (suit == Card::TRUMPS)
{
ret = TestPlayTrump(card, stats);
}
// Card type requested is a standard card
else
{
// The card is the required suit
if (card.GetSuit() == suit)
{
ret = true;
}
else if (stats.hasSuit == true)
{
// not the required card, but he has the suit in hands
// he must play the required suit
ret = false;
}
else
{
// We are here if the player has not the requested suit
ret = TestPlayTrump(card, stats);
}
}
return ret;
}
/*****************************************************************************/
/**
* @brief Player::TestPlayTrump
*
* This method test if the player can play a trump
*
* @param cVerif
* @param hasTrump
* @param maxPreviousTrump
* @return
*/
bool Deck::TestPlayTrump(const Card &card, const Stats &stats)
{
bool ret = false;
// He must play a trump if he has some, higher than the highest previous played trump,
// or any other cards in other case
if (card.GetSuit() == Card::TRUMPS)
{
// He may have to play a higher trump
if (stats.previousTrump == true)
{
if (card.GetValue() > stats.maxPreviousTrump)
{
// higher card, ok!
ret = true;
}
else
{
// does he have a higher trump in hands?
if (stats.highestTrumpValue > stats.maxPreviousTrump)
{
ret = false; // yes, he must play it
}
else
{
ret = true;
}
}
}
else
{
// No any previous trump played, so he can play any value
ret = true;
}
}
else
{
// Does he have a trump?
if (stats.hasTrump == true)
{
// If he has only one trump and this trump is the fool, then he can play any card
if (stats.highestTrumpValue == 0)
{
ret = true;
}
else
{
ret = false; // he must play a trump
}
}
else
{
ret = true; // he can play any card
}
}
return ret;
}
/*****************************************************************************/
bool Deck::TestHandle(const Deck &handle)
{
bool ret = true;
Deck::Statistics stats;
// Check if the handle size is correct
if (Tarot::GetHandleType(handle.Size()) == Tarot::NO_HANDLE)
{
ret = false;
}
// Test if the handle contains only trumps
stats.Reset();
handle.AnalyzeTrumps(stats);
if (handle.Size() != stats.trumps)
{
ret = false;
}
// Test if the player has all the cards of the declared handle
for (const auto &c : handle)
{
if (!HasCard(c))
{
ret = false;
}
}
// If the fool is shown, then it indicates that there is no more any trumps in the player's hand
stats.Reset();
AnalyzeTrumps(stats);
if ((handle.HasFool() == true) && (stats.trumps > handle.Size()))
{
ret = false;
}
return ret;
}
/*****************************************************************************/
bool Deck::TestDiscard(const Deck &discard, const Deck &dog, std::uint8_t numberOfPlayers)
{
bool valid = true;
if (discard.Size() == Tarot::NumberOfDogCards(numberOfPlayers))
{
for (const auto &c : discard)
{
// Look if the card belongs to the dog or the player's deck
if (HasCard(c) || dog.HasCard(c))
{
// Look the card value against the Tarot rules
if ((c.GetSuit() == Card::TRUMPS) ||
((c.GetSuit() != Card::TRUMPS) && (c.GetValue() == 14U)))
{
valid = false;
}
// Look if this card is unique
if (discard.Count(c) != 1U)
{
valid = false;
}
}
else
{
valid = false;
}
}
}
else
{
valid = false;
}
return valid;
}
/*****************************************************************************/
/**
* @brief Sort the deck of card, optional order can be given
* The sorting order is given in parameter as a string format, one letter
* per suit.
* Example:
* "THSDC" will sort cards in the following format:
* Trumps, Hearts, Spades, Diamonds, Clubs
*
* @param order String containing the order of suits
*/
void Deck::Sort(const std::string &order)
{
if (Size() != 0)
{
Sorter sorter(order);
std::sort(mDeck.begin(), mDeck.end(), sorter);
}
}
/*****************************************************************************/
void Deck::SetOwner(Team team)
{
mOwner = team;
}
/*****************************************************************************/
std::uint8_t Deck::SetCards(const std::string &cards)
{
std::uint8_t count = 0U;
std::size_t found = std::string::npos;
std::size_t pos = 0;
// Clear this deck before setting new cards
Clear();
do
{
std::size_t size;
found = cards.find(';', pos);
if (found != std::string::npos)
{
// calculate size of the string between the delimiters
size = found - pos;
}
else
{
// last card: get remaining characters
size = cards.size() - pos;
}
std::string cardName = cards.substr(pos, size);
pos = found + 1;
count++;
Card card(cardName);
if (card.IsValid())
{
mDeck.push_back(card);
}
}
while (found != std::string::npos);
return count;
}
/*****************************************************************************/
Team Deck::GetOwner()
{
return mOwner;
}
/*****************************************************************************/
Card Deck::Last()
{
return mDeck.back();
}
/*****************************************************************************/
void Deck::Statistics::Reset()
{
nbCards = 0U;
oudlers = 0U;
trumps = 0U;
majorTrumps = 0U;
kings = 0U;
queens = 0U;
knights = 0U;
jacks = 0U;
weddings = 0U;
longSuits = 0U;
cuts = 0U;
singletons = 0U;
sequences = 0U;
for (std::uint8_t i = 0U; i < 4U; i++)
{
suits[i] = 0U;
}
littleTrump = false;
bigTrump = false;
fool = false;
points = 0.0F;
}
/*****************************************************************************/
void Deck::AnalyzeTrumps(Statistics &stats) const
{
int val;
stats.nbCards = Size();
// looking for trumps
for (const auto &c : mDeck)
{
if (c.GetSuit() == Card::TRUMPS)
{
stats.trumps++;
val = c.GetValue();
if (val >= 15)
{
stats.majorTrumps++;
}
if (val == 21)
{
stats.bigTrump = true;
stats.oudlers++;
}
if (val == 1)
{
stats.littleTrump = true;
stats.oudlers++;
}
if (val == 0)
{
stats.fool = true;
stats.oudlers++;
}
}
stats.points += c.GetPoints();
}
}
/*****************************************************************************/
void Deck::AnalyzeSuits(Statistics &stats)
{
std::uint8_t k;
// true if the card is available in the deck
std::array<bool, 14U> distr;
// Normal suits
for (std::uint8_t suit = 0U; suit < 4U; suit++)
{
std::uint8_t count = 0U; // Generic purpose counter
distr.fill(false);
for (const auto &c : mDeck)
{
if (c.GetSuit() == suit)
{
count++;
std::uint8_t val = c.GetValue();
distr[val - 1U] = true;
if (val == 11U)
{
stats.jacks++;
}
if (val == 12U)
{
stats.knights++;
}
if (val == 13U)
{
stats.queens++;
}
if (val == 14U)
{
stats.kings++;
}
}
}
stats.suits[suit] = count;
if (count >= 5U)
{
stats.longSuits++;
}
if (count == 1U)
{
stats.singletons++;
}
if (count == 0U)
{
stats.cuts++;
}
if (distr[13] && distr[12])
{
stats.weddings++; // king + queen
}
// Sequence detection
count = 0U; // sequence length
bool detected = false; // sequence detected
for (k = 0U; k < 14U; k++)
{
if (distr[k])
{
count++;
if (!detected)
{
if (count >= 5U)
{
// Ok, found sequence, enough for it
stats.sequences++;
detected = true;
}
}
}
else
{
count = 0U;
detected = false;
}
}
}
}
//=============================================================================
// End of file Deck.cpp
//=============================================================================