Repository navigation
Expand file tree
/
Copy pathPlayer.cpp
More file actions
158 lines (128 loc) · 3.13 KB
/
Copy pathPlayer.cpp
File metadata and controls
158 lines (128 loc) · 3.13 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
157
158
/*
Player.cpp
Definitions for Player
*/
#include "Card.h"
#include "AIRole.h"
#include "HumanRole.h"
using namespace std;
// Player constructor
Player::Player(int id, bool is_human, Game* game)
: Subject(game), role_(NULL), id_(id), score_(0), won_(false) {
setRole(is_human);
}
// Player destructor
Player::~Player() {
delete role_;
}
// sets whether the current player is a human or ai
void Player::setRole(bool is_human) {
// destructs the previous role if any
delete role_;
// constructs new role based on bool
if (is_human) {
role_ = new HumanRole(this);
}
else {
role_ = new AIRole(this);
}
}
// switches current player with a role of AI
void Player::rageQuit() {
// replaces human role with ai role
setRole(false);
}
// start a new round by giving the player a new set of cards
void Player::newRound(const CardList &new_cards) {
hand_ = new_cards;
// removes all the cards which were discarded
discards_.clear();
}
// handle the end of round for the player
void Player::endRound() {
// updates players score
updateScore();
// notifies gui round is over
notify();
}
// handles the end of the game
void Player::endGame() {
// clears hand and notifies gui of update
hand_.clear();
notify();
}
// find out if the player possesses the 7 of spades
bool Player::has7OfSpades() const {
return hand_.find(Card(SPADE, SEVEN)) != -1;
}
// tells Player object to take the cards in its discard pile
// and add them for points
void Player::updateScore() {
// sums up the discards
for (int i = 0; i < discards_.size(); i++) {
score_ += ((int)discards_[i]->getRank()) + 1;
}
}
// called when it is the players turn to play
void Player::notifyTurn(const CardList& table) {
// updates legal moves, and depending on if AI or Human either passes control to GUI or plays
updateLegalMoves(table);
role_->play();
}
// Plays the given card, called from GUI
void Player::playCard(const Card& card)
{
role_->playCard(card);
}
// Discards the given card, called from GUI
void Player::discardCard(const Card& card)
{
role_->discardCard(card);
}
// id get accessor
int Player::id() const {
return id_;
}
// score get accessor
int Player::score() const {
return score_;
}
// returns the number of discards - accessor
int Player::discardCount() const {
return discards_.size();
}
// hand get accessor
const CardList &Player::playerHand() const {
return hand_;
}
// updates the cards which are legally playable
void Player::updateLegalMoves(const CardList& table) {
legalMoves_ = GameLogic::legalMoves(table, hand_); // calls helper function to obtain legal cards
}
// accessor to the player's legal moves
const CardList& Player::legalMoves() const {
return legalMoves_;
}
// checks if there are any cards to play
bool Player::hasLegalMoves() const {
return legalMoves_.size() != 0;
}
// hint request
void Player::makeHintRequest() {
notify();
}
// score reset
void Player::resetScore() {
score_ = 0;
won_ = false;
}
// won mutator
void Player::won(bool newVal) {
won_ = newVal;
// notifies of change in winner status
notify();
}
// won accessor
bool Player::won() const {
return won_;
}