Help with a function.

Hi Guys,
First time poster here. Firstly yes this is a homework assignment and No I'm not after solutions just fresh eyes :). This is my Blackjack program. It compiles and runs however I'm finding that when the dealer is dealt his hand the values of the cards K (or cards[12][0]) is not converting to 10. This only occurs when it comes to the dealer not the player. for cards J and Q the value changes but not K. Any help in pointing out what I'm missing would be great.

Cheers

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
276
277
278
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include "blackjack.h"
using namespace std;



int main()
{

	int seed;

	cout << "Enter your Student Number to begin play: " << endl;
	cin >> seed;

	srand(seed);	

	for(int i = 0; i < 13; i++) //allocates card values to array
	{
		cards[i][0] = i + 1;
		cards[i][1] = 4;


	}

	deal(); //deals player cards
	fc_chk(cards[card][0]); //checks for face cards
	val_chg(cards[card][0]); // changes face card array values to 10
	p_calc(cards[card][0]); // calculates cards
	
	cout << " and ";
	deal();
	fc_chk(cards[card][0]);
	val_chg(cards[card][0]);
	p_calc(cards[card][0]);
	cout << " which totals: " << p_total << endl;

	move();

	deal(); // deals dealer cards
	fc_chk(cards[card][0]);
	val_chg(cards[card][0]);
	d_calc(cards[card][0]);

	cout << " and ";
	deal();
	fc_chk(cards[card][0]);
	val_chg(cards[card][0]);
	d_calc(cards[card][0]);
	cout << " Which totals: " << d_total << endl;

	dlr_chk(d_total);

	winner(p_total, d_total);


}

int deal()
{
	


	card = (rand() % 13+1); //randomly draws cards
		if (cards[card][1] == 0)
		{
			card = (rand() % 13+1);
			cards[card][1] -= cards[card][1] - 1;
		}
		else
			cards[card][1] -= cards[card][1] - 1;

	return cards[card][0];
}

int p_calc(int num) //calculates player cards
{
	if(num == cards[0][0])
	{
		ace_chk(num, p_total);
	}
	
	p_total += num;

		
		if(p_total > 21)
		{
			cout << "You Bust!" << endl;
		}

	return p_total;

}

int d_calc(int num) //calculates dealer hand
{
	if(num == cards[0][0])
	{
		ace_chk(num, p_total);
	}

	d_total += num;
	
	return d_total;
}

int move() //determines player's move
{
	char select;

	cout << "Would you like to [h]it or [s]tand?" << endl;
	cin >> select;

		switch (select)
		{
			case 'h':
			case 'H':
			{
				deal();
					cout << "your new card is " << cards[card][0];
					p_calc(cards[card][0]);
					cout << " with a new total of: " << p_total << endl;

				move();
				break;
			}
			case 's':
			case 'S':
			{
				cout << "you stand on: " << p_total << endl;
				break;
			}
		}
}

int dlr_chk(int tot) //determines whether dealer hits or stands
{
	if (tot < 17)
	{
		
		
		cout << "Dealers new card is ";
		deal();
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		d_calc(cards[card][0]);
		cout << " to a new total of: " << d_total << endl;
		
		dlr_chk(d_total);
	}

	if (tot >= 17 && tot < 21)
	{
		cout << "Dealer stands on " << d_total << endl;
	}
	
	if (tot > 21)
	{
		cout << "Dealer Busts!" << endl;
	}
}

void winner(int p, int d) // determines the winner
{
	if(p > d && p < 21 || d > 21)
	{
		cout << "You win" << endl;
	}

	if(p == d)
	{
		cout << "It's a Push!" << endl;
	}

	if(p < d && d < 21 || p > 21)
	{
		cout << "Dealer wins" << endl;
	}
}

