-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPack.cpp
More file actions
75 lines (70 loc) · 2.25 KB
/
Pack.cpp
File metadata and controls
75 lines (70 loc) · 2.25 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
#include "Pack.hpp"
#include <cassert>
#include <vector>
#include <iostream>
// EFFECTS: Initializes the Pack to be in the following standard order:
// the cards of the lowest suit arranged from lowest rank to
// highest rank, followed by the cards of the next lowest suit
// in order from lowest to highest rank, and so on.
Pack::Pack() {
next = 0;
int iterator = 0;
for (int s = RED; s <= BLUE; ++s) {
Suit suit = static_cast<Suit>(s);
for (int r = ZERO; r <= PLUS4; ++r) {
Rank rank = static_cast<Rank>(r);
Card c(rank, suit);
cards.at(iterator) = c;
++iterator;
}
}
}
// REQUIRES: pack_input contains a valid representation of a Pack
// MODIFIES: pack_input
// EFFECTS: Initializes Pack by reading from pack_input.
Pack::Pack(std::istream& pack_input) {
int iterator = 0;
while (iterator < 52) {
Card c;
operator>>(pack_input, c);
cards.at(iterator) = c;
++iterator;
}
}
// REQUIRES: cards remain in the Pack
// EFFECTS: Returns the next card in the pack and increments the next index
Card Pack::deal_one() {
Card c = cards[next];
next++;
return c;
}
// EFFECTS: Resets next index to first card in the Pack
void Pack::reset() {
next = 0;
}
// EFFECTS: Shuffles the Pack and resets the next index. This
// performs an in shuffle seven times. See
// https://en.wikipedia.org/wiki/In_shuffle.
void Pack::shuffle() {
reset();
int shuffle_count(0);
std::array<Card, (PACK_SIZE/2)> first_half;
std::array<Card, (PACK_SIZE/2)> second_half;
while (shuffle_count < 7) {
for (int i(0); i < 12; ++i) { // fix me
first_half.at(i) = cards.at(i);
}
for (int i(12); i < 24; ++i) { // fix me
second_half.at(i-12) = cards.at(i);
}
for (int i(0); i < 12; ++i) { // fix me
cards.at(2*i) = second_half.at(i);
cards.at(1+(2*i)) = first_half.at(i);
}
++shuffle_count;
}
}
// EFFECTS: returns true if there are no more cards left in the pack
bool Pack::empty() const {
return (next >= 52);
}