-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjackGame.cpp
104 lines (82 loc) · 2.62 KB
/
BlackjackGame.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
#include "BlackjackGame.h"
#include <iostream>
BlackjackGame::BlackjackGame(int numDecks) : shoe(numDecks), balance(1000) {}
void BlackjackGame::playGame() {
player.clearHand();
dealer.clearHand();
// Check if the shoe needs to be reshuffled
if (shoe.isEmpty()) {
shoe.shuffle();
std::cout << "Reshuffling the shoe." << std::endl;
}
displayBalance();
dealInitialCards();
playerTurn();
dealerTurn();
determineWinner();
displayBalance();
askToPlayAgain();
}
int BlackjackGame::getDealerTotal() const {
return dealer.getHandValue();
}
int BlackjackGame::getPlayerTotal() const {
return player.getHandValue();
}
void BlackjackGame::dealInitialCards() {
player.addCard(shoe.dealCard());
dealer.addCard(shoe.dealCard());
player.addCard(shoe.dealCard());
std::cout << "YOU: ";
player.displayHand();
std::cout << "D: ";
dealer.displayPartialHand();
}
void BlackjackGame::playerTurn() {
char choice;
do {
std::cout << "Do you want to hit (h) or stand (s)? ";
std::cin >> choice;
if (choice == 'h') {
player.addCard(shoe.dealCard());
std::cout << "Player's ";
player.displayHand();
}
} while (choice == 'h' && player.getHandValue() < 21);
}
void BlackjackGame::dealerTurn() {
dealer.displayPartialHand();
while (dealer.getHandValue() < 17) {
dealer.addCard(shoe.dealCard());
std::cout << "Dealer hits." << std::endl;
dealer.displayPartialHand();
}
std::cout << "Dealer stands with a total of " << dealer.getHandValue() << " points." << std::endl;
}
void BlackjackGame::determineWinner() {
int playerValue = getPlayerTotal();
int dealerValue = getDealerTotal();
std::cout << "Player has " << playerValue << " points. Dealer has " << dealerValue << " points." << std::endl;
if (playerValue > 21 || (dealerValue <= 21 && dealerValue >= playerValue)) {
std::cout << "Dealer wins!" << std::endl;
balance -= 10;
}
else {
std::cout << "Player wins!" << std::endl;
balance += 10;
}
}
void BlackjackGame::displayBalance() const {
std::cout << "Current balance: $" << balance << std::endl;
}
void BlackjackGame::askToPlayAgain() {
char choice;
std::cout << "Do you want to play again? (y/n): ";
std::cin >> choice;
if (choice == 'y') {
playGame();
}
else {
std::cout << "Thanks for playing! Your final balance is: $" << balance << std::endl;
}
}