Blackjack random numbers

I am a beginer and I want to have a code with a random number generator, but my code does not follow what I would like it to follow.... PLEASE HELP! . This is what have so far... please help with any corrections, improvements, etc.


#include <iostream>;
#include <cstdlib> //drand function
#include <ctime> // time function
#include <Windows.h> //Sleep function
using namespace std;

/* *****************
Oscar, Zamora
1/20/13
This program was made
so that people can
understand and
experience how video
games are made using
the language of C++
***************** */

//start( float radius); //Function Prototype
//float volumes( float radius); // Function Prototype

int main()

{

int num;
char choicei = 'i';
char choiceh = 'h';
char choices = 's';
char choicec = 'c';
char choiceb = 'b';


// Suspend program for 2 seconds

Sleep(2000);
//seed random number ganerator using the system clock
srand(static_cast<unsigned int> (time(0)));
//generate a random number between 1 and 13
num = rand() % 13 + 1;




// Welcome user to the introduction screen

cout << " BLACKJACK\n\n\n";
cout << " Thank youdsfafafd for playing this game!\n\n";
cout << " Press 'b' to Start the game\n";
cout << " Press 'i' for intructions on the game\n";
cout << " Press 'c' for credits on the game\n";

// Show the instructions

if (choicei == 'i')

{

cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";

}

if (choicec == 'c')

{

cout << " The writer, producer, developer, and artist of this game is Oscar Zamora";

}

if (choiceb == 'b')
{
do
{

//Tell the user what card they got

cout << " You got" << num;

// Add up all the user"s cards and print

// Ask them if the y want to hit or stay

cout << " Would you like to get hit ('h') or stay ('s')";

//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

}while (choices == 's');
}
}
I'm having trouble reading the code without the code brackets (use the <> button beside your post for that).

The trick with just blindly implementing a random number generator (like rand() in <cstdlib>) is that you need to ensure that you don't draw the same card twice.

What I like to do is make a deck of cards. I use a "Card" class which has information about the suit and rank, then I add each card to the deck (I use a for loop to iterate through each rank and suit). The deck is a std::vector<Card>.

Now that I've got all of the cards in my deck, I use std::random_shuffle() to randomize the deck. Now I just take cards off of the top of the deck without hassle!
closed account (3qX21hU5)
Not sure what you are having problems with.

It looks to me that you still have some more work your going to do on the program, but here is some things I just noticed.

1
2
3
4
5
cout << " BLACKJACK\n\n\n";
cout << " Thank youdsfafafd for playing this game!\n\n";
cout << " Press 'b' to Start the game\n";
cout << " Press 'i' for intructions on the game\n";
cout << " Press 'c' for credits on the game\n";


You got a menu screen working, but you don't let the user input any selections. You are going to need a cin >> MenuChoice in there to determine what if statement to run.

Now I can't really offer anymore advice other then what stewbond told you since I don't really know what you are having trouble with other then random number generation. But it looks like you are on the right track.
I'ts that once I do get into my main coding... all of the coding pops up and repeats itself continuously without restraint... what should I do?
closed account (3qX21hU5)
It repeats itself because of this.

1
2
3
4
5
char choicei = 'i';
char choiceh = 'h';
char choices = 's';
char choicec = 'c';
char choiceb = 'b';


Lets look at the first if statement

1
2
3
4
5
6
7
if (choicei == 'i')

{

cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";

}


This basically means that if choicei is 'i' it executes the code in the brackets. Now notice how you set choicei to i already char choicei = 'i'; so it will execute the statement no matter what. And the same goes for every other one. So you need to just have one variable to hold the letter the user inputs

Like this

1
2
3
4
5
6
cin >> input;

if (input == 'i')
{
    // Do whatever you want i to do
}


and so on
Okay. That seems to be working, but my do-while loop keeps repeating itself when called on. Here's my code... can you see what's wrong?

#include <iostream>;
#include <cstdlib> //drand function
#include <ctime> // time function
#include <Windows.h> //Sleep function
using namespace std;

/* *****************

1/20/13
This program was made
so that people can
understand and
experience how video
games are made using
the language of C++
***************** */