void fc_chk(int f) // checks for face cards
{
	
	switch (f)
	{
		case 1:
		{
			cout << face[3];

			break;
		}

		case 11:
		{
			
			cout << face[0];
			

			break;
		}
		
		case 12:
		{

			
			cout << face[1];
			

			break;
		}

		case 13:
		{

			
			cout << face[2];
			
		
			break;
		}

		default:
		{
			cout << cards[card][0];
		}

	}

}
int val_chg(int val) // changes calue of J Q K to 10 (king is not changing??)
{
	if(val == cards[0][0])
	{
		cards[0][0] = 11;
	}	
	
	if(val == cards[10][0])
	{

		cards[10][0] = 10;
	}

	if(val == cards[11][0])
	{

		cards[11][0] = 10;
	}
	
	if(val == cards[12][0])
	{

		cards[12][0] = 10;
	}

	else
	{
		cards[card][0] = cards[card][0];
	}
}

int ace_chk(int a, int t) // checks Ace and changes value depending on total
{
	if(t <= 10 && t < 21)
	{
		a = 11;

		return a;
	}
	else
	{
		a = 1;
	
		return a;
	}	
}

closed account (j3Rz8vqX)
With just assumptions, due to not having the header file, here are some possibilities:

Maybe insert:
1
2
    cout<<"Card: "<<card<<endl;//Figure out what index we're at.
    cout<<"Card value: "<<num<<endl;//Figure out if the value changed. 

into line 79, line 98.

Other stuff:

Not the culprit:
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
int val_chg(int val) // changes calue of J Q K to 10 (king is not changing??)
{
	if(val == cards[0][0])
	{
		cards[0][0] = 11;
	}

	if(val == cards[10][0])
	{

		cards[10][0] = 10;
	}

	if(val == cards[11][0])
	{

		cards[11][0] = 10;
	}

	if(val == cards[12][0])
	{

		cards[12][0] = 10;
	}

	else
	{
		cards[card][0] = cards[card][0];
	}
}

Is it intended that the above not return a value from the function?
But this doesn't help us.

Also not the culprit:
1
2
3
4
5
6
7
8
9
10
11
int d_calc(int num) //calculates dealer hand
{
	if(num == cards[0][0])
	{
		ace_chk(num, p_total);
	}

	d_total += num;
	
	return d_total;
}

Should this be d_total? Not sure, since player has p_total, dealer may want d_total; but then again you may be wanting to compete with player, apologies if i'm wrong.
Last edited on
sorry here is the header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

int deal();
int p_calc(int);
int d_calc(int);
int move();
int dlr_chk(int);
int cards[13][2];
int card;
int p_total = 0, d_total = 0;
void winner(int, int);
void fc_chk(int);
char face[4] = {'J', 'Q', 'K', 'A'};
int val_chg(int);
int win_track(int, int);
int p_points = 0, d_points = 0;
int ace_chk(int, int);
int player;
int dealer;
int player_wins;
int dealer_wins;
int score_calc(int, int);


I inserted the cout you suggested and it is showing the correct card position but the value has not changed from 13 to 10, btw thanks for pointing out the p_total mistake in ace_chk.. that would have caused problems later for sure.
Looking closer i noticed that the K card position has changed from cards[12][0] to cards[13][0]... what would have caused this?
closed account (j3Rz8vqX)
Before line 122's insertion, you need to change the value of J,Q,K,1 to their appropriate values.
1
2
3
4
5
				        deal();
					cout << "your new card is " << cards[card][0];
					val_chg(cards[card][0]);
					p_calc(cards[card][0]);
					cout << " with a new total of: " << p_total << endl;


Edit:
Also, 165421351%13 = (0-12)+1 = (1-13);
0=A; //cards[0][0] = 11;
1=2;
2=3;
...
10=J;
11=Q;
12=K;
13=!@#$%$@#;?

Possibly rand() % 12+1 = (1-12)

Edit #2:
Actually, you'll never get A, since 0 is not possible.
Just do: rand() % 13; = (0-12), counting from zero seems valid.

Have a good day.
Last edited on
thank you so much.. i just realised that error pretty much 2 mins before you posted this, but good to get confirmation.

