-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
156 lines (132 loc) · 3.78 KB
/
player.cpp
File metadata and controls
156 lines (132 loc) · 3.78 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
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
//
// Created by willworthington on 11/4/2018.
//
#include <istream>
#include <time.h>
#include <cstdlib>
#include "card.h"
#include "deck.h"
#include "player.h"
using namespace std;
//adds a card to the hand
void Player::addCard(Card c){
myHand.push_back(c);
}
void Player::bookCards(Card c1, Card c2){
myBook.push_back(c1);
myBook.push_back(c2);
}
//OPTIONAL
// comment out if you decide to not use it
//this function will check a players hand for a pair.
//If a pair is found, it returns true and populates the two variables with the cards that make the pair.
bool Player::checkHandForBook(Card &c1, Card &c2){
//Loops from back to front, so that it will check the most likely case first,
//which is that the newest card (at the end) matches a card earlier in the deck
for (int i=myHand.size()-1; i>=1; i--){
for (int j=i-1; j>=0; j--){
if(myHand[i].getRank()==myHand[j].getRank()){
c1=myHand[i];
c2=myHand[j];
return true;
}
}
}
return false;
}
//OPTIONAL
// comment out if you decide to not use it
//Does the player have a card with the same rank as c in her hand?
bool Player::rankInHand(Card c) const{
for (int i=0; i<myHand.size(); i++){
if (myHand[i].getRank()==c.getRank()) return true;
}
return false;
}
//uses some strategy to choose one card from the player's
//hand so they can say "Do you have a 4?"
Card Player::chooseCardFromHand() const{
//No strategy yet
//Strategize later, this is just a simple guesser
int length = myHand.size();
srand(time(NULL));
int index = rand() % length;
return myHand[index];
}
//Does the player have the card c in her hand?
bool Player::cardInHand(Card c) const{
for (int i=0; i<myHand.size(); i++){
if (myHand[i]==c) return true;
}
return false;
}
//Remove the card c (with matching rank) from the hand and return it to the caller
Card Player::removeCardFromHand(Card c){
Card tmp=c;
Card errorCard(0,Card::spades);
for (int i=0; i<myHand.size(); i++){
if (myHand[i]==c){
//tmp = myHand[i];
myHand.erase(myHand.begin()+i);
return tmp;
}
}
return errorCard;
}
//Remove the card c (with matching rank) from the hand and return it to the caller
Card Player::removeRankFromHand(Card c){
Card tmp;
Card errorCard(0,Card::spades);
for (int i=0; i<myHand.size(); i++){
if (myHand[i].getRank()==c.getRank()){
tmp = myHand[i];
myHand.erase(myHand.begin()+i);
return tmp;
}
}
return errorCard;
}
string Player::showHand() const{
string hand;
for (int i=0; i<myHand.size(); i++){
if (i!=myHand.size()-1){
hand += myHand[i].toString() + " ";
}
else{
hand += myHand[i].toString();
}
}
return hand;
}
string Player::showBooks() const{
string books;
for (int i=0; i<myBook.size(); i++){
if (i!=myBook.size()-1){
books += myBook[i].toString() + " ";
}
else{
books += myBook[i].toString();
}
}
return books;
}
int Player::getHandSize() const{
return myHand.size();
}
int Player::getBookSize() const{
return myBook.size();
}
//OPTIONAL
// comment out if you decide to not use it
//this function will check a players hand for a pair.
//If a pair is found, it returns true and populates the two variables with the cards tha make the pair.
/*
bool Player::checkHandForPair(Card &c1, Card &c2){
}*/
//OPTIONAL
// comment out if you decide to not use it
//Does the player have a card with the same rank as c in her hand?
//e.g. will return true if the player has a 7d and the parameter is 7c
/*
bool Player::sameRankInHand(Card c) const{
}*/