Skip to main content
GameDev.net gamedev.net
🔒 Locked

[C++] Need help with random numbers

Started by Killinger Apr 9, 2005 at 10:22 PM 6 replies 1.3k views
Original Post
Killinger
Killinger
This game is kind of like BlackJack, but with nothing fancy. Just random number 1-12 and you can hit or stand. Trying to get better than computer, who can get between 18 and 21. Most code is obvious, so I won't comment much. First card is always 0. Any help there? Also, the program won't add the cards correctly. I don't know what I should do to get that achieved. Any help is appreciated.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int card, currentcard, all, house, finish;
char hit;

int getcard()		//getcard function
{
	srand(time(NULL));
	currentcard = rand() % 12 + 1;	//getting random numbers
	all = all + currentcard;		
	house = rand() % 4 + 18;
	return 0;

}
int main()
{
	all = 0;
	all = getcard();
	cout << "Welcome to 21!\n\n";
	cout << "Your first card is " << all << "\n";
	do
	{
		cout << "Would you like to (H)it or (S)tand?\n";
		cin >> hit;
		if (hit == 'h')
			{
				getcard();
				cout << "Your card was " << currentcard << "\n";
				cout << "Your sum is " << all << "\n";
				if (all >= 21)
				{
					goto end;
				}
			}
	}while (hit == 'h');

end:
{
	cout << "The house had " << house << "\n";
	if (house == all)
		{
			cout << "House wins ties...sorry!\n";
		}
	else if (all > 21)
	{
		cout << "You busted! House wins...\n";
	}
	else if (house >= all)
		{
			cout << "You lose!\n";
		} 
	else
		{
			cout << "You win!\n";
		}
	return 0;
}

	return 0;
}




Thanks alot! [Edited by - Killinger on April 9, 2005 11:10:46 PM]
Fahrenheit451
Fahrenheit451
maybe before this line:

cout << "Your first card is " << card << "\n";


you should make the assignment to the card variable with a call to getcard()

card = getcard();cout << "Your first card is " << card << "\n";
choffstein
choffstein
	srand(time(NULL));	currentcard = rand() % 12 + 1;	//getting random numbers	all = card + currentcard;			house = rand() % 4 + 18;	card = rand() % 12 + 1;


The reason things are not adding up right is because you are saying "all" = "card" + "currentcard". Okay, lets just trace through it. We begin. Card is assigned by the system to some totally random number, because you never set it as anything. Same with current card.

Luckily, current card is overwritten by the rand() call. So now we have ALL equaling our current card plus some totally random number. Next, we hit for the house? Thats legit...I suppose. Normally the house might stand, but hey, that can be implemented later.

Then you do something totally wack...you do card again? What is the purpose of the card variable? You have a variable for the total sum of the player cards, and the current card...so what is the variable "card" for? Why not just remove it all together, and have "all = all + currentcard"? Make sure you set all = 0 at the beginning of the program...

Here is what I would recommend.

	srand(time(NULL));	currentcard = rand() % 12 + 1;	//getting random numbers	all = all + currentcard;			house = rand() % 4 + 18;


Now, before this line:
cout << "Your first card is " << card << "\n";

You are going to want to say
all = 0;all = getcard();cout << "Your first card is " << all << "\n";


That should do ya right.
void*
void*
Quote:

...
int main()
{

cout << "Welcome to 21!\n\n";
cout << "Your first card is " << card << "\n";
...


take a closer look, you don't set the value of card before outputting it for the first time. You should call getcard before outputting "Your first card is..."

Oh, and you should initialize your other values, too. For the moment it may seem to work because you're probably compiling in debug mode, and most compilers will do the variable initialization to zero for you when in debug mode. But if you were to switch to compiling in release mode, it is likely that these values won't be initialized, and you'll start wondering why you have strange (and probably very large) values for card, all, house, etc.
Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."
Zahlman
Zahlman
And after you've fixed all that, also move the srand() call to the beginning of the program, not the beginning of getcard(). You do not want to re-seed the random number generator each time; that destroys the effective randomness.
Zahlman
Zahlman
Quote:
Original post by Killinger
I modified the code to what I have now. Everything is all good except for the first number is always 0. I have tried putting getcard(); at the beginning of main, but it just doesn't work (compiles, but sum is always 0).


Nononono. srand(time(NULL)) comes out of getcard() and goes into main(). getcard() stays where it is, and main() calls it as it did.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.