BlackJack Program Setup

So I have recently received a new assignment in class to create a blackjack program. I have been out for quite a few classes and it's affected my ability to do this program. I struggle greatly in C++ and i'm trying my best, (i have a B right now) and i don't want to fail this because this program is worth a lot of points. I know most of you guys will be like "we don't do homework for free" and all that stuff, but i honestly need help. And unlike other people who post their programming assignments waiting for someone to write their program for them, i actually spent a lot of time on this but i simply cant get it. Now i'm not asking for someone to write the program for me. But merely for someone to point me in the right direction and give me a few tips.

The assignment is as follows:

Two cards are dealt to each player. The dealer shows one card face up, and the other is face down. The player gets to see both of his or her cards and the total of them is added. Face cards (Kings, Queens, Jacks) are worth 10 points, Aces are worth 1 or 11 points, and all other cards are worth their face value. The goal of the game is to get as close to 21 (“blackjack”) without going over, which is called “busting.”

The human player goes first, making his or her decisions based on the single dealer card showing. The player has two choices: Hit or Stand. Hit means to take another card. Stand means that the player wishes no more cards, and ends the turn, allowing for the dealer to play.

The dealer must hit if their card total is less than 17 (or a soft 17), and must stand if it is 17 or higher.

Whichever player gets closest to 21 without exceeding it, wins.

Requirements

For this assignment you need to do the following:
• Write a program that plays Blackjack

• Have the program use at least 3 functions:
1. For the Dealer
2. For the Player
3. To deal a card

• Have the program intelligently determine if an Ace should be interpreted as a 1 or an 11. This is somewhat difficult. You need to be able to also handle multiple aces. If there are any aces in the hand, and the total exceeds 21, change the 11 to a 1 (i.e. subtract 10) until there are no more aces in the hand or the total is below 21. (Hint: keep a counter that says how many aces have been dealt so far.)

• You don’t need to be able to deal from a real deck. Just generate a random number, where 1 is an ace and 10, 11, 12, and 13 represent 10 as well. (To get the right distribution of cards.) You can ignore suit.

Grading

This is a difficult project that you should concentrate on correctness and readability. This code will be very difficult to follow without proper commenting. Please take time to make the user’s interface as readable and easy to understand as possible. I will grade for correctness, readability, comments, and a separate grade for appearance of output screen.
YOU MUST USE PASS BY REFERENCE PARAMETERS OR YOU WILL NOT RECEIVE FULL CREDIT

You may use the following code to generate a random integer:

// postcondition: returns a random integer from 1 to 13
int getRand()
{
if(i == 100) i = 0;
int rands[100];
srand((unsigned)time(0));

for(int index=0; index<100; index++)
{
rands[index] = (rand()%13)+1;
}

i++;

int n = rands[i];
return n;
}


To use this function, insert the following lines after your include directives:

static int i = -1;

int getRand();


and include the ctime library

Programming concepts needed to complete this project:

1) User input
2) Using functions
3) Control Structures
4) Conditional statements
5) Advanced tracking variable values
6) Boolean statements


Now so far, this is what i have set up. I know it's probably nothing but i wanna show i actually tried.


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
  #include<iostream>
#include<ctime>
#include<string>
using namespace std;

void setup();

void output();

int dealACard();

int getRand();
static int i = -1;

int main()
{
	output();
	setup();
}

void output()
{
	string name;
	cout << "Welcome to blackjack \nPlease enter your name: ";
	cin >> name;
	cout << "\n";
}

void setup()
{
	int total, d1, p1, p2;
	char choice;
	
	d1 = getRand();

	cout << "The dealer: " << endl;
	cout << "?" << " + " << d1 << "\n" << endl;
	
	p1 = getRand();
	p2 = getRand();

	total = p1 + p2;

	cout << "You: " << endl;
	cout << p1 << " + " << p2 << " = " << total << endl;

	cout << "\n";

	cout << "Do you want to hit (h) or stand (s)?: ";
	cin >> choice;
}


int getRand()
{
	if(i == 100) i = 0;
	int rands[100];
	srand((unsigned)time(0));

	for(int index=0; index<100; index++)
	{ 
		rands[index] = (rand()%13)+1; 
	}

	i++;

	int n = rands[i];
	return n;
}
Last edited on
First of all, the getRand() function your instructor provided seems really strange. He said that you MAY use the one he/she provided, but you don't have to. Therefore, I'd like to suggest a better and more intuitive alternative, which looks like this:

1
2
3
4
5
6
7
8
9
10
#include <ctime>

int random(int min, int max) {
	static bool random=false;//Static bool which exists past the scope of this function. Holds the state of whether srand has been seeded.
	if(random==false) {
		random=true;//The code in this if control structure is guaranteed to only execute once - the first time it's called.
		srand(time(0));//That is, if you don't tamper with 'random'
	}
	return (min+(rand()%(max-min+1)));
}


And here's the main game loop to get you started. It's just pseudo-code comments:

1
2
3
4
5
6
7
8
9
10
11
12
int main() {
	bool done = false;
	while(!done) {
		//1.)Remove all the cards from each players hand
		//2.)Add two cards to each players hand, with a random face value in the range of 2 - 11. Ensure even distribution of cards since K, Q, and J are worth 10 points
		//3.)Display one of the dealers cards, as well as your own
		//4.)I'm assuming you're just doing a two player game (user against dealer AI). In this case, ask the user if they want to hit or stand. Make sure that input is valid.
		//5.)Check winning conditions
		//6.)Display who won, ask the user if they would like to play again. If they don't, done = true;
	}
	return 0;
}
Last edited on
We have not done the getRand() function in class yet. Hence the reason my teacher provided it to me. I have to use that one or else my teacher will know i used online help in my program.

