My BlackJack Program

Hello everyone! Check out my cool little blackjack game I programmed myself :) Please comment your feedback and changes I should make... Thank you! P.S. Anyone is welcome to use the code for themselves for whatever! Have fun!

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
  #include <iostream>
#include <algorithm>
#include <ctime>
#include <iomanip>
#include <Windows.h>
using namespace std;

HANDLE hCon;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

void SetColor(Color c)
{
	if (hCon == NULL)
		hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hCon, c);
}

class Deck
{
	int cards[52];
public:
	int count = -1;
	void set()
	{
		srand(time(0));
		int x = 1;
		for (int i = 0; i < 52; i++, x++)
		{
			if (x == 14)
				x = 1;
			cards[i] = x;
		}
		for (int x = 0; x < rand() % 100; x++)
			random_shuffle(&cards[0], &cards[52]);
	}
	void shuffle()
	{
		random_shuffle(&cards[0], &cards[52]);
	}
	int getCard()
	{
		if (count == 51)
		{
			this->shuffle();
			count = -1;
		}
		count++;
		return cards[count];
	}
	void printCard(int hand)
	{
		int card = hand;
		if (card == 1)
			cout << "A";
		else if (card < 11)
			cout << card;
		else if (card == 11)
			cout << "J";
		else if (card == 12)
			cout << "Q";
		else if (card == 13)
			cout << "K";
	}
	void printHand(int hand[], int cardCount)
	{
		for (int index = 0; index < cardCount; index++)
		{
			this->printCard(hand[index]);
			cout << " ";
		}
	}
	int printScore(int hand[], int cardCount)
	{
		int aces = 0;
		int score = 0;
		int card;
		for (int i = 0; i < cardCount; i++)
		{
			card = hand[i];
			if (card == 1)
			{
				aces++;
				score++;
			}
			else if (card < 10)
				score += card;
			else
				score += 10;
		}
		while (aces > 0 && score < 12)
		{
			aces--;
			score += 10;
		}
		return score;
	}
};
int main()
{
	Deck deck;
	deck.set();
	double money = 1000, bet = 0;
	int playerHand[12], houseHand[12];
	int playerCardCount, houseCardCount, playerScore, houseScore;
	char HorS;

	start:
	cout << "How much money would you like to start out with? $";
	cin >> money;

	while (1)
	{
		system("cls");
		if (money < 10)
		{
			cout << "Would you like to restart [Y/N]? ";
			cin >> HorS;
			if (HorS == 'Y' || HorS == 'y')
				goto start;
			else if (HorS == 'N' || 'n')
				return 0;
		}
		do
		{
			system("cls");
			if (bet > money)
			{
				cout << "You do not have that much money to bet!";
				system("pause>nul");
				system("cls");
			}
			cout << "Money : $" << money << endl;
			cout << "Enter Bet : $";
			cin >> bet;
		} while (bet > money);
		
		playerHand[0] = deck.getCard();
		playerHand[1] = deck.getCard();
		houseHand[0] = deck.getCard();
		houseHand[1] = deck.getCard();
		playerCardCount = 2;
		houseCardCount = 2;
		playerScore = deck.printScore(playerHand, playerCardCount);
		houseScore = deck.printScore(houseHand, houseCardCount);

		do
		{
			system("cls");
			cout << "Dealer's Hand:" << endl;
			cout << "* ";
			deck.printCard(houseHand[1]);
			cout << endl << "Your Hand:" << endl;
			deck.printHand(playerHand, playerCardCount);
			SetColor(BLUE);
			cout << " Score : " << playerScore;
			SetColor(WHITE);
			
			cout << endl << "[H]it or [S]tay: ";
			cin >> HorS;

			if (HorS == 'H' || HorS == 'h')
			{
				playerHand[playerCardCount] = deck.getCard();
				playerCardCount++;
				playerScore = deck.printScore(playerHand, playerCardCount);
			}
			else if (HorS == 'S' || HorS == 's')
				break;
			else
			{
				SetColor(DARKRED);
				cout << endl << "Invalid! Try agin!" << endl;
				SetColor(WHITE);
				system("pause");
			}
		} while (playerScore < 22);
		SetColor(DARKRED);
		if (playerScore > 21)
		{
			cout << endl << "You BUSTED!" << endl;
			cout << "You lose $" << bet;
			money -= bet;
		}
		else
		{
			while (houseScore < 17)
			{
				houseHand[houseCardCount] = deck.getCard();
				houseCardCount++;
				houseScore = deck.printScore(houseHand, houseCardCount);
			}
			if (houseScore > 21)
			{
				SetColor(GREEN);
				cout << endl << "Dealer BUSTED!" << endl;
				cout << "You win $" << bet;
				money += bet;
			}
			else if (playerScore > houseScore)
			{
				SetColor(GREEN);
				cout << endl << "You won!" << endl;
				cout << "You win $" << bet;
				money += bet;
			}
			else if (playerScore < houseScore)
			{
				cout << endl << "You lost!" << endl;
				cout << "You lose $" << bet;
				money -= bet;
			}
			else
			{
				SetColor(TEAL);
				cout << endl << "Tie!";
			}
		}
		SetColor(WHITE);
		cout << endl << "Dealer's Hand:" << endl;
		deck.printHand(houseHand, houseCardCount);
		cout << " Score : " << houseScore;
		cout << endl << "Your Hand:" << endl;
		deck.printHand(playerHand, playerCardCount);
		cout << " Score : " << playerScore;
		system("pause>nul");
	}
}
A few tips:

For printCard function, define an array of 13 entries so when you print the hand, you simply print the char based on card. It makes the code much cleaner and easier to maintain. Make sure you rename it to card because hand is not the right name.

If you have any primitive data types that is not going to be modified, put const before the declaration. The data value will be stored in read only memory section which will be more efficient.

For member functions in Deck that has void in return, return the instance of Deck. i.e.:

Deck &

That way, you can do the following:

1
2
Deck d;
d.set().shuffle();


for line 27, you can declare both i and x as follows:
for (int i = 1, x =1, i < 52, ++i, ++x)

Also, you don't need the extra if statement in the loop to reset x. Use modulus as follows:

cards[i] = (i + 1) % 13;

For each 13th element, it's set to zero, simply do

cards[12] = cards[25] = cards[38] = cards[51] = 13;

That way, you're using one variable (which is i) and there is no if statement.

In your main function, move most of the code into a separate function so it can be reused as an api. Also, avoid using system because system is an expensive call and the commands you put down are for windows OS. You need to think about other platforms such as linux.

In your main function, you don't return 0.
Any idea how I can implement a double down feature?
Topic archived. No new replies allowed.