-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.cpp
60 lines (53 loc) · 1.16 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
/**
* @file deck.cpp
* @author DSN
* @brief Defines member functions of \c Deck
* @version 0.2
* @date 2023-08-19
*
*/
#include "deck.h"
#include <random>
#include <ctime>
#include <algorithm>
#include <iostream>
/**
* @details
* Create 52 cards to initialize the deck
*
*/
Deck::Deck()
{
using Index = CardArray::size_type;
Index index{ 0 };
for (std::size_t suit{ 0 }; suit < getNumSuits(); ++suit)
for (std::size_t rank{ 0 }; rank < getNumRanks(); ++rank)
m_deck[index++] = Card(getRank(rank), getSuit(suit));
}
void Deck::print() const
{
for (auto& card : m_deck)
std::cout << card << ' ';
}
/**
* @details
* - Uses a Mersenne Twister to arrange the deck in a random order
* - Sets \c m_cardIndex to 0.
*
*/
void Deck::shuffle()
{
// static so that it gets seeded only once
static std::mt19937 mt{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
std::shuffle(m_deck.begin(), m_deck.end(), mt);
m_cardIndex = 0;
}
/**
* @details
* - Deals a card pointed to by \c m_cardIndex
*
*/
const Card& Deck::dealCard()
{
return m_deck[m_cardIndex++];
}