I appreciate all the help, but honestly i'm in the dark here. If you could, please explain to me in detail. I will be eternally grateful to you man.
For the purpose of this function, I'm going to assume you represent figures with the numbers from 11 to 13 and the ace is the number 1. You should use an array to represent a hand of cards. This way, you can make a function like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int smartTotal(int hand[], int size){
    int total = 0;
    int nbOfAces = 0;
    
    for(int i=0; i<size; i++){    // for each card in the hand
        if(hand[i] == 1)
            nbOfAces++;           // count the amount of aces to add them later
        else if(hand[i] > 10)
            total += 10;          // figures have a value of 10
        else
            total += hand[i];
    }

    while(nbOfAces != 0){     // for each ace
        nbOfAces--;
        if((total + 11 + nbOfAces) <= 21)
            total += 11;
        else
            total++;
    }
    return total;
}


Here's an example of what the last loop does. Let's say you have 2 aces and a 2 in your hand. Before this loop, total is equal to 2 and nbOfAces is 3.

The loop is going to test if 2+11+1 is too big to win (total before counting the aces + first ace considered as 11 + other aces considered as 1s). 14 is below or equal to 21, so we'll consider the first ace as an 11 which we add to the total value. We remove the other ace and perform the same test. 13+11+0=24 which is over 21, so we won't consider the second ace as 11, we'll just add it as a 1. Total is now 14 and the nbOfAces is 0.

That should get you started :)
Last edited on
@pivottt

Ohh, so that's the function i should use for the aces? Basically it'll decide what the ace should be, right? Oh my god thank you so much. You just solved the hardest part of the program for me!

Here's the problem, the characters [] used in line 1, haven't been taught to us in class yet. So if my teacher sees that, he'll know that i got it online. Could you make the program work without those characters?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int smartTotal(int *hand, int size){
    int total = 0;
    int nbOfAces = 0;
    
    for(int i=0; i<size; i++){    // for each card in the hand
        if(*(hand + i) == 1)
            nbOfAces++;           // count the amount of aces to add them later
        else if(*(hand + i) > 10)
            total += 10;          // figures have a value of 10
        else
            total += *(hand + i);
    }

    while(nbOfAces != 0){     // for each ace
        nbOfAces--;
        if((total + 11 + nbOfAces) <= 21)
            total += 11;
        else
            total++;
    }
    return total;
}


Though that probably isn't much better, just get the idea from the algorithm and work out another way of doing it. That way you also learn something!
Just to follow up, and for reference, here is the getRand() function your instructor provided:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int getRand()
{
	if(i == 100) i = 0;
	int rands[100];
	srand((unsigned)time(0));

	for(int index=0; index<100; index++)
	{ 
		rands[index] = (rand()%13)+1; 
	}

	i++;

	int n = rands[i];
	return n;
}


I find this function strange because:
1.) Every time you call the function, you allocate an array of 100 integers. Whatever.
2.) Every time you call this function, you reseed the pseudo-random number generator, which is bad.
3.)Then, you fill the array with random values.
4.) Then, you increment the global static integer i
5.) Finally, you use i as an index to access and return the element in that location of the array.

This is stupid, because the seed for the pseudo random number generator will always be the same when the function is called, which means the unnecessarily non-static array gets filled with THE SAME NUMBERS every time. Additionally, if you use more than 100 numbers, you start at the beginning again.

I know you're thinking that using my method would look suspicious to your teacher, but I don't see why it should be. My method makes use of a static variable, like your instructors. It seeds the pseudo-random number generator (only once though, unlike your teachers), and returns actual pseudo-random numbers, as opposed to precomputed pseudo-random numbers. Not that all of this is a big deal, but you know, whatever - I forgot where I was going with this.
Last edited on
An array is a set of data of the same type written one after another in the memory. The [] characters are used to specify which element of the array you want to access, 0 being the first.

In your game, the greatest amount of cards a player can have in his hand is 21 (all aces), so you can declare an array of integers with this line in the main:
int hand[21];

You can also initialize an array to certain values like this :
int something[3] = {1, 2, 3};
Which would create an array containing 1, 2, 3 at the positions 0, 1 and 2.

You can also fully initialize an array with zero's with this syntax:
int hand[21] = {0};
Which is what you want in this case. A zero means no card.

Not using arrays would make this program a lot longer to make. Your teacher probably won't mind if you took the time to search and ask for help online as long as you tried and made the program. Because I don't really see how you would represent the player's hand if not this way - I don't think you want to make 21 variables, one per possible card.

Arrays are especially helpful when combined with for loops. If you want something to happen for each element of the array (in your case, each card of the hand), you just use this for loop:
1
2
3
for(int i=0; i<sizeOfArray; ++i){
    // do stuff with array[i]
}
Last edited on
@pivottt

Thanks a lot for explaining all that man. I really appreciate it. But if i tell my teacher i used online help, he'll closely look at the rest of my program and think i cheated and that's the last thing i want. Would you be able to talk to me elsewhere? Maybe email me so i can talk to you? I would really appreciate your help in some things.
Topic archived. No new replies allowed.