-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.cpp
More file actions
98 lines (81 loc) · 2.14 KB
/
Deck.cpp
File metadata and controls
98 lines (81 loc) · 2.14 KB
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
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include "Card.h"
#include "Deck.h"
using namespace std;
// You can call members functions in constructors, but
// YOU CANNOT CALL CONSTRUCTORS IN MEMBERS FUNCTIONS
// Deck default constructor
Deck::Deck() {
reset();
}
// Reseting the deck
void Deck::reset() {
int x = 0;
while (x < DECK_SIZE) {
for (int suit = 0; suit < 4; ++suit) {
for (int rank = 0; rank < 13; ++rank) {
cards[x] = Card(static_cast<Card::Rank>(rank), static_cast<Card::Suit>(suit));
x++;
}
}
}
// next pointer is reset to first card in array
next = &cards[0];
}
// Shuffling the deck
void Deck::shuffle(int n) {
// For invalid inputs
if(n < 0 || n > 52){
cout << "Invalid place to cut the deck" << endl;
return;
}
// Making a new array to hold cards from 1st card
// to the nth card
Card cards_array[52];
int card_counter = 0;
int left_in = 0;
int right_in = n;
// Copying over 1st card to nth card into the
// new array
for(int x = 0; x < n; ++x) {
cards_array[x] = cards[x];
}
// Updating original cards array in deck
// by putting first card of right, and then
// first card of left, and so on
while(left_in < n && right_in < 52) {
cards[card_counter] = cards[right_in];
card_counter++;
right_in++;
cards[card_counter] = cards_array[left_in];
card_counter++;
left_in++;
}
// This is for when the left deck has more cards
// than the right deck. If the left deck has more
// cards, then the right will get exhausted first.
// So we use this while loop to place the rest
// of the left deck into the original cards array
// in deck.
if(n > 26) {
for(int x = card_counter; x < 52; ++x) {
cards[x] = cards_array[left_in];
left_in++;
}
}
// Reseting next pointer to first card in array
next = &cards[0];
}
// Returns the Card at the position next is at
Card Deck::deal() {
return *next++;
}
// Cards remaining in the deck left to deal
int Deck::cards_remaining() const {
return 52 - (next - cards);
}