-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHand.cpp
More file actions
115 lines (101 loc) · 2.35 KB
/
Copy pathHand.cpp
File metadata and controls
115 lines (101 loc) · 2.35 KB
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
#include <iostream>
#include <string>
#include "Hand.h"
using namespace std;
void Hand::AddCard(Card* c)
{
_hand.push_back(c);
//Notify(this);
}
void Hand::RemoveCard(Card* c)
{
for (size_t i = 0; i < _hand.size(); i++)
{
if (_hand[i] == c)
{
_hand.erase(_hand.begin() + i);
break;
}
}
//Notify(this);
}
void Hand::ViewHand()
{
if (!IsEmpty())
{
for (size_t i = 0; i < _hand.size(); i++)
{
cout << "[Card " << i + 1 << "]: ";
_hand[i]->Print();
cout << endl;
}
}
else
{
cout << "You have no cards" << endl;
}
}
bool Hand::TradeIn(bool isComp)
{
vector<Card**> trades;
for (size_t i = 0; i < _hand.size() - 2; i++){
for (size_t j = i + 1; j < _hand.size() - 1; j++){
for (size_t k = j + 1; k < _hand.size(); k++){
if (_hand[i] == _hand[j] && _hand[j] == _hand[k])
{
Card* tmp[3] = { _hand[i], _hand[j], _hand[k] };
trades.push_back(tmp);
}
else if (_hand[i] != _hand[j] && _hand[i] != _hand[k] && _hand[j] != _hand[k])
{
Card* tmp[3] = { _hand[i], _hand[j], _hand[k] };
trades.push_back(tmp);
}
else if (_hand[i]->GetFaceValue() == Wild || _hand[j]->GetFaceValue() == Wild || _hand[k]->GetFaceValue() == Wild)
{
Card* tmp[3] = { _hand[i], _hand[j], _hand[k] };
trades.push_back(tmp);
}
}
}
}
if (trades.size() > 0){
if (isComp){
for (int i = 0; i < 3; i++){
for (size_t j = 0; j < _hand.size(); j++){
if (trades[0][i] == _hand[j])
_hand.erase(_hand.begin() + j);
}
}
return true;
}
else{
cout << "Choose one of the following sets of cards to discard: \n\n";
for (size_t i = 0; i < trades.size(); i++)
{
cout << "[" << trades[i][0]->GetFaceValue() << ", " << trades[i][1]->GetFaceValue() << ", " << trades[i][2]->GetFaceValue() << "]: " << i + 1 << endl;
}
string response;
do{
cin.ignore();
getline(cin, response);
} while (!isNumber(response) || stoi(response) > int(trades.size()));
for (int i = 0; i < 3; i++){
for (size_t j = 0; j < _hand.size(); j++){
if (trades[stoi(response)-1][i] == _hand[j])
_hand.erase(_hand.begin() + j);
}
}
return true;
}
}
else{
cout << "No valid trade options.\n";
return false;
}
}
bool Hand::isNumber(string s){
string::const_iterator it = s.begin();
while (it != s.end() && isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}