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

Blackjack (a C++ issue)

Started by snowfell Jun 5, 2008 at 4:40 PM 12 replies 4.3k views
Original Post
snowfell
snowfell
I am trying to make a simple blackjack game just for some practice. I have a simple issue with my game so far and I could use some help. In my current code I need the first to set up a randomizing variable (currently all I have is that you input a number and it moves on, as if you drew that). But even more importantly I can't seem to remember how to get my program to loop back around again. If you guys seen any other problems with my game so far please let me know: #include using namespace std; int main() { int score=0; int current=0; int add=0; int close; cout << "BLACKJACK \n"; if (current < 21) { cin >> add; current = current + add; cout << "your current number:" << current; cout << "\n"; if (current > 21) { cout << "You Lose"; } else if (current = 21) { cout << "You Win"; score = score + 1; } } //terminate the program cin >> close; cin.get(); }
SirKnight
SirKnight
To have your game loop back again, use just that a loop. Any loop would work, but in this case I'd probably use a do { } while(); loop. Make the condition of the while false if the user wants to exit.

For random numbers, use the rand() function. See here: http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

That page also has a number guessing example that allows the user to replay the game just like you want.
agm_ultimatex
agm_ultimatex
just put a simple do while loop around the code. Your game is very very simple obviously. A blackjack game i dealt with in c++ class had about 8 classes in it.

this should work.
#include <iostream>using namespace std;int main(){string question;do{int score=0;int current=0;int add=0;int close;bool keepPlaying = true;cout << "BLACKJACK \n";if (current < 21){cin >> add;current = current + add;cout << "your current number:" << current;cout << "\n";if (current > 21){cout << "You Lose";}else if (current = 21){cout << "You Win";score = score + 1;}}cout << "Play again? y/n" << endl;cin >> question;}} while(question == "y" or question == "Y");//terminate the programcin >> close;cin.get();
SirKnight
SirKnight
Don't use string for this. Use a single char.

It's also better for a beginner to avoid using those C++ classes like string and use character arrays instead for strings. They will learn a lot more and become better programmers by doing that. A long time ago, that's what my programming teachers said and I now understand why.

Plus, it's faster to do if( ch == 'y' ) assuming ch is a char than it is doing if( ch == "y" ) if ch is of the string class type.
agm_ultimatex
agm_ultimatex
Quote:
Original post by SirKnight
Don't use string for this. Use a single char.

It's also better for a beginner to avoid using those C++ classes like string and use character arrays instead for strings. They will learn a lot more and become better programmers by doing that. A long time ago, that's what my programming teachers said and I now understand why.

Plus, it's faster to do if( ch == 'y' ) assuming ch is a char than it is doing if( ch == "y" ) if ch is of the string class type.


I have to disagree. The string class is great. I never programmed in C, but I know about c-strings, or char[]. A string works fine for this and obviously ouputing information in more complex terms later.
agm_ultimatex
agm_ultimatex
Quote:
Original post by SirKnight
Don't use string for this. Use a single char.

It's also better for a beginner to avoid using those C++ classes like string and use character arrays instead for strings. They will learn a lot more and become better programmers by doing that. A long time ago, that's what my programming teachers said and I now understand why.

Plus, it's faster to do if( ch == 'y' ) assuming ch is a char than it is doing if( ch == "y" ) if ch is of the string class type.


I have to disagree. The string class is great. I never programmed in C, but I know about c-strings, or char[]. A string works fine for this and obviously outputing information in more complex terms later.
snowfell
snowfell
I am a little confused. First before I ask about a new game I need to ask then if they want to hit or not (a bit lost at this step). Then I can worry about starting a entirely new game. Guys I was really hoping for a simple answer, this is only meant to be a simple warmup example. I would rather not use strings or classes unless they are needed.
agm_ultimatex
agm_ultimatex
Blackjack is a complicated game. There card face values, suits(or atleast a way to keep track of 4 types of each card). Wether a card is face up or down. Many others. The book beginning c++ game programming has a blackjack source code in it. I would send you my altered version as I did a couple exercises, and split the classes into separate files. But I think that would break the laws of utilizing the source code.
snowfell
snowfell
What I have in mind is very simple. All I want to do is make it so that you start with a random number (ten or less), then it simply asks "Hit?". If you say yes it adds a random number (ten or less) onto the current one. It keeps doing this till you say no, the number equals 21, or the number is over 21. Then it tells you how many wins and losses you have, then restarts current number. Last time I check that isn't that difficult. I don't plan on doing any GUI's or anything, just a short and sweet little mini game.
snowfell
snowfell
Ok I kindof got this going for me here. I have a working loop but whenever you add the first number it automaticly = 21 and it says you win. Then all the inputs work fine. Here is what I have so far:

#include
using namespace std;

int wins=0;
int loses=0

int current=0;
int add=0;
int close;
int running=0;

int main()
{
cout << "BLACKJACK \n";

for(;;)
{
cin >> add;
current = current + add;
add=0;
cout << "your current number:" << current;
cout << "\n";

if (current > 21)
{
cout << "\n You Lose";
loses = loses + 1
}

else if (current = 21)
{
cout << "\n You Win";
wins = wins + 1;
}
}
//terminate the program
cin >> close;
cin.get();
}
grekster
grekster
else if (current = 21)

should be

else if (current == 21)


= is an assignment
== is an equality test
Quote: Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
theOcelot
theOcelot
You did this:

if (current = 21){cout << "You Win";score = score + 1;}

You need to do this:
if (current == 21)//notice double '=': '=='{cout << "You Win";score = score + 1;}


You used the assignment operator, which assigned 21 to current, and then returned 21 the 'if' statement! The assignment operator returns whatever was assigned, in this case 21, which evaluates to true.

Always use the equality operator in an if, unless you exactly know what you're doing.
snowfell
snowfell
ah right forgot. I am still getting premature win messages. I don't really know why looking back at the source code. This is the main problem I am currently having, fixing this will unlock a lot of doors.

Topic Locked

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

Sign in to reply to this topic.