-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
123 lines (95 loc) · 3 KB
/
Player.cpp
File metadata and controls
123 lines (95 loc) · 3 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
#include "Player.hpp"
#include "Card.hpp"
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
class SimplePlayer : public Player {
private:
std::string name;
std::vector<Card> hand;
public:
SimplePlayer(const std::string &name_in) : name(name_in){}
const std::string & get_name() const override;
void add_card(const Card &c) override;
Card play_card(const Card & upcard) override;
bool hand_empty() const override;
bool has_playable_cards(const Card & upcard) const override;
};
const std::string & SimplePlayer::get_name() const {
return name;
}
void SimplePlayer::add_card(const Card &c) {
hand.push_back(c);
}
Card SimplePlayer::play_card(const Card & upcard) {
return hand[0];
}
bool SimplePlayer::hand_empty() const {
return hand.empty();
}
bool SimplePlayer::has_playable_cards(const Card & upcard) const {
return !hand.empty();
}
class HumanPlayer : public Player {
private:
std::string name;
std::vector<Card> hand;
public:
HumanPlayer(const std::string &name_in) : name(name_in){}
const std::string & get_name() const override;
void add_card(const Card &c) override;
Card play_card(const Card & upcard) override;
bool hand_empty() const override;
bool has_playable_cards(const Card & upcard) const override;
void print_hand() const;
};
const std::string & HumanPlayer::get_name() const {
return name;
}
void HumanPlayer::add_card(const Card &c) {
hand.push_back(c);
std::sort(hand.begin(), hand.end());
}
Card HumanPlayer::play_card(const Card & upcard) {
int card_index(0); Card *chosen_one;
print_hand();
std::cout << "Human player " << name << ", please select a card:\n";
std::cin >> card_index;
chosen_one = &hand[card_index];
hand.erase(hand.begin() + card_index);
return *chosen_one;
}
bool HumanPlayer::hand_empty() const {
return hand.empty();
}
bool HumanPlayer::has_playable_cards(const Card & upcard) const {
return !hand.empty();
}
void HumanPlayer::print_hand() const {
for (size_t i=0; i < hand.size(); ++i)
std::cout << "Human player " << name << "'s hand: "
<< "[" << i << "] " << hand[i] << "\n";
}
//EFFECTS: Returns a pointer to a player with the given name and strategy
//To create an object that won't go out of scope when the function returns,
//use "return new Simple(name)" or "return new Human(name)"
//Don't forget to call "delete" on each Player* after the game is over
Player * Player_factory(const std::string &name, const std::string &strategy) {
if (strategy == "Simple") {
return new SimplePlayer(name);
}
else if (strategy == "Human") {
return new HumanPlayer(name);
}
// Invalid strategy if we get here
assert(false);
return nullptr;
}
std::ostream & operator<<(std::ostream &os, const Player &p) {
os << p.get_name();
return os;
}