//start( float radius); //Function Prototype
//float volumes( float radius); // Function Prototype

int main()

{

int num;
int menuchoice;
char input;
float bet = 's';


// Suspend program for 2 seconds

Sleep(2000);
//seed random number ganerator using the system clock
srand(static_cast<unsigned int> (time(0)));
//generate a random number between 1 and 13
num = rand() % 13 + 1;




// Welcome user to the introduction screen

cout << " BLACKJACK\n\n\n";
cout << " Thank you for playing this game!\n\n";
cout << " Press 'b' to Start the game\n";
cout << " Press 'i' for intructions on the game\n";
cout << " Press 'c' for credits on the game\n";


// Show the instructions
cin >> input;

if (input == 'i')

{
cout << " The game you will be playing is called 'Blackjack' or 'twenty-one'.\n This is a multiplayer games in which one of the players want to get cards";

}

cin >> input;

if (input == 'c')

{

cout << " The writer, producer, developer, and artist of this game is";

}

cin >> input;

if (input == 'b')
{
do
{

//Tell the user what card they got

cout << " You got" << num;

// Add up all the user"s cards and print

// Ask them if the y want to hit or stay

cout << " Would you like to get hit ('h') or stay ('s')";

//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

}while (bet == 's');
}
}
closed account (3qX21hU5)
Yup right now you are kind of doing the same thing as before but with your bet variable. while (bet == 's'); notice above that you initialized bet to 's' float bet = 's';. Now the first thing wrong with this is that you are initializing a float which is ment to hold number with decimals (or whole numbers) like 1.33 but not characters. So you are going to want to change that to something else.

Now since this looks like it is suppose to keep track of when the user is getting there card you might want to consider changing it to a bool variable like bool bet = false;.

That way you can do somethign like 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
do
{

//Tell the user what card they got

cout << " You got" << num;

// Add up all the user"s cards and print

// Ask them if the y want to hit or stay

cout << " Would you like to get hit ('h') or stay ('s')";

cin >> StayOrBet;

// This is the important part if the user chooses 's' change bet to true so it knows to continue to the computers hand.
if (StayOrBet == 's' || StayOrBet == 'S')
{
    bet = true;
}

//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

}while (bet == false);


Boolean variables can either be true or false and in this case it is probably the best choice. What is does is basically if the user enters 's' or 'S' it changes the bet variable to true and when we change it to true this is not longer correct while (bet == false); so it exits the loop.
Last edited on
Thanks Zereo!!! My problem has been fixed, but now I face the problem of saying what the total amount they have gotten.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	// Let Player 1 go first

			cout << " Player 1's turn!!!";

			// Tell the player what card they got

			cout << " You got" << num;

			// Add up all the user"s cards and print

			

			// Ask them if the y want to hit or stay

			cout << " Would you like to get hit ('h') or stay ('s')";

			//If they want to hit, loop back to generating a random number again otherwise let the computer get a hand

			cin >> choice;

			}while (choice == 'h');

In the "Add up all the user's cards and print" the total is supposed to be 'num', but num has to be used for several different numbers, so how do I add all those 'nums' to make a 'total' ?
closed account (3qX21hU5)
Ok this is actually quite simple to do. What you need to do is declare a new variable to hold the total for that hand. What you will need is a integer variable to hold all the total of all the cards the player has drawn. So create a new int total = 0; outside of the the do-while loop.

But anyways once you have the new variable to hold the total all you have to do is keep adding whatever card the player has drawn to that total.

Here is a quick outline of what the do-while loop can look like.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int p1Total = 0;

do
{
    char choice;

    // You need to have the rand() inside the loop so it generates a new number ever time
    num = rand() % 13 + 1;

    // Tell the player what card they got
    cout << "You got a " num;

    // Add the card to the total and print
    p1Total += num;
    cout << "You have " << p1Total << endl;

    // Ask them if they would like to stay or hit
    cin >> choice;

    // Then whatever else you want it to do.

}while (choice == 'h');


Now just remember that once you start a new game or a new hand you will have to set the total variable to 0 again or else you will still have the total from the last hand in there.
Last edited on
Topic archived. No new replies allowed.