-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.h
462 lines (417 loc) · 9.15 KB
/
blackjack.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#ifndef BLACKJACK_H
#define BLACKJACK_H
enum states
{
BUYIN,
BET,
DRAW,
DEALERCHECK,
PLAY,
INSURANCE,
NEXTPLAYER,
DEALERPLAY
};
#include <iostream>
#include <string>
//#include <strstream> //deprecated in mingw???
#include <cstdlib>
#include "shoe.h"
#include "hand.h"
#include "player.h"
#include "playerAI.h"
using namespace std;
class Blackjack
{
public:
Blackjack(int numDecks) : numPlayers(1), gameState(BUYIN), autoplay(false), numRoundsToPlay(1000000), roundsPlayedCounter(0), quit(false)
{
shoe = new Shoe(numDecks);
cutCard = 52*numDecks*0.75;
shoe->shuffle();
}
~Blackjack()
{
delete shoe;
}
void play()
{
while(!quit)
{
switch(gameState)
{
case BUYIN:
if(autoplay)
{
input = bot.playHand(NULL, 0, 0, 0, BUYIN);
}
else
{
status_output = "How much money do you wish to play with? $";
drawTable();
cin >> input;
}
num_input = atoi(input.c_str());
players[0].receiveWinnings(num_input);
gameState = BET;
break;
case BET:
if(autoplay)
{
input = bot.playHand(NULL, 0, 0, shoe->trueCount(), BET);
}
else
{
status_output = "Enter your bet, or press B to add chips. Press Q to quit: $";
drawTable();
cin >> input;
}
num_input = atoi(input.c_str());
if(input == "b" || input == "B")
{
gameState = BUYIN;
break;
}
else if(input == "q" || input == "Q")
{
quit = true;
break;
}
else if(input == "autoplay")
{
autoplay = true;
break;
}
else if(!players[0].setBet(num_input)) //if setBet() succeeds, skip to next else
{
status_output = "You do not have enough money. ";
drawTable();
system("pause");
break;
}
else
{
gameState = DRAW;
break;
}
//break;
case DRAW:
if(shoe->numCardsDealt() > cutCard)
{
shoe->shuffle();
if(!autoplay)
{
status_output = "The deck was shuffled. ";
drawTable();
system("pause");
}
}
dealer.getCard(shoe->drawCard()); //give the dealer his faceup card
holeCard = shoe->drawCard();
players[0].getCard(shoe->drawCard());
players[0].getCard(shoe->drawCard()); //draw 2 cards
if(dealer.getTotal() == pointValue(ACE))
gameState = INSURANCE;
else if(dealer.getTotal() == pointValue(TEN))
gameState = DEALERCHECK;
else
gameState = PLAY;
break;
case INSURANCE:
if(autoplay)
{
input = bot.playHand(NULL, 0, 0, shoe->trueCount(), INSURANCE);
}
else
{
status_output = "Insurance? ";
drawTable();
cin >> input;
}
if(input == "n" || input == "N")
{
gameState = DEALERCHECK;
}
else if(input == "y" || input == "Y")
{
if(!players[0].canInsure())
{
if(!autoplay)
{
status_output = "Cannot insure. ";
drawTable();
system("pause");
}
}
else
{
players[0].insurance();
}
}
break;
case DEALERCHECK:
if(dealer.getTotal() + pointValue(holeCard) == 21) //dealer has a blackjack
{
gameState = DEALERPLAY; //skip to the end of the hand
}
else
{
gameState = PLAY;
}
break;
case PLAY:
do
{
while(players[0].getTotal() < 21)
{
if(players[0].isSplitAce()) //cannot hit split aces
break;
if(autoplay)
{
input = bot.playHand(players[0].ptrCurrHand(), dealer.getTotal(), players[0].numOfHands(), shoe->trueCount(), PLAY);
}
else
{
status_output = "Hit, Stand, sPlit, Double, or suRrender? ";
drawTable();
cin >> input;
}
/* HIT */
if(input == "h" || input == "H")
{
players[0].getCard(shoe->drawCard());
}
/* STAND */
else if(input == "s" || input == "S")
{
break;
}
/* SPLIT */
else if(input == "p" || input == "P")
{
if(!players[0].canSplit())
{
if(!autoplay)
{
status_output = "Cannot split. ";
drawTable();
system("pause");
}
}
else
{
players[0].split(shoe->drawCard(), shoe->drawCard());
}
}
/* DOUBLE DOWN */
else if(input == "d" || input == "D")
{
if(!players[0].canDoubleDown())
{
if(!autoplay)
{
status_output = "Cannot double down. ";
drawTable();
system("pause");
}
}
else
{
players[0].doubleDown();
players[0].getCard(shoe->drawCard());
break;
}
}
/* SURRENDER */
else if(input == "r" || input == "R")
{
if(!players[0].canSurrender())
{
if(!autoplay)
{
status_output = "Too late to surrender. ";
drawTable();
system("pause");
}
}
else
{
players[0].surrender();
players[0].receiveWinnings(players[0].getHandValue() / 2); //receive surrender winnings immediately
break;
}
}
}
}
while(players[0].nextHand()); //do all the split hands
gameState = DEALERPLAY;
break;
case DEALERPLAY:
dealer.getCard(holeCard);
while(dealer.getTotal() < 17)
{
dealer.getCard(shoe->drawCard());
}
/* NEED TO REDO GAME OUTCOME LOGIC */
/*if(dealer.getTotal == 99)
{
if(players[0].getTotal() == 99)
{
}
else if(players[0].getTotal()
}
else if(dealer.getTotal() > 21)
{
}
else if(dealer.getTotal() < 21)
{
}*/
players[0].resetNextHand();
do
{
if(players[0].didSurrender())
{
if(!autoplay)
{
status_output = "Surrendered. ";
drawTable();
}
}
else if(players[0].getTotal() == 99 && dealer.getTotal() != 99)
{
players[0].receiveWinnings(players[0].getHandValue() * 2.5);
if(!autoplay)
{
status_output = "Blackjack! ";
drawTable();
}
}
else if(players[0].getTotal() > 21 && players[0].getTotal() != 99)
{
if(!autoplay)
{
//bust
status_output = "Bust. ";
drawTable();
}
}
else if(dealer.getTotal() > 21 && dealer.getTotal() != 99)
{
players[0].receiveWinnings(players[0].getHandValue() * 2);
if(!autoplay)
{
status_output = "You win. ";
drawTable();
}
}
else if(dealer.getTotal() > players[0].getTotal())
{
if(!autoplay)
{
status_output = "You lose. ";
drawTable();
}
}
else if(dealer.getTotal() < players[0].getTotal())
{
players[0].receiveWinnings(players[0].getHandValue() * 2);
if(!autoplay)
{
status_output = "You win. ";
drawTable();
}
}
else if(dealer.getTotal() == players[0].getTotal())
{
players[0].receiveWinnings(players[0].getHandValue());
if(!autoplay)
{
status_output = "Push. ";
drawTable();
}
}
else
{
//should never reach here
exit(99099);
}
if(!autoplay)
{
system("pause");
}
}
while(players[0].nextHand());
gameState = BET;
dealer.newRound();
players[0].newRound();
if(autoplay)
{
drawTable();
roundsPlayedCounter++;
if(roundsPlayedCounter == numRoundsToPlay)
{
autoplay = false;
roundsPlayedCounter = 0;
cout << numRoundsToPlay << " rounds were played. " << flush;
system("pause"); // Linux doesn't have pause
}
}
break;
}
}
} /* end play() */
void drawTable() const
{
if(autoplay)
{
if(roundsPlayedCounter % 50000 == 0)
cout << roundsPlayedCounter << "\n";
}
else
{
//system("cls"); //Linux doesn't have cls
cout << " Blackjack \n\n"; //80 spaces
cout << "Dealer: "; dealer.printHand(); cout << " Total: " << dealer.getTotal()/*((dealer.getTotal()==99) ? ("BJ") : (dealer.getTotal()))*/ << "\n";
cout << " Deck: "; drawDeck(); cout << '\n'; //39 spaces, can accomodate 8 decks
for(int i=0;i<numPlayers;i++)
{
cout << "Player " << i+1 << ", Hand " << players[i].handNum()+1 << ": "; players[i].printHand(); cout << " Total: " << players[i].getTotal()/*((players[i].getTotal()==99) ? ("BJ") : (players[i].getTotal()))*/ << "\n";
cout << "Bankroll: $" << players[i].getBankroll();
}
cout << "\n\n" << status_output << flush;
}
} /* end drawTable() */
void drawDeck() const
{
int bars, spaces;
int fieldsize = shoe->totalNumCards()/13;
bars = (shoe->totalNumCards() - shoe->numCardsDealt())/13;
spaces = fieldsize - bars;
cout << '[';
int i;
for(i=0;i<bars;i++)
{
cout << '|';
}
for(i=0;i<spaces;i++)
{
cout << ' ';
}
cout << ']';
} /* end drawDeck() */
private:
Shoe *shoe;
int numPlayers;
unsigned int cutCard;
string input;
int num_input;
string status_output;
Player dealer;
char holeCard;
Player players[2]; //todo: adjust this to the max num of players we want
PlayerAI bot;
states gameState;
bool autoplay;
int numRoundsToPlay, roundsPlayedCounter;
bool quit;
};
#endif