Blackjack Program

Hi everyone!

I am having a lot of problems with my blackjack program that I just created. Unfortunately I cannot get it to run..if someone could help me with this problem that would be great! Thanks!

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
#include <iostream>
#include <ctime>

using namespace std;

const int NUMCARDS = 52;
const int NUMATTEMPTS = 5;
const int MAXNUMCARDS = 12;

void displayMenu();
void NewHandMenu();
void ShuffleUpDemCards(bool DeckOfCards[]);
int NewCards(bool DeckOfCards[]);
void cardCharacters(int NewCard);
void PlayersCardHand(int PlayersHand[], int PlayersDealtHand);
void HousesCardHand(int HousesHand[], int HousesDealtHand);
int PlayersHandScore(int PlayersHand[], const int PlayersDealtHand);
int HousesHandScore(int HousesHand[], const int HousesDealtHand);
void ScoresAndHands(int HousesHand[], const int HousesDealtHand, int PlayersHand[], const int PlayersDealtHand);


int main()
{
	time_t qTime;
	time(&qTime);
	srand(qTime);


	bool DeckOfCards[NUMCARDS];
	int PlayersHand[MAXNUMCARDS];
	int HousesHand[MAXNUMCARDS];
	int PlayersDealtHand = 0;
	int HousesDealtHand = 0;
	int badAttempts=0;
	
	
	
	while (false) {
		displayMenu();
		ShuffleUpDemCards(DeckOfCards);
		
		PlayersHand[0] = NewCards(DeckOfCards);
		PlayersHand[1] = NewCards(DeckOfCards);
		HousesHand[0] = NewCards(DeckOfCards);
		HousesHand[1] = NewCards(DeckOfCards);

		HousesDealtHand = 2;
		PlayersDealtHand = 2;
	}

	NewHandMenu();

	char choice;
	bool HitsforPlayer = true;
	int PlayerScore = PlayersHandScore(PlayersHand, PlayersDealtHand);
	int HouseScore = HousesHandScore(HousesHand, HousesDealtHand);
	do {
		cout << "The House's Hand" << endl;
		cardCharacters(HousesHand[1]);
		cout << endl;
		cout << "The Player's Score = " << PlayersHandScore(PlayersHand, PlayersDealtHand) << endl;
		PlayersCardHand(PlayersHand, PlayersDealtHand);

		cout << "Player, would you like to hit(press h) or stay(press s)";
		cin >> choice;

		if(choice == 'h') {
			cout << "Player selects to hit!!\n";
			PlayersHand[PlayersDealtHand] = NewCards(DeckOfCards);
			PlayersDealtHand++;
		}
		else if(choice == 's') {
			cout << "Player selects to stay!!\n";
		bool HitsforPlayer = false;
		}
		else {
			cout << "You entered an invalid choice!!!\n";
			cout << "Please try again! This time press an ""h"" to hit or press a ""s"" to stay!!\n";
			cin >> choice;
		}

		cout << endl << endl;
		PlayerScore = PlayersHandScore(PlayersHand, PlayersDealtHand);
	} while (HitsforPlayer && PlayerScore < 22);

	if(PlayerScore > 21) {
		cout << "I am sorry, but you have busted!!\n";
		cout << "The House Wins the Hand!!!\n";
		ScoresAndHands(HousesHand, HousesDealtHand, PlayersHand, PlayersDealtHand);
	}

	else {
		while (HouseScore < 17) {
			HousesHand[HousesDealtHand] = NewCards(DeckOfCards);
			HousesDealtHand++;
			HouseScore = HousesHandScore(HousesHand, HousesDealtHand);
		}

		bool HouseBusts = (HouseScore > 21);

		if(HouseBusts) {
			cout << "Congratulations, the House Busted!!!\n";
			cout << "You win the Hand!!\n";
			ScoresAndHands(HousesHand, HousesDealtHand, PlayersHand, PlayersDealtHand);
		}
		else {
			if (PlayerScore == HouseScore) {
				cout << "Both your hand and the Houses hand are the same!!\n";
				cout << "The hand results in a push, and no one wins!!\n";
				ScoresAndHands(HousesHand, HousesDealtHand, PlayersHand, PlayersDealtHand);
			}
			else if (PlayerScore > HouseScore) {
				cout << "Your hand is higher than the Houses hand!!\n";
				cout << "You win the hand!!\n";
				ScoresAndHands(HousesHand, HousesDealtHand, PlayersHand, PlayersDealtHand);
			}

			else  {
				cout << "The Houses hand is higher than your hand!!\n";
				cout << "The House wins the hand!!\n";
				ScoresAndHands(HousesHand, HousesDealtHand, PlayersHand, PlayersDealtHand);
			}
		}
	}
}


void displayMenu()
{
	cout << "*******************************************\n";
	cout << "HI, AND WELCOME TO OUR BLACKJACK PROGRAM!!!\n";
	cout << "*******************************************\n";
	
}

void NewHandMenu()
{
	cout << "*******************************************\n";
	cout << "------Starting a New Hand------------------\n";
	cout << "*******************************************\n";

}


