-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.cpp
146 lines (130 loc) · 3.06 KB
/
blackjack.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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function to generate a random number between min and max (inclusive)
int get_random_number(int min, int max)
{
static bool first = true;
if (first)
{
srand(time(NULL));
first = false;
}
return min + rand() % ((max + 1) - min);
}
// Function to convert card number to face value
int get_card_value(int card_number)
{
if (card_number > 10)
{
return 10;
}
else if (card_number == 1)
{
return 11;
}
else
{
return card_number;
}
}
int main()
{
// Initialize game variables
int player_score = 0;
int dealer_score = 0;
int player_aces = 0;
int dealer_aces = 0;
// Print welcome message
cout << "Welcome to CLI Blackjack!" << endl;
// Deal initial cards
player_score += get_card_value(get_random_number(1, 13));
dealer_score += get_card_value(get_random_number(1, 13));
player_score += get_card_value(get_random_number(1, 13));
dealer_score += get_card_value(get_random_number(1, 13));
// Check for blackjack
if (player_score == 21)
{
cout << "Player has blackjack! Player wins!" << endl;
return 0;
}
if (dealer_score == 21)
{
cout << "Dealer has blackjack! Dealer wins!" << endl;
return 0;
}
// Print initial scores
cout << "Player score: " << player_score << endl;
cout << "Dealer score: " << dealer_score << endl;
// Player's turn
while (player_score < 21)
{
char choice;
cout << "Hit or stand? (h/s) ";
cin >> choice;
if (choice == 'h')
{
int card = get_card_value(get_random_number(1, 13));
player_score += card;
if (card == 11)
{
player_aces++;
}
}
else
{
break;
}
// Check for bust
if (player_score > 21 && player_aces > 0)
{
player_score -= 10;
player_aces--;
}
// Print current score
cout << "Player score: " << player_score << endl;
}
// Check for player bust
if (player_score > 21)
{
cout << "Player busts! Dealer wins!" << endl;
return 0;
}
// Dealer's turn
while (dealer_score < 17)
{
int card = get_card_value(get_random_number(1, 13));
dealer_score += card;
if (card == 11)
{
dealer_aces++;
}
// Check for bust
if (dealer_score > 21 && dealer_aces > 0)
{
dealer_score -= 10;
dealer_aces--;
}
}
// Check for dealer bust
if (dealer_score > 21)
{
cout << "Dealer busts! Player wins!" << endl;
return 0;
}
// Compare scores
if (player_score > dealer_score)
{
cout << "Player wins!" << endl;
}
else if (dealer_score > player_score)
{
cout << "Dealer wins!" << endl;
}
else
{
cout << "It's a tie!" << endl;
}
return 0;
}