-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHand.cpp
176 lines (155 loc) · 2.91 KB
/
Hand.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
#include "stdafx.h"
#include "Card.h"
#include "Hand.h"
#include <cctype>
//const int Hand::(int)_cards.size() = 5;
Hand::Hand()
{
}
Hand::Hand(const Card * cards, int size)
{
for(int i = 0; i < size; i++){
_cards.push_back(cards[i]);
}
sortCards();
}
Hand::Hand(const Hand & hand)
{
copy(hand);
}
Hand::~Hand()
{
//vectors are automatically deallocated
}
//higher rank cards to the right,
//higher suit cards to the right
//just use a simple bubble sort
void Hand::sortCards()
{
bool doneSorting = false;
while(!doneSorting)
{
Card temp;
doneSorting = true;
for(int i = 1; i < (int)_cards.size(); i++){
if(_cards[i - 1] > _cards[i]){
doneSorting = false;
temp = _cards[i - 1];
_cards[i - 1] = _cards[i];
_cards[i] = temp;
}
}
}
}
bool Hand::operator<(const Hand & hand) const
{
//compare the right cards first
//since we sort in increasing order
for(int i = (int)_cards.size() - 1; i >= 0; i--){
Card c1 = _cards[i];
Card c2 = hand._cards[i];
if(c1.get_erank() < c2.get_erank()){
return true;
}
else if(c1.get_erank() > c2.get_erank()){
return false;
}
}
//same hand
return false;
}
bool Hand::operator>(const Hand & hand) const
{
//compare the right cards first
//since we sort in increasing order
for(int i = (int)_cards.size() - 1; i >= 0; i--){
Card c1 = _cards[i];
Card c2 = hand._cards[i];
if(c1.get_erank() > c2.get_erank()){
return true;
}
else if(c1.get_erank() < c2.get_erank()){
return false;
}
}
//same hand
return false;
}
Hand & Hand::operator=(const Hand otherHand)
{
copy(otherHand);
return *this;
}
bool Hand::operator==(const Hand &otherHand) const
{
for(int i = 0; i < (int)_cards.size(); i++){
if(!(_cards[i] == otherHand._cards[i])){
return false;
}
}
return true;
}
const Card & Hand::operator[] (size_t position){
if ((position > _cards.size()) || (position < 0)){
throw "Invalid_Index";
}
else{
return _cards[position];
}
}
void Hand::insert(Card card){
_cards.push_back(card);
sortCards();
}
void Hand::remove_card (size_t position){
if ((position > _cards.size()) || (position < 0)){
throw "Invalid_Index";
}
else{
_cards.erase(_cards.begin()+ position);
}
}
Card Hand::get_card(int index) const
{
return _cards[index];
}
vector<Card> Hand::get_cards() const
{
return _cards;
}
bool Hand::copy(const Hand & hand)
{
if(this == &hand){
cout << "same as me!" << endl;
return false;
}
else{
_cards.clear();
for(int i = 0; i < (int)hand._cards.size(); i++){
_cards.push_back(hand._cards[i]);
}
return true;
}
}
int Hand::size() const
{
return (int)_cards.size();
}
void Hand::print() const
{
cout << toString() << endl;
}
ostream & operator<<(ostream & out, const Hand & hand)
{
out << hand.toString();
return out;
}
string Hand::toString() const
{
string s = "Hand{\n";
for(int i = 0; i < (int)_cards.size(); i++){
s = s + _cards[i].toString() + "\n";
}
s = s + "}";
return s;
}