-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
341 lines (276 loc) · 11.8 KB
/
main.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
339
340
341
// TOOD: consider creating dealer class
// TODO: test everything EXTENSIVELY!!!
// TODO: fix all for loops with auto
// IO
#include <iostream>
#include <iomanip>
// Runtime time calculation
#include <chrono>
// Game
#include <random>
// Classes
#include <deck.hpp>
#include <hand.hpp>
#include <shoe.hpp>
#include <player.hpp>
#include <strategy.hpp>
// Program Settings
#include <settings.hpp>
int main() {
auto start = std::chrono::steady_clock::now();
// Shoe for simulation
Shoe shoe = Shoe(MAX_DECKS);
// Total amount of cards in one deck -- (almost) always constant
const int ONE_DECK_SIZE = shoe.one_deck.size();
// Amount of cards to stop dealing when reached
const int PENETRATION_CARDS = ONE_DECK_SIZE * MAX_DECKS * PENETRATON_PERCENT;
// Statistic variables
int num_rounds = 0;
// num_hands includes hands created from splits; num_rounds does not
int num_hands = 0;
int num_wins = 0;
int num_losses = 0;
int num_draws = 0;
int num_player_blackjacks = 0;
int num_dealer_blackjacks = 0;
// Total player profit
// TODO: replace with bankroll, bet spread, true count, etc.
float player_profit = 0;
// Simulation loop
while(num_rounds < MAX_HANDS) {
if(shoe.size() <= PENETRATION_CARDS) shoe = Shoe(MAX_DECKS);
// Player variables
Player player = Player();
Player dealer = Player();
// Populate initial hands
player.add(shoe);
dealer.add(shoe);
player.add(shoe);
dealer.add(shoe);
// Useful dealer constants
const int dealer_upcard = dealer.at(0).at(0);
const int dealer_init_value = dealer.get_value();
Hand* p_dealer_hand = &dealer.hands.at(0);
// Has the dealer gotten a blackjack
bool dealer_blackjack = false;
if(DEBUG) std::cout << "--PLAYER LOOP--" << "\n";
// main round loop
while(true) {
// init current index
int current_index = player.get_first_active_index();
// exit loop if no active hands
if(current_index == -1) break;
// note: player.hands.at() instead of player.at() because player.at() does not return const
Hand* p_current_hand = &player.hands.at(current_index);
// if card was split aces, only allow one more card before de-activating
if(p_current_hand->get_split_aces_final_card()) {
player.add(shoe, current_index);
p_current_hand->set_split_aces_final_card(false);
p_current_hand->set_active(false);
continue;
}
// populate hand if necessary
while(p_current_hand->size() < 2) player.add(shoe, current_index);
int player_value = player.get_value(current_index);
if(DEBUG) {
std::cout << "Dealer: " << dealer_upcard << "\n";
std::cout << "Player: " << player_value << " -> " << player << "\n";
}
// handle blackjacks
if(dealer_init_value == 21) {
dealer_blackjack = true;
break;
}
if(player_value == 21) {
p_current_hand->set_active(false);
continue;
}
// handle bust
if(player_value > 21) {
p_current_hand->set_active(false);
continue;
}
// action player_action = dealer_strategy(*p_current_hand);
action player_action = basic_strategy(*p_current_hand, dealer_upcard);
// TODO: consider wrapping in a function
switch (player_action) {
case action::hit:
if(DEBUG) std::cout << "HIT! " << player << " -> ";
player.hit(shoe, current_index);
if(DEBUG) std::cout << player << "\n";
break;
case action::stand:
if(DEBUG) std::cout << "STAND! " << player << " -> ";
player.stand(current_index);
if(DEBUG) std::cout << player << "\n";
break;
case action::double_down:
if(DEBUG) std::cout << "DOUBLE! " << player << " -> ";
player.double_down(shoe, current_index);
if(DEBUG) std::cout << player << "\n";
break;
case action::split:
if(DEBUG) std::cout << "SPLIT! " << player << " -> ";
player.split(current_index);
if(DEBUG) std::cout << player << "\n";
// reset pointer.. shouldn't need to be here, but here in case
p_current_hand = &player.hands.at(current_index);
num_rounds--;
break;
case action::surrender:
if(DEBUG) std::cout << "SURRENDER! " << player << " -> ";
player.surrender(current_index);
if(DEBUG) std::cout << player << "\n";
break;
default:
if(DEBUG) std::cout << "ERROR! " << player << " -> ";
if(DEBUG) std::cout << player << "\n";
break;
}
}
if(DEBUG) std::cout << "--PLAYER LOOP END--" << "\n\n";
if(DEBUG) std::cout << "--DEALER LOOP--" << "\n";
if(DEBUG) std::cout << dealer.get_value() << " -> " << dealer << "\n";
// dealer hitting loop
bool all_busted = player.all_busted();
bool all_blackjacks = player.all_blackjacks();
while(true) {
// exit loop if all players have busted or all players have blackjacks
if(all_busted || all_blackjacks) break;
action dealer_action = dealer_strategy(*p_dealer_hand);
// exit loop if dealer stands
if(dealer_action == action::stand) break;
dealer.hit(shoe);
// exit loop if dealer busts
if(p_dealer_hand->is_bust()) break;
if(DEBUG) std::cout << dealer.get_value() << " -> " << dealer << "\n";
}
if(DEBUG) std::cout << dealer.get_value() << " -> " << dealer << "\n";
if(DEBUG) std::cout << "--DEALER LOOP END--" << "\n\n";
if(DEBUG) std::cout << "--WINNING LOOP--" << "\n";
int dealer_final_value = dealer.get_value();
// player winning/losing loop
for(Hand hand : player) {
num_rounds++;
num_hands++;
Hand* p_hand = &hand;
int value = p_hand->get_value();
bool player_blackjack = p_hand->is_blackjack();
bool player_bust = p_hand->is_bust();
// define magnitude of money based on double/surrender
float double_multiplier = p_hand->get_was_doubled() ? 2 : 1;
float surrender_multiplier = p_hand->get_was_surrendered() ? 0.5 : 1;
float money_magnitude = 1*double_multiplier*surrender_multiplier;
// std::cout<<"here14"<<"\n";
// std::cout<<*p_hand<<"\n";
// winner logic
// push
if(dealer_blackjack && player_blackjack) {
num_draws++;
num_dealer_blackjacks++;
num_player_blackjacks++;
if(DEBUG) std::cout << "TWO BLACKJACKS" << "\n";
continue;
}
// num_losses
if(dealer_blackjack) {
num_losses++;
num_dealer_blackjacks++;
player_profit -= money_magnitude;
if(DEBUG) std::cout << "DEALER BLACKJACK" << "\n";
if(DEBUG) std::cout << "-" << money_magnitude << "\n";
continue;
}
// num_wins
if(player_blackjack) {
num_wins++;
num_player_blackjacks++;
player_profit += money_magnitude*BLACKJACK_PAYOUT;
if(DEBUG) std::cout << "PLAYER BLACKJACK" << "\n";
if(DEBUG) std::cout << money_magnitude*BLACKJACK_PAYOUT << "\n";
continue;
}
// num_losses
if(player_bust) {
num_losses++;
player_profit -= money_magnitude;
if(DEBUG) std::cout << "PLAYER BUST" << "\n";
if(DEBUG) std::cout << "-" << money_magnitude << "\n";
continue;
}
// num_wins
if(dealer.at(0).is_bust()) {
num_wins++;
player_profit += money_magnitude;
if(DEBUG) std::cout << "DEALER BUST" << "\n";
if(DEBUG) std::cout << money_magnitude << "\n";
continue;
}
// push
if(dealer_final_value == value) {
num_draws++;
if(DEBUG) std::cout << "STANDARD PUSH" << "\n";
continue;
}
// num_wins
if(dealer_final_value < value) {
num_wins++;
player_profit += money_magnitude;
if(DEBUG) std::cout << "STANDARD WIN" << "\n";
if(DEBUG) std::cout << money_magnitude << "\n";
continue;
}
// num_losses
if(dealer_final_value > value) {
num_losses++;
player_profit -= money_magnitude;
if(DEBUG) std::cout << "STANDARD LOSS" << "\n";
if(DEBUG) std::cout << "-" << money_magnitude << "\n";
continue;
}
// should never get here
}
if(DEBUG) std::cout << "--WINNING LOOP END--" << "\n\n";
}
if(PRINT_STATS) {
// calculate program time early to avoid time from printing (even though it is negligable)
auto end = std::chrono::steady_clock::now();
auto diff = end - start;
// prevent scientific notation in printout
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << "TOTAL = " << num_hands << "\n\n";
std::cout << "WINS = " << num_wins << " -> " << num_wins*1.0/num_hands * 100 << "%\n";
std::cout << "LOSS = " << num_losses << " -> " << num_losses*1.0/num_hands * 100 << "%\n";
std::cout << "DRAWS = " << num_draws << " -> " << num_draws*1.0/num_hands * 100 << "%\n\n";
std::cout << "DEALER BJs = " << num_dealer_blackjacks << " -> " << num_dealer_blackjacks*1.0/num_hands * 100 << "%\n";
std::cout << "PLAYER BJs = " << num_player_blackjacks << " -> " << num_player_blackjacks*1.0/num_hands * 100 << "%\n\n";
float profit = BETTING_UNIT * player_profit;
float profit_per_hour = (profit) / (num_hands*1.0 / HANDS_PER_HOUR);
// define sign chars to print dollar sign prettier
// e.g., -$400 instead of $-400
char profit_sign;
if(profit < 0) {
// if negative, turn positive and assign negative sign
profit *= -1;
profit_sign = '-';
} else {
// if non-negative, assign no sign
profit_sign = '\0';
}
char profit_per_hour_sign;
if(profit_per_hour < 0) {
// if negative, turn positive and assign negative sign
profit_per_hour *= -1;
profit_per_hour_sign = '-';
} else {
// if non-negative, assign no sign
profit_per_hour_sign = '\0';
}
std::cout << "PLAYER PROFIT:\n";
std::cout << "-> " << player_profit << " betting units\n";
std::cout << "-> " << profit_sign << "$" << profit << "\n";
std::cout << "-> " << profit_per_hour_sign << "$" << profit_per_hour << "/hr\n\n";
std::cout << std::chrono::duration<double, std::milli>(diff).count() *1.0 / 1000 << " seconds" << std::endl;
}
}