Have a GREAT day :)
And I'm back.. I almost have the game ready however there is a problem in my ace_chk function. When called it is working correctly and going into the correct if statement however the value is not changing.. im sure it is a tiny thing that I cant see I just need some fresh eyes.

this is the header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

int deal();
int p_calc(int);
int d_calc(int);
void move();
void dlr_chk(int);
int cards[13][2];
int card;
int p_total = 0, d_total = 0;
void winner(int, int);
void fc_chk(int);
char face[4] = {'J', 'Q', 'K', 'A'};
void val_chg(int);
int p_points = 0, d_points = 0;
int ace_chk(int, int);
int player;
int dealer;
int player_wins;
int dealer_wins;
void score_calc(int, int);
void bj_chk(int);
bool playing = true;
void play_again();
 


and the actual program:
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include "blackjack.h"
using namespace std;



int main()
{
		int seed;

		cout << "Enter your Student Number to begin play: " << endl;
		cin >> seed;

		srand(seed);	

		for(int i = 0; i < 13; i++) //allocates card values to array
		{
			cards[i][0] = i + 1;
			cards[i][1] = 4;
		}

	while(playing)
	{

		deal(); //deals player cards
		fc_chk(cards[card][0]); //checks for face cards
		val_chg(cards[card][0]); // changes face card array values to 10/11
		p_calc(cards[card][0]); // calculates cards
		//cout << p_total << endl; this was doubling your first output.
	
		cout << " and ";
		deal();
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		p_calc(cards[card][0]);
		cout << " which totals: " << p_total << endl;

		bj_chk(p_total);

		move();

		deal(); // deals dealer cards
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		d_calc(cards[card][0]);

		cout << " and ";
		deal();
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		d_calc(cards[card][0]);
		cout << " Which totals: " << d_total << endl;

		dlr_chk(d_total);

		winner(p_total, d_total);

		play_again();
	}	

}

int deal() //deals cards
{
	


	card = (rand() % 13); //randomlises cards
		if (cards[card][1] == 0)
		{
			card = (rand() % 13);
			cards[card][1] -= cards[card][1] - 1;
		}
		else
			cards[card][1] -= cards[card][1] - 1;

	return cards[card][0];
}

int p_calc(int num) //calculates player cards
{

	p_total += num;



	return p_total;

}

int d_calc(int num) //calculates dealer hand
{


	d_total += num;
	
	return d_total;
}

void move() //determines player's move
{
	char select;

	cout << "Would you like to [h]it or [s]tand?" << endl;
	cin >> select;
	select = toupper(select);

		switch (select)
		{
			case 'H':
				deal();
				cout << "your new card is: "; 
				fc_chk(cards[card][0]);
				val_chg(cards[card][0]);
				p_calc(cards[card][0]);
				cout << " with a new total of: " << p_total << endl;
				
				if(p_total > 21)
				{
					cout << "You Bust" << endl;
				}
				else if(p_total < 21)
				{
					move();
				}
				break;
			case 'S':
				cout << "you stand on: " << p_total << endl;
				break;
			default:
				break;
		}
}

void dlr_chk(int tot) //determines whether dealer hits or stands
{
	if (tot < 17)
	{
		
		
		cout << "Dealers new card is ";
		deal();
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		d_calc(cards[card][0]);
		cout << " to a new total of: " << d_total << endl;
		
		dlr_chk(d_total);
	}

	if (tot >= 17 && tot < 21)
	{
		cout << "Dealer stands on " << d_total << endl;

		//return d_total;
	}
	
	if (tot > 21)
	{

		cout << "Dealer Busts!" << endl;

		//return d_total;
	}
}

void winner(int p, int d) // determines the winner
{
	if((p > d && p < 21) || d > 21)
	{
		cout << "You win" << endl;
		
		score_calc(p, d);
	}

	if(p == d)
	{
		cout << "It's a Push!" << endl;
	}

	if((p < d && d < 21) || p > 21)
	{
		cout << "Dealer wins" << endl;

		score_calc(p, d);
	}
}

