-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
338 lines (268 loc) · 6.9 KB
/
Player.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* AUTHOR: Mohib Ahmed
* CSCI 235 Summer 2023 Spades Project
* Player.cpp
*/
#include "Player.h"
#include <iostream>
#include <algorithm>
#include <limits>
class Human : public Player
{
public:
Human(std::string name);
const std::string& get_name() const override;
//const std::string& get_strategy() const override;
void set_bid() override;
int get_bid() const override;
bool add_trick() override;
int get_trick() const override;
bool nil_bid() const override;
bool blind_nil_bid() const override;
bool add_card(const Card& c) override;
Card lead_card(bool broken_spade) override;
Card play_card(Suit led_suit) override;
void reset()override;
private:
std::vector<Card> hand;
std::string name;
int trick;
int bid;
/* Gets all the playable cards when leading a trick
* @post The playable cards when leading a trick are cards that are not
* Spades. If there are no cards that are not Spades, then the entire
* hand is playable.
* @return vector of playable Cards when leading a trick
*/
std::vector<Card> get_playable_hand() const;
/* Gets all the playable cards during a trick
* @post The playable cards are those of the lead Suit. If there are no
* Cards of the lead suit the then entire hand is playable
* @param lead Suit of the trick
* @return vector of playable Cards during the trick
*/
std::vector<Card> get_playable_hand(Suit lead) const;
/* Prints out the Player's playable_hand
* @post The playable hand is defined as the parameter rather than the
* hand data member since the concept of a playable hand changes
* depending on the state of the game. The format for print a card in the
* hand should be `[[card_index + 1]] [rank] of [suit]\n`. So for example,
* "[1] Two of Clubs\n[2] Three of Diamonds\n[3] Ace of Spades\n". Notice
* that there is the `[]` surround the index in the example. It is needed.
* @param playable_hand
*/
void print_hand(const std::vector<Card>& playable_hand) const;
/* Asks the user to select the card they want to play
* @param playable_hand The hand the user is able to use
* @return the index of the card the user selected from the the playable_hand
*/
int ask_for_card_index(const std::vector<Card>& playable_hand) const;
/* Remove the card from the hand
* @pre
* @post removes the card from the hand and makes sure that the vector is still sorted
* @param card to remove from the hand
*/
void remove_card(Card card);
};
Human::Human(std::string name) : name(name), trick(0), bid(0)
{
hand.reserve(MAX_HAND_SIZE);
}
const std::string& Human::get_name() const
{
return name;
}
//const std::string& Human::get_strategy() const
//{
// return "Human";
//}
void Human::set_bid()
{
char is_blind_nil;
std::cout << "Does Player " << name << " want to bid blind nil? [y/n]\n";
while (!(std::cin >> is_blind_nil) ||
(is_blind_nil != 'y' && is_blind_nil != 'n'))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Please answer using only `y` for yes or `n` for no.\n";
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (is_blind_nil == 'y')
{
bid = -2;
return;
}
print_hand(hand);
std::cout << "How much would you like to bid (0 to 13 inclusive)?\n";
int input_bid;
while (!(std::cin >> input_bid) ||
0 > input_bid || input_bid > MAX_HAND_SIZE)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Please answer with values from 0 to 13 inclusive.\n";
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
bid = input_bid == 0 ? -1 : input_bid;
}
int Human::get_bid() const
{
return bid <= 0 ? 0 : bid;
}
bool Human::add_trick()
{
bool is_add_successful = trick < MAX_HAND_SIZE;
if (is_add_successful)
{
++trick;
}
return is_add_successful;
}
int Human::get_trick() const
{
return trick;
}
bool Human::nil_bid() const
{
return bid == -1;
}
bool Human::blind_nil_bid() const
{
return bid == -2;
}
bool Human::add_card(const Card& c)
{
bool is_add_successful = hand.size() < MAX_HAND_SIZE;
if (is_add_successful)
{
hand.push_back(c);
for (int i = static_cast<int>(hand.size()) - 1; i > 0 && hand[i] < hand[i - 1]; --i)
{
std::swap(hand[i], hand[i - 1]);
}
}
return is_add_successful;
}
Card Human::lead_card(bool broken_spade)
{
if (hand.empty())
{
throw std::out_of_range("Player has no more cards to play with.");
}
std::vector<Card> playable_hand = broken_spade ? hand : get_playable_hand();
print_hand(playable_hand);
int card_index = ask_for_card_index(playable_hand);
Card card = playable_hand[card_index];
remove_card(card);
return card;
}
Card Human::play_card(Suit led_suit)
{
if (hand.empty())
{
throw std::out_of_range("Player has no more cards to play with");
}
std::vector<Card> playable_hand = get_playable_hand(led_suit);
print_hand(playable_hand);
int card_index = ask_for_card_index(playable_hand);
Card card = playable_hand[card_index];
remove_card(card);
return card;
}
void Human::reset()
{
hand.clear();
bid = 0;
trick = 0;
}
std::vector<Card> Human::get_playable_hand() const
{
std::vector<Card> playable_hand;
playable_hand.reserve(MAX_HAND_SIZE);
for (size_t i = 0; i < hand.size() && hand[i].get_suit() != Suit::SPADES; ++i)
{
playable_hand.push_back(hand[i]);
}
if (playable_hand.empty())
{
return hand;
}
return playable_hand;
}
std::vector<Card> Human::get_playable_hand(Suit lead) const
{
std::vector<Card> playable_hand;
playable_hand.reserve(MAX_HAND_SIZE);
for (size_t i = 0; i < hand.size(); ++i)
{
if (hand[i].get_suit() == lead)
{
playable_hand.push_back(hand[i]);
}
}
if (playable_hand.empty())
{
return hand;
}
return playable_hand;
}
void Human::print_hand(const std::vector<Card>& playable_hand) const
{
for (size_t i = 0; i < playable_hand.size(); ++i)
{
std::cout << '[' << i + 1 << ']' << ' ' << playable_hand[i] << '\n';
}
}
int Human::ask_for_card_index(const std::vector<Card>& playable_hand) const
{
size_t card_index = 0;
std::cout << "Please enter a number between 1 and "
<< playable_hand.size() << " inclusive:\n";
while (!(std::cin >> card_index) ||
card_index <= 0 || card_index > playable_hand.size())
{
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "Please enter a number between 1 and "
<< playable_hand.size() << " inclusive:\n";
}
return card_index - 1;
}
void Human::remove_card(Card card)
{
int card_index = -1;
for (size_t i = 0; i < hand.size(); ++i)
{
if (hand[i] == card)
{
card_index = i;
break;
}
}
if (card_index == -1)
{
return;
}
for (size_t i = card_index; i < hand.size() - 1; ++i)
{
std::swap(hand[i], hand[i + 1]);
}
hand.pop_back();
}
Player* Player_factory(const std::string& name, const std::string& strategy)
{
if (strategy == "Human")
{
return new Human(name);
}
else
{
return nullptr;
}
}
std::ostream& operator<<(std::ostream& os, const Player& p)
{
os << p.get_name();
return os;
}