-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.cpp
60 lines (47 loc) · 1.19 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
#include "Card.h"
#include <string>
#include <cassert>
using namespace std;
// constructor
Card::Card(Suit s, Rank r){
suit_ = s;
rank_ = r;
}
// gets suit
Suit Card::getSuit() const{
return suit_;
}
// gets rank
Rank Card::getRank() const{
return rank_;
}
//equalitiy
bool operator==(const Card &a, const Card &b){
return a.getSuit() == b.getSuit() && a.getRank() == b.getRank();
}
//streaming operators
ostream &operator<<(ostream &out, const Card &c){
string suits[SUIT_COUNT] = {"C", "D", "H", "S"};
string ranks[RANK_COUNT] = {"A", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "J", "Q", "K"};
out << ranks[c.getRank()] << suits[c.getSuit()];
return out;
}
istream &operator>>(istream &in, Card &c){
string suits = "CDHS", ranks = "A234567891JQK";
string str;
in >> str;
assert ( !in.fail() );
//Read in the rank, make sure it's valid
c.rank_ = (Rank)ranks.find( str.at(0) );
assert ( c.rank_ != string::npos );
//If it's a 10, make sure the 2nd character is a 0
if ( c.rank_ == TEN ){
assert(str.at(1) == '0');
str.at(1) = str.at(2);
}
//Read in the suit, make sure it's valid
c.suit_ = (Suit)suits.find( str.at(1) );
assert ( c.suit_ != string::npos );
return in;
}