Blackjack Program

I am having a lot of problems with my blackjack program. It doesn't run at all! If someone can help me 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 (true {
		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;
}
Last edited on
Ugh, that's a lot of code.

What does or doesn't it do? What do you expect?
Where is the problem situated?

Any debugging info ?
Well. This program is supposed to be a blackjack program. But unfortunately I think that I completely messed up on programming it. First off, the program compiles but nothing happens. I need to resolve this problem before I can even begin to resolve anything else.
Not that I am some awesomely skilled coder or anything, but where are the comments? One of my first instructors drilled that into us and I suggest you teach yourself the same thing. thorough commenting not only helps other people understand what your code is doing, but will help you when you look back on the code. That being said, I will take a look at the code in a few hours when I am at a computer with an IDE.

GL,
UNkillableThrill

edit (10:31am) - line 38: you need a second ')'
edit (10:34am) - line 169: (NewCard/13); should be (NewCard%4);

edit (10:37am) - line 166: I would not use if/else so much, it can make it a little hard to follow the code. In this situation, I would use switch(). Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
 
switch(RANK){
  case 0:
    cout << "Ace of ";
  case 10:
    cout << "Jack of ";
  case 11:
    cout << "Queen of ";
  case 12:
    cout << "King of ";
  default:
    cout << RANK+1 << " of ";
}


Note, as I did above, you also want to put a space after the card value so that the suit is not stuck on the back end of the word "of": cout << "Ace of ";
Last edited on
One more question, have you used structures or pointers at all yet? If you have experience with those, then I would suggest making the players' hands and the deck an array of Card structs. Pass a pointer to the individual card to the functions. Ex:
1
2
3
4
5
struct Card{
  string name;  // to use a string, you will need to also use #include <string>
  int suit;
  int value;
};


Then you can use the case to also append the name variable to say the card's value:
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
Card newCard;
// let's assume you initialize newCard to have a value of 11 and a suit of 4
//your cardCharacter() will now be written like this (assuming you pass a pointer to the card):
switch(newCard->value){
  case 0:
    newCard->name = "Ace of ";
  case 10:
    newCard->name = "Jack of ";
  case 11:
    newCard->name = "Queen of ";
  case 12:
    newCard->name = "King of ";
  default:
    newCard->name = value + 1;
    newCard->name.append(" of ");
}

//and your  switch() for the suit would be:

switch(newCard->suit){
  case 0:
    newCard->name.append("Hearts");
  case 1:
    newCard->name.append("Spades");  
  case 2:
    newCard->name.append("Diamonds"); 
  case 3:
    newCard->name.append("Clubs"); 
  default:
    cout << "INVALID SUIT VALUE";
}
Last edited on
Topic archived. No new replies allowed.