void fc_chk(int f) // checks for face cards
{
	
	switch (f)
	{
		case 1:
		{
			cout << face[3];

			break;
		}

		case 11:
		{	
			cout << face[0];
			

			break;
		}
		
		case 12:
		{
			cout << face[1];
			

			break;
		}

		case 13:
		{
			cout << face[2];
			
		
			break;
		}

		default:
		{
			cout << cards[card][0];
		}

	}

}
void val_chg(int val) // changes calue of J Q K to 10
{	

	if(val == cards[0][0])
	{
		ace_chk(cards[0][0], p_total);

	}
	
	if(val == cards[10][0])
	{

		cards[10][0] = 10;
		
	}

	if(val == cards[11][0])
	{

		cards[11][0] = 10;

	}
	
	if(val == cards[12][0])
	{

		cards[12][0] = 10;

	}

	else
	{
		cards[card][0] = cards[card][0];
	}
}

int ace_chk(int a, int t) // checks Ace and changes value depending on total
{

	if(a == cards[0][0] && t <= 10)
	{
		a = 11;
		cout << "X " << endl;

		return a;
	}
	else if(a == cards[0][0] && t > 10)
	{
		a = 1;
		cout << "Y " << endl;
	
		return a;
	}
	else
	{
		return cards[card][0];
	}
}

void score_calc(int p, int d) //calculates and dislplays scores
{
	if((p > d && p < 21) || d > 21)
	{
		
		player += 5;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}

	if(p > d && p == 21)
	{
		player += 10;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}

	if((p < d && d < 21) || p > 21)
	{
		dealer += 5;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}
	
	if(p < d && d == 21)
	{
		dealer += 10;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}

}

void bj_chk(int bj)
{
	if(bj == 21)
	{
		cout << "Winner Winner, Chicken Dinner. Blackjack!" << endl;
	}
	
}

// Loops to continue playing.
void play_again()
{

	char choice;
	cout << "Care for another game?" << endl;
	cin >> choice;

	choice= toupper(choice);

	switch(choice){
		case 'Y':
			p_total = 0;
			d_total = 0;
			cout << "New game starting now..." << endl;
			cout << endl;
			break;
		case 'N':
			cout << "Thanks for playing.." << endl;
			playing = false;
			break;
		default:
			cout << "Invalid selection" << endl;
			play_again();
			break;
	}
}
closed account (j3Rz8vqX)
Your ace check has modify your value and returned it, but you have nothing on the receiving end to catch it.

Ace check function is called:
1
2
3
4
5
	if(val == cards[0][0])
	{
		ace_chk(cards[0][0], p_total);//Function called!

	}

Function processed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int ace_chk(int a, int t) // checks Ace and changes value depending on total
{

	if(a == cards[0][0] && t <= 10)
	{
		a = 11;
		cout << "X " << endl;

		return a;
	}
	else if(a == cards[0][0] && t > 10)
	{
		a = 1;
		cout << "Y " << endl;

		return a;
	}
	else
	{
		return cards[card][0];
	}
}


When the value (a) is returned, it needs something to contain is value.

You should assign it to cards[0][0].

You should be able manage that.

Hint:
Line 240 should assign the returning function to a variable, in your case:cards[0][0]

Example of assigning returned data from a function to a variable.
1
2
3
4
5
6
7
8
9
10
11
12
void myFunction(int i)
{
    return i*2;
}
int main()
{
    int i = 7;
    std::cout<<"The value of i: "<<i<<std::endl;
    i = myFunction(i);//<----------------------------------Calling function and assigning return value to variable i.
    std::cout<<"The new value of i: "<<i<<endl;
    return 0;
}


C++ reads code from right to left.

The assignment of i is implemented last.

Have a good day.
thanks ive made the changes so it allocates to the card in a void function.. im a bit shaky on int functions so ill need to study up on them a bit more.. ive found one last bug that i cant get to work, in this case its in the black jack checker for the player... for some reason when A is the first card dealt it equates to 11.. however i threw in a cout statement to see what was going on and it went to the correct path and then the J that was dealt also went into ace_chk :\

