forked from Mordequess/CS247_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.cpp
57 lines (47 loc) · 1019 Bytes
/
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
#include <sstream>
#include <cstdlib>
#include <stdlib.h>
#include "Card.h"
#include "Deck.h"
Deck::Deck() : seed_(0) {
newDeck();
}
Deck::~Deck(){}
Deck::Deck(int seed) : seed_(seed) {
newDeck();
}
Card* Deck::getArray(int index) const{
return cards_[index];
}
void Deck::setArray(int index, Card* newCard){
cards_[index] = newCard;
}
//shuffle deck using seed (default 0)
void Deck::shuffle(){
int n = 52;
while ( n > 1 ) {
int k = (int) (lrand48() % n); //seed_
--n;
Card *c = cards_[n];
cards_[n] = cards_[k];
cards_[k] = c;
}
}
//fills an empty deck with all cards in sorted order
void Deck::newDeck(){
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 13; i++) {
cards_[i + j*13] = (new Card(static_cast<Suit>(j), static_cast<Rank>(i)));
}
}
}
//prints deck in current order
std::ostream &operator<<(std::ostream& out, const Deck& d){
for (int i = 0; i < 4; i++){
for (int j = 0; j < 13; j++) {
out << d.getArray(j+i*13) << " ";
}
out << std::endl;
}
return out;
}