-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.cpp
126 lines (107 loc) · 2.91 KB
/
Card.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
122
123
124
125
126
// Jack Bennett 0370668//
#include "Card.h"
const std::string Card::RankName = "23456789TJQKA";
const std::string Card::SuitName = "SDCH";
const int Card::numRanks = RankName.length();
const int Card::numSuits = SuitName.length();
const int Card::numCards = Card::numRanks * Card::numSuits;
//const Card Card::TwoOfClubs (Card::Two, Card::Clubs);
//const Card Card::QueenOfSpades (Card::Queen, Card::Spades);
Card::Card() : rank(Two), suit(Spades) {}
Card::Card(Rank r, Suit s) : rank(r), suit(s) {}
Card::Card(std::string sCard){
if (sCard[0] == Card::RankName[0])
rank = Two;
else if (sCard[0] == Card::RankName[1])
rank = Three;
else if (sCard[0] == Card::RankName[2])
rank = Four;
else if (sCard[0] == Card::RankName[3])
rank = Five;
else if (sCard[0] == Card::RankName[4])
rank = Six;
else if (sCard[0] == Card::RankName[5])
rank = Seven;
else if (sCard[0] == Card::RankName[6])
rank = Eight;
else if (sCard[0] == Card::RankName[7])
rank = Nine;
else if (sCard[0] == Card::RankName[8])
rank = Ten;
else if (sCard[0] == Card::RankName[9])
rank = Jack;
else if (sCard[0] == Card::RankName[10])
rank = Queen;
else if (sCard[0] == Card::RankName[11])
rank = King;
else if (sCard[0] == Card::RankName[12])
rank = Ace;
if (sCard[1] == Card::SuitName[0])
suit = Spades;
else if (sCard[1] == Card::SuitName[1])
suit = Hearts;
else if (sCard[1] == Card::SuitName[2])
suit = Clubs;
else if (sCard[1] == Card::SuitName[3])
suit = Diamonds;
}
Card::~Card() {
rank = Two;
suit = Spades;
}
Card::Rank Card::getRank() const {
return rank;
}
Card::Suit Card::getSuit() const {
return suit;
}
int Card::getValueHeart() const{
if(Card::suit == Hearts){
return 1;
}
else if (Card::rank == Queen && Card::suit == Spades){
return 13;
}
else{
return 0;
}
}
bool Card::isValid(std::string s) {
bool sRank = false;
bool sSuit = false;
if (s.length() != 2) {
return false;
}
for(int i = 0; i < Card::numRanks; i++){
if(s[0] == Card::RankName[i]){
sRank = true;
break;
}
}
for (int i = 0; i < Card::numSuits; i++){
if(s[1] == Card::SuitName[i]){
sSuit = true;
break;
}
}
if (sRank && sSuit){
return true;
}
else{
return false;
}
}
// Overloaded operators: output
extern std::ostream& operator<< (std::ostream &out, const Card &c) {
std::cout << Card::RankName[c.getRank()] << Card::SuitName[c.getSuit()];
return out;
}
//overload operator for comparing cards
extern bool operator== (const Card& c1, const Card& c2){
if (c1.getRank() == c2.getRank() && c1.getSuit() == c2.getSuit()){
return true;
}
else{
return false;
}
}