here are the changes:
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include "blackjack.h"
using namespace std;



int main()
{
		int seed, game = 0;

		cout << "Enter your Student Number to begin play: " << endl;
		cin >> seed;

		srand(seed);	

		for(int i = 0; i < 13; i++) //allocates card values to array
		{
			cards[i][0] = i + 1;
			cards[i][1] = 4;
		}

	while(playing)
	{

		deal(); //deals player cards
		fc_chk(cards[card][0]); //checks for face cards
		val_chg(cards[card][0]); // changes face card array values to 10/11
		p_calc(cards[card][0]); // calculates cards
	
		cout << " and ";
		deal();
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		p_calc(cards[card][0]);
		cout << " which totals: " << p_total << endl;

		playerbj_chk(p_total);

			if(playing)
			{		
				move();
			}

				if(playing)
				{

					deal(); // deals dealer cards
					fc_chk(cards[card][0]);
					val_chg(cards[card][0]);
					d_calc(cards[card][0]);

					cout << " and ";
					deal();
					fc_chk(cards[card][0]);
					val_chg(cards[card][0]);
					d_calc(cards[card][0]);
					cout << " Which totals: " << d_total << endl;

					dlr_chk(d_total);
				}
		
		winner(p_total, d_total);

		playing = true;


		play_again();
		
	}	

}

int deal() //deals cards
{
	


	card = (rand() % 13); //randomlises cards
		if (cards[card][1] == 0)
		{
			card = (rand() % 13);
			cards[card][1] -= cards[card][1] - 1;
		}
		else
			cards[card][1] -= cards[card][1] - 1;

	return cards[card][0];
}

int p_calc(int num) //calculates player cards
{

	p_total += num;



	return p_total;

}

int d_calc(int num) //calculates dealer hand
{


	d_total += num;
	
	return d_total;
}

void move() //determines player's move
{
	char select;

	cout << "Would you like to [h]it or [s]tand?" << endl;
	cin >> select;
	select = toupper(select);

		switch (select)
		{
			case 'H':
				deal();
				cout << "your new card is: "; 
				fc_chk(cards[card][0]);
				val_chg(cards[card][0]);
				p_calc(cards[card][0]);
				cout << " with a new total of: " << p_total << endl;
				
				if(p_total > 21)
				{
					cout << "You Bust" << endl;

					playing = false;
				}
				else if(p_total < 21)
				{
					move();
				}
				break;
			case 'S':
				cout << "you stand on: " << p_total << endl;
				break;
			default:
				break;
		}
}

void dlr_chk(int tot) //determines whether dealer hits or stands
{
	if (tot < 17)
	{
		
		
		cout << "Dealers new card is ";
		deal();
		fc_chk(cards[card][0]);
		val_chg(cards[card][0]);
		d_calc(cards[card][0]);
		cout << " to a new total of: " << d_total << endl;
		
		dlr_chk(d_total);
	}

	if (tot >= 17 && tot < 21)
	{
		cout << "Dealer stands on " << d_total << endl;

	}
	
	if (tot > 21)
	{

		cout << "Dealer Busts!" << endl;
	}

	else
	{
		dealerbj_chk(tot);
	}
}

void winner(int p, int d) // determines the winner
{
	if((p > d && p < 21) || d > 21)
	{
		cout << "You win" << endl;
		
		score_calc(p, d);
	}

	if(p == d)
	{
		cout << "It's a Push!" << endl;
		
		score_calc(p, d);
	}

	if((p < d && d < 21) || p > 21)
	{
		cout << "Dealer wins" << endl;

		score_calc(p, d);
	}
}

