-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cc
153 lines (126 loc) · 3.77 KB
/
player.cc
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
#include "player.h"
Player::Player(int playerNum, int score): playerNum{playerNum}, score{score} {}
Player::Player(int playerNum, int score, std::vector<std::shared_ptr<Card>> hand,
std::vector<std::shared_ptr<Card>> allLegalPlays, std::vector<std::shared_ptr<Card>> discards)
: playerNum{playerNum}, score{score}, hand{hand}, allLegalPlays{allLegalPlays},
discards{discards} {}
Player::~Player() {}
int Player::getScore() const {
return score;
}
void Player::setScore(int s) {
score = s;
}
int Player::getPlayerNum() const {
return playerNum;
}
bool Player::has7S() const {
for (unsigned int i = 0; i < hand.size(); ++i) {
if (hand[i]->getSuit() == SPADE && hand[i]->getRank() == SEVEN) {
return true;
}
}
return false;
}
// Clears all cards data on the player
void Player::clearCards() {
discards.clear();
hand.clear();
allLegalPlays.clear();
}
// Functions for the Hand
void Player::addHandCard(std::shared_ptr<Card> c) {
hand.push_back(c);
}
void Player::removeHandCard(int index) {
hand.erase(hand.begin() + index);
}
std::shared_ptr<Card> Player::findHandCard(int index) {
return hand[index];
}
std::vector<std::shared_ptr<Card>> Player::getHand() {
return hand;
}
int Player::getHandCardIndex(std::string s) const {
for (unsigned int i = 0; i < hand.size(); ++i) {
if (s == hand[i]->getInfo()) {
return i;
}
}
return -1;
}
bool Player::emptyHand() const {
return hand.size() == 0;
}
std::string Player::getHandInfo() const {
std::string str = "Your hand: ";
for (unsigned int i = 0; i < hand.size(); ++i) {
str += hand[i]->getInfo() + " ";
}
return str;
}
// Functions for Legal Plays
void Player::findAllLegalPlays(std::shared_ptr<Table> & table) {
// Clear previous allLegalPlays
allLegalPlays.clear();
// Determines current allLegalPlays
for (unsigned int i = 0; i < hand.size(); ++i) {
if (table->checkLegalMove(hand[i])) {
allLegalPlays.emplace_back(hand[i]);
}
}
}
std::shared_ptr<Card> Player::findLegalCard(int index) {
return allLegalPlays[index];
}
std::vector<std::shared_ptr<Card>> Player::getAllLegalPlays() {
return allLegalPlays;
}
int Player::getAllLegalPlaysIndex(std::string s) const {
for (unsigned int i = 0; i < allLegalPlays.size(); ++i) {
if (s == allLegalPlays[i]->getInfo()) {
return i;
}
}
return -1;
}
std::string Player::getLegalInfo() const{
std::string str = "Legal plays: ";
for (unsigned int i = 0; i < allLegalPlays.size(); ++i) {
str += allLegalPlays[i]->getInfo() + " ";
}
return str;
}
bool Player::noLegalPlays() const {
return allLegalPlays.size() == 0;
}
// Functions for Discards
void Player::addDiscard(std::shared_ptr<Card> c) {
discards.push_back(c);
}
std::vector<std::shared_ptr<Card>> Player::getDiscards() {
return discards;
}
// For testing only
int Player::getDiscardIndex(std::string s) const {
for (unsigned int i = 0; i < discards.size(); ++i) {
if (s == discards[i]->getInfo()) {
return i;
}
}
return -1;
}
int Player::sumDiscard() const {
int roundScore = 0;
for (unsigned int i = 0; i < discards.size(); ++i) {
roundScore += static_cast<int>(discards[i]->getRank()) + 1;
}
return roundScore;
}
std::string Player::getDiscardInfo() const {
std::string str = "Player" + to_string(playerNum) + "’s discards: ";
for (unsigned int i = 0; i < discards.size(); ++i) {
str += discards[i]->getInfo() + " ";
}
return str;
}