-
Notifications
You must be signed in to change notification settings - Fork 3
/
cards.cpp
122 lines (111 loc) · 3.14 KB
/
cards.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//Author: PlanckBit
//MIT License
//Copyright (c) 2019 PlanckBit
#include <iostream>
#include <stdlib.h>
#include "cards.h"
//Card suit and rank
playingCard::playingCard(cardSuit s, cardRank r) {
suit=s;
rank=r;
}
cardRank playingCard::theRank() const {
return rank;
}
cardSuit playingCard::theSuit() const {
return suit;
}
//Will print out one card at a time
void playingCard::print() const {
switch(rank) {
case 0:
std::cout << "2"; break;
case 1:
std::cout << "3"; break;
case 2:
std::cout << "4"; break;
case 3:
std::cout << "5"; break;
case 4:
std::cout << "6"; break;
case 5:
std::cout << "7"; break;
case 6:
std::cout << "8"; break;
case 7:
std::cout << "9"; break;
case 8:
std::cout << "T"; break;
case 9:
std::cout << "J"; break;
case 10:
std::cout << "Q"; break;
case 11:
std::cout << "K"; break;
case 12:
std::cout << "A"; break;
}
switch(suit) {
case 0:
std::cout << "C" <<" "; break;
case 1:
std::cout << "D" <<" "; break;
case 2:
std::cout << "H" <<" "; break;
case 3:
std::cout << "S" <<" "; break;
}
}
deckOfCards::deckOfCards() {
cardsInDeck=52;
}
//This will let the user inspect whether the deck is empty or not
int deckOfCards::deckEmpty() const {
if (cardsInDeck <= 2)
//the deck is empty, this tells the user by returning true
return 1;
else
//the deck is not empty, returns false
return 0;
}
//All elements are set to 1, meaning all cards in deck are now playable
// user will call this function when deckEmpty returns true;
void deckOfCards::shuffle() {
for(int i=0; i<=51; i++) {
deck[i]=1;
}
cardsInDeck=52;
}
//The user will call this function to get a card
playingCard deckOfCards::deal() {
int c; //used for switch statement
cardSuit s; //this is used to determine suit
cardRank r; //this is used to determine rank of that suit
const int divisor=13;
int card_dealt=(rand() % (52));
if (deck[card_dealt]==1) {
//card found, determine the suit and rank
c=(card_dealt/divisor);
r=(card_dealt%divisor);
//remove card from deck, by setting this element to zero
deck[card_dealt]=0;
//decrement the number of cards in the deck
--cardsInDeck;
switch(c) {
case 0:
s=CLUBS; break;
case 1:
s=DIAMONDS; break;
case 2:
s=HEARTS; break;
case 3:
s=SPADES; break;
}
}
else {
//The card was not in the deck so get another card recurrsively
return deckOfCards::deal();
}
playingCard cards(s,r);
return cards;
}