-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeck.cpp
More file actions
96 lines (52 loc) · 1.64 KB
/
Deck.cpp
File metadata and controls
96 lines (52 loc) · 1.64 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
#include "Deck.h"
#include "Card.h"
#include "Stack.h"
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
Deck::Deck() {
int index = 0;
for (int rank = 0; rank < 13; rank++) {
for ( int suit = 0; suit < 4; suit++) {
deck->push(Card(rank, suit)); //the "->" means to access a function from the class type that the pointer is storing an address for (this line is the same as a deck object x, then using x.push(...))
}
}
index++;
}
void Deck::shuffle() {
Card temp[52];
for (int i=0; i<52; i++){
temp[i] = deck->pop();
}
/*
while (!deck->isFull()){
int i = rand()%52;
if (temp[i] == NULL) continue;
else {
deck->push(temp[i]);
temp[i] = NULL;
}
}
}
*/
int i1, i2;
srand(time(0));
for (int i=0; i<1000; i++){ //interchanges cards 1000 times per shuffle
i1 = rand()%52; //pick 2 random indexes to swap with one another in this temp card array
i2 = rand()%52;
Card store = temp[i1]; //stores a temp copy of the card at index i1 of the temp card array for proper completion of the swap
temp[i1] = temp[i2];
temp[i2] = store;
}
for (int i=0; i<52; i++) deck->push(temp[i]);
}
int Deck::getCount() return deck->getCount();
Card Deck::deal() return deck->pop();
ostream& operator <<(ostream& out, const Deck& in){ //codes for how to output a deck called "in" with operator << using variable "out"
Stack* copy = in.deck;
for (int i = 0; i<copy.getCount(); i++){
out << copy.deck[i] << endl;
}
return out;
}