-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.cpp
177 lines (161 loc) · 3.42 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/// File: card.cpp
/// This class is responsible for creating and managing Cards and outputting them.
///
#include "card.h"
#include <iostream>
using namespace std;
const int RANK_INDEX = 0;
const int SUIT_INDEX = 1;
/// No argument constructor.
Card::Card() {
rank = TWO;
suit = CLUBS;
}
/// Constructor that sets the rank and suit for a Card object.
Card::Card(Rank rank, Suit suit) {
this->rank = rank;
this->suit = suit;
}
/// Constructor that sets the rank and suit for a Card object from an input string.
Card::Card(string input) {
char rankString = input.at(RANK_INDEX);
char suitString = input.at(SUIT_INDEX);
switch(rankString) {
case '2':
rank = TWO;
break;
case '3':
rank = THREE;
break;
case '4':
rank = FOUR;
break;
case '5':
rank = FIVE;
break;
case '6':
rank = SIX;
break;
case '7':
rank = SEVEN;
break;
case '8':
rank = EIGHT;
break;
case '9':
rank = NINE;
break;
case 'T':
rank = TEN;
break;
case 'J':
rank = JACK;
break;
case 'Q':
rank = QUEEN;
break;
case 'K':
rank = KING;
break;
case 'A':
rank = ACE;
break;
}
switch(suitString) {
case 'C':
suit = CLUBS;
break;
case 'D':
suit = DIAMONDS;
break;
case 'H':
suit = HEARTS;
break;
case 'S':
suit = SPADES;
break;
}
}
/// Accessor for the Rank instance variable.
Rank Card::getRank() const {
return rank;
}
/// Accessor for the Suit instance variable.
Suit Card::getSuit() const {
return suit;
}
/// Overloads the function operator to provide a comparison which sets an ordering between two Card objects.
bool Card::operator()(Card* card1, Card* card2) {
if (card1->getRank() < card2->getRank()) {
return false;
}
return true;
}
/// Puts a string representation of this Card on the output stream.
ostream& operator<<(ostream& out, const Card& card) {
Rank cardRank = card.getRank();
Suit cardSuit = card.getSuit();
string rankString;
string suitString;
switch(cardRank) {
case TWO:
rankString = '2';
break;
case THREE:
rankString = '3';
break;
case FOUR:
rankString = '4';
break;
case FIVE:
rankString = '5';
break;
case SIX:
rankString = '6';
break;
case SEVEN:
rankString = '7';
break;
case EIGHT:
rankString = '8';
break;
case NINE:
rankString = '9';
break;
case TEN:
rankString = 'T';
break;
case JACK:
rankString = 'J';
break;
case QUEEN:
rankString = 'Q';
break;
case KING:
rankString = 'K';
break;
case ACE:
rankString = 'A';
break;
}
switch(cardSuit) {
case CLUBS:
suitString = 'C';
break;
case DIAMONDS:
suitString = 'D';
break;
case HEARTS:
suitString = 'H';
break;
case SPADES:
suitString = 'S';
break;
}
out << rankString << suitString;
return out;
}
/// Destructor – does nothing as no objects are created dynamically by the constructor of this class.
Card::~Card() {
//dtor
}