void ShuffleUpDemCards(bool DeckOfCards[])
{
	int index;
	for(index=0; index<NUMCARDS; index ++) {
		DeckOfCards[52] = false;
	}
}

int NewCards(bool DeckOfCards[])
{
	DeckOfCards[NUMCARDS] = true;
	int NewCard = -1;
	do {
		NewCard = (rand() % 52);
		if (!DeckOfCards[NewCard]) {
			DeckOfCards = false;
		}
	} while (DeckOfCards);
	return NewCard;
}

void cardCharacters(int NewCard)
{
	const int RANK = (NewCard%13);
	const int SUIT = (NewCard/13);
	if(RANK==0)
		cout << "Ace of";
	else if(RANK<9)
		cout << (RANK+1) << " of ";
	else if(RANK==10)
		cout << "Jack of ";
	else if(RANK==11)
		cout << "Queen of ";
	else 
		cout << "King of ";
	
	if(SUIT==0)
		cout << "Hearts";
	else if(SUIT==1)
		cout << "Spades";
	else if(SUIT==2)
		cout << "Diamonds";
	else 
		cout << "Clubs";
}

void PlayersCardHand(int PlayersHand[], int PlayersDealtHand)
{
	int CardIndex;

	for(CardIndex = 0; CardIndex < PlayersDealtHand; CardIndex++) {
		const int NewCard = PlayersHand[CardIndex];
		cardCharacters(NewCard);
		cout << " ";
	}
	cout << endl;

}

void HousesCardHand(int HousesHand[], int HousesDealtHand)
{
	int CardIndex;

	for(CardIndex = 0; CardIndex < HousesDealtHand; CardIndex++) {
		const int NewCard = HousesHand[CardIndex];
		cardCharacters(NewCard);
		cout << " ";
	}
	cout << endl;
}

int PlayersHandScore(int PlayersHand[], const int PlayersDealtHand) 
{
	int AceCount	= 0;
	int Score		= 0;
	int CardIndex;
	for (CardIndex = 0; CardIndex < PlayersDealtHand; CardIndex++) {
		const int NextCard = PlayersHand[CardIndex];
		const int Rank = (NextCard % 13);
		if (Rank == 0) {
			AceCount++;
			Score++;
		} 
		else if (Rank < 9) {
			Score += (Rank + 1);
		} 
		else {
			Score += 10;
		}
	}
	while (AceCount > 0 && Score < 12) {
		AceCount--;
		Score +=  10;
	}
	return Score;
}

int HousesHandScore(int HousesHand[], const int HousesDealtHand)
{
	int AceCount	= 0;
	int Score		= 0;
	int CardIndex;
	for (CardIndex = 0; CardIndex < HousesDealtHand; CardIndex++) {
		const int NextCard = HousesHand[CardIndex];
		const int Rank = (NextCard % 13);
		if (Rank == 0) {
			AceCount++;
			Score++;
		} 
		else if (Rank < 9) {
			Score += (Rank + 1);
		} 
		else {
			Score += 10;
		}
	}
	while (AceCount > 0 && Score < 12) {
		AceCount--;
		Score +=  10;
	}
	return Score;
}

void ScoresAndHands(int HousesHand[], const int HousesDealtHand, int PlayersHand[], const int PlayersDealtHand) 
{
	cout << "House's Hand: Score = " << HousesHandScore(HousesHand, HousesDealtHand) << endl;
	HousesCardHand(HousesHand, HousesDealtHand);
	cout << "Player's Hand: Score = " << PlayersHandScore(PlayersHand, PlayersDealtHand) << endl;
	PlayersCardHand(PlayersHand, PlayersDealtHand);
	cout << endl;
}
It compiles and runs.

What does this do?
1
2
3
4
5
6
7
8
9
10
11
12
	while (false) {
		displayMenu();
		ShuffleUpDemCards(DeckOfCards);
		
		PlayersHand[0] = NewCards(DeckOfCards);
		PlayersHand[1] = NewCards(DeckOfCards);
		HousesHand[0] = NewCards(DeckOfCards);
		HousesHand[1] = NewCards(DeckOfCards);

		HousesDealtHand = 2;
		PlayersDealtHand = 2;
	}

The code you are talking about is supposed to get 2 cards for the starting hands of both the player and the house. Also the while loop is supposed to be set to (true) and not (false). Because it is set to false the program does not output right, and therefore doesn't run right. Is there a simpler way at all to write just a simple blackjack program?
Last edited on
Ther idea of using a loop to do turns is ok. I was really pointing you to the while (false) clause.

Have you tried stepping thru your program with a debugger? There is a school of thought (Steve Maguire) that says you should see every line of your code execute.
I could try that, but I am an absolute beginner at programming. Is there any possible way that you could help me with this program?
You should try the debugging approach first and then come to us with specific questions you have about what isn't working.
If you don't have a debugger, you could insert temporary cout statements to show where the program is and the value of relevant variables.

Debugging is a part of programming and you'll learn so much by doing so, however you do it.
I have changed this program around. Please refer to my new Blackjack Program thread..I am having some problems with that code as well.



http://cplusplus.com/forum/general/17034/

thanks for any help that you can provide me!
Topic archived. No new replies allowed.