void fc_chk(int f) // checks for face cards
{
	
	switch (f)
	{
		case 1:
		{
			cout << face[3];

			break;
		}

		case 11:
		{	
			cout << face[0];
			

			break;
		}
		
		case 12:
		{
			cout << face[1];
			

			break;
		}

		case 13:
		{
			cout << face[2];
			
		
			break;
		}

		default:
		{
			cout << cards[card][0];
		}

	}

}
void val_chg(int val) // changes calue of J Q K to 10
{	

	if(val == cards[0][0])
	{
		ace_chk(cards[0][0], p_total);

	}
	
	if(val == cards[10][0])
	{

		cards[10][0] = 10;
		
	}

	if(val == cards[11][0])
	{

		cards[11][0] = 10;

	}
	
	if(val == cards[12][0])
	{

		cards[12][0] = 10;

	}

	else
	{
		cards[card][0] = cards[card][0];
	}
}

void ace_chk(int a, int t) // checks Ace and changes value depending on total
{

	if(a == cards[0][0] && t <= 11)
	{
		cards[0][0] = 11;
		cout << "X ";
	}
	else if(a == cards[0][0] && t > 11)
	{
		cards[0][0] = 1;
		cout << "Y ";
	}
	else
	{
 		cards[card][0] = cards[card][0];
	}
}

void score_calc(int p, int d) //calculates and dislplays scores
{
	if((p > d && p < 21) || d > 21)
	{
		
		player += 5;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}

	if(p > d && p == 21)
	{
		player += 10;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}

	if((p < d && d < 21) || p > 21)
	{
		dealer += 5;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}
	
	if(p < d && d == 21)
	{
		dealer += 10;

		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;
	}

	else
	{
		cout << "Player's Score: " << player << endl;
		cout << "Dealer's Score: " << dealer << endl;		
	}

}

void playerbj_chk(int bj)
{
	if(bj == 21)
	{
		cout << "Winner Winner, Chicken Dinner. Blackjack!" << endl;
		
		player += 15;

		playing = false;
		
	}
	
}

void dealerbj_chk(int bj)
{
	if(bj == 21)
	{
		cout << "Winner Winner, Chicken Dinner. Blackjack!" << endl;

		dealer += 15;
	}
}

// Loops to continue playing.
void play_again()
{

	char choice;
	cout << "Care for another game?" << endl;
	cin >> choice;

	choice= toupper(choice);

	switch(choice)
{
		case 'Y':
			p_total = 0;
			d_total = 0;
			cout << "New game starting now..." << endl;
			cout << endl;
			break;
		case 'N':
			cout << "Thanks for playing.." << endl;
			playing = false;
			break;
		default:
			cout << "Invalid selection" << endl;
			play_again();
			break;
	}
}



if you input the following sequence youll see what i mean:
number: 3597131
hit
hit
hit (used to check a player busting)
yes
stay
yes
here it should draw A and J
closed account (j3Rz8vqX)
Now that I really look at it, there are some interesting concerns.

First let us recognize your array, illustrated with: index:value:face(if any)
1
2
3
4
5
6
7
8
9
10
11
12
13
    for(int i = 0; i < 13; i++) //allocates card values to array
    {
        cards[i][0] = i + 1;
        cards[i][1] = 4;//You never use index [card][1], although assigned 4.
    }
0:1:A
1:2:-
2:3:-
3:4:-
4:5:-
5:6:-
6:7:-
7:8:-
8:9:-
9:10:-
10:11:J
11:12:Q
12:13:K


I've condensed your face check function to 3 comparison:
1
2
3
4
5
6
7
8
9
void fc_chk(int f) // checks for face cards
{
    if(f==1)                    //case 1: is 'A';
        cout<<face[3];
    else if(f>10)               //case 2 is {'J','Q','K'};
        cout<<face[f-11];       //11-11=0(J),12-11=1(Q),13-11=2{K};
    else
        cout<<cards[card][0];   //Print out the regular members.
}

I had noticed fewer and fewer face cards as the game progress.

The result was because the below would permanently change the values within the array:
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
void val_chg(int val) // changes calue of J Q K to 10
{	
	if(val == cards[0][0])
	{
		ace_chk(cards[0][0], p_total);
	}
	if(val == cards[10][0])
	{
		cards[10][0] = 10;
		
	}
	if(val == cards[11][0])
	{

		cards[11][0] = 10;
	}
	
	if(val == cards[12][0])
	{

		cards[12][0] = 10;
	}
	else
	{
		cards[card][0] = cards[card][0];
	}
}
If first: A=11 (future J - there goes your J!<permanently>)
If second:A=1 (itself again - which is okay)

