-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.h
162 lines (134 loc) · 3.61 KB
/
Player.h
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
//
// Created by Kai Blahnik on 7/6/22.
//
#ifndef POKER_PLAYER_H
#define POKER_PLAYER_H
#include <vector>
#include <string>
#include <stdexcept>
#include "Deck.h"
#include "Card.h"
#include "Input.h"
using std::vector, std::string, std::cout;
const int STARTING_MONEY = 1000;
class Player {
public:
explicit Player(string n) {
name = std::move(n);
}
void addCard(const Card & card) {
hand.push_back(card);
}
vector<Card> discardHand() {
auto temp = hand;
hand.clear();
return temp;
}
int getHandValue() {
int total = 0;
for(const Card card: hand) {
total += card.getValue();
}
if(total > 21) {
int ind = findHighAce();
if(ind != -1) {
hand[ind].setHigh(false);
return getHandValue();
}
}
return total;
}
int findHighAce() const {
for(int i = 0; i < hand.size(); ++i)
if(isHighAce(hand[i]))
return i;
return -1;
}
int compare(Player & other) {
if(other.getHandValue() > 21)
return 1;
if(getHandValue() > 21)
return -1;
if(getHandValue() > other.getHandValue())
return 1;
if(getHandValue() < other.getHandValue())
return -1;
return 0;
}
virtual int turnDecision() {
enterContinue();
if(getHandValue() < 17) return 1;
else return 0;
}
friend std::ostream & operator<<(std::ostream & strm, Player & p) {
string value = std::to_string(p.getHandValue());
if(p.hole)
value = "Unknown";
strm << p.name << ": " << value;
for(int i = 0; i < p.hand.size(); ++i) {
if(p.hole && i == 0)
strm << "\n\t" << "X HOLE CARD X";
else
strm << "\n\t" << p.hand[i];
}
return strm;
}
const string & getName() const {return name;}
void setHole(bool h) {hole = h;}
private:
bool hole{};
string name;
vector<Card> hand;
};
class Bettor : public Player {
public:
explicit Bettor(string n) : Player(std::move(n)) {
money = STARTING_MONEY;
bet = -1;
};
virtual void betDecision() {
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr(1, money);
bet = distr(gen);
}
int getBet() const {return bet;}
int getMoney() const {return money;}
void setMoney(int m) {money = m;}
protected:
int money;
int bet;
};
class Human : public Bettor {
public:
explicit Human(string n) : Bettor(std::move(n)) {};
void betDecision() override {
cout << "How much do you want to bet?\n";
string input = userInput();
int b = -1;
try {
b = std::stoi(input);
}
catch(std::invalid_argument & e) {
cout << "Your bet must be an integer\n";
return betDecision();
}
if(b > getMoney()) {
cout << "You cannot bet more than your current money\n";
return betDecision();
}
if(b <= 0) {
cout << "Your bet must be a positive integer\n";
return betDecision();
}
bet = b;
}
int turnDecision() override {
cout << "Hit or stand? Type all lowercase\n";
string input = userInput();
if(input == "stand") return 0;
if(input == "hit") return 1;
else return turnDecision();
}
};
#endif //POKER_PLAYER_H