-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeck.cpp
76 lines (63 loc) · 2.02 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
/// File: deck.cpp
/// This class is responsible for creating and managing the deck for the game.
///
#include "deck.h"
#include "card.h"
#include "random.h"
#include <algorithm>
#include <iostream>
using namespace std;
const int NUMBER_OF_CARDS = 52;
const int NUMBER_OF_RANKS = 13;
const int NUMBER_OF_SUITS = 4;
/// No Argument constructor – Creates a dynamically allocated array of Card* objects and initialises them. Initialises cardsDealt to 0.
Deck::Deck() {
deckArray = new Card*[NUMBER_OF_CARDS];
int index = 0;
for (int rank = 0; rank < NUMBER_OF_RANKS; rank++) {
for (int suit = 0; suit < NUMBER_OF_SUITS; suit++) {
Card *card = new Card((Rank)rank, (Suit)suit);
deckArray[index] = card;
index++;
}
}
cardsDealt = 0;
}
/// Returns a pointer to the next Card object from the deck, increments cardsDealt.
Card* Deck::dealNextCard() {
Card* dealtCard = deckArray[cardsDealt];
cardsDealt++;
return dealtCard;
}
/// Sets cardsDealt to 0.
void Deck::reset() {
cardsDealt = 0;
}
/// Shuffles cards in the deck.
void Deck::shuffle() {
Random randomizer;
random_shuffle(&deckArray[0], &deckArray[NUMBER_OF_CARDS]);
}
/// Puts a string representation of the Card objects in the deck to the screen.
ostream& operator<<(ostream& out, Deck& deck) {
for(int card = 0; card < NUMBER_OF_CARDS; card++) {
Card* dealtCard = deck.deckArray[card];
out << *dealtCard << " ";
}
return out;
}
/// Reads 52 strings from an input stream, where each string represents a card e.g. 2C, AD, JH,
/// constructing a Card* and assigning them into the array of Card*.
istream& operator>>(istream& in, Deck& deck) {
string cardString;
for(int index = 0; index < NUMBER_OF_CARDS; index++) {
in >> cardString;
Card *card = new Card(cardString);
deck.deckArray[index] = card;
}
return in;
}
/// Destructor – deletes the contents of the array of Card*.
Deck::~Deck() {
delete[] deckArray;
}