J=10 (J will never again become present - unless A creates its value!)
Q=10 (Q will never again become present)
K=10 (K will never again become present)

They will eventually all cease to be faced cards and add to the 10 pool.

A will remain itself, if the second card, or change into J.
And J,Q,K will become 10.
So all of the values will eventually become 10.


A solution would be to not change the values of the card, but change how the cards are interpreted before being accumulated.

Or this:
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
//Function prototypes:
int val_chg(int);                       //Modified to return an integer
int ace_chk(int, int);                  //Modified to return an integer

//Main function: (game-loop)
int main()
{
    //...Stuff...
    while(playing)
    {
        deal();                         //deals player cards
        fc_chk(cards[card][0]);         //checks for face cards
        p_calc(val_chg(cards[card][0]));//get the values of the cards and call p_calc with its value

        cout << " and ";

        deal();                         //deals player cards
        fc_chk(cards[card][0]);         //checks for face cards
        p_calc(val_chg(cards[card][0]));//get the values of the cards and call p_calc with its value

        cout << " which totals: " << p_total << endl;

        playerbj_chk(p_total);          //Check player hand for instant win!

        if(playing)                     //if(true)
            move();                     //Prompt user for hit/stand options
        if(playing)                     //if(player did not bust)
        {
            deal();                     // deals dealer cards
            fc_chk(cards[card][0]);     // checks for face cards
            d_calc(val_chg(cards[card][0]));    //...for dealer

            cout << " and ";
            deal();                     //deal ... for dealer
            fc_chk(cards[card][0]);     //check ... for dealer
            d_calc(val_chg(cards[card][0]));    //...for dealer
            cout << " Which totals: " << d_total << endl;

            dlr_chk(d_total);           //algorithms for dealer hit/stand
        }
        winner(p_total, d_total);
        playing = true;
        play_again();
    }
}

//Function declarations:
int val_chg(int val) // changes calue of J Q K to 10
{

	if(val == cards[0][0])
	{
		return ace_chk(cards[0][0], p_total);//returns ace_chk's returned value.

	}
	else if(val>10)         //J or greater!
    {
        return 10;//return 10 for any value greater than 10.
    }
	else
	{
		return cards[card][0];//return the cards value
	}
	return 0;//default
}

int ace_chk(int a, int t) // checks Ace and changes value depending on total
{

	if(a == cards[0][0] && t <= 11)
	{
		cout << "X ";
		return 11;    //return 11 to the calling procedure
	}
	else if(a == cards[0][0] && t > 11)
	{
		cout << "Y ";
		return 1;    //returns 1 to the calling procedure
	}/*
	else
	{
 		cards[card][0] = cards[card][0]; //Will never happen; already validated
	}*/
	return 0;//default
}


There can be other approaches, such as resetting the array before every deal, but the above is what I chose.

Also, when things have all setting down, you may want to rethink your 2 dimensional array.

My point is that the second index of the 2d-array is never being used; you literally don't need it unless you are planning to keep of how often cards appear ^^.

Have a good day.
Last edited on
closed account (j3Rz8vqX)
What I'm getting at:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i = 0; i < 13; i++) //print data
    {
        cout<<"cards["<<i<<"][0]: "<<cards[i][0]<<endl;
    }
//May eventually produce:
0:10:A
1:2:-
2:3:-
3:4:-
4:5:-
5:6:-
6:7:-
7:8:-
8:9:-
9:10:-
10:10:J
11:10:Q
12:10:K

Therefore, you can still draw card(s), the indexes randomed, of values 1, 10,11, and 12 but they will never display their correct face card and will all eventually become 10s.

Hence why you saw J in the A zone.

It wasn't that J was in the A zone, but rather A had become 11, which was defined by the face check function to be J.
Last edited on
Topic archived. No new replies allowed.