Sign in to follow this  
Shruubi

problem with my blackjack game

Recommended Posts

so i decided to do a little skills test and write a blackjack game in c++, i have no compiling errors and everything compiles up dandy, however..... when the console screen pops up, it just sits there with a flashing cursor not actually printing out anything, and i can't for the life of me see my error (although i would bet my left foot i made a pretty glaringly obvious one.)

any help would be appreciated.

[code]
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int draw(void) //draw function
{
int Rand_Num;
Rand_Num = rand() % 10 + 1;
return Rand_Num;
}

int main()
{
int player_total = 0;
int opp_total = 0;
char choice;
char choice_end = 'y';

srand ( time(NULL) ); //seed random num generating

do //game loop
{
//reset game vars
player_total = 0;
opp_total = 0;
for(opp_total = 0; opp_total > 17 && opp_total < 21; opp_total = draw())/*opp sits if
between 17 and 21, nifty way of doing AI i thought */
{
if(opp_total > 21) //if opponent draws over 21
{
break;
}
else //continue with game
{
cout << "draw or sit? (d or s) "; //draw or sit
cin >> choice;
if(choice == 'd')
{
player_total += draw(); //add drawn to player total
cout << "your total is " << player_total << endl;
if (player_total > 21 || player_total == 21) /*end game draw/sit phase
if player has more than 21 or has 21 */
{
break;
}
}
else //if if player chooses sit
{
cout << "you sit with a total of " << player_total << endl;
break;
}
}
if((player_total <= 21 && player_total > opp_total) || (opp_total > 21))
//player win conditions
{
cout << "you win! do you wish to play again? (y or n) ";
cin >> choice_end;
}
else if((opp_total <= 21 && opp_total > player_total) || (player_total > 21))
//player lose conditions
{
cout << "you lose! do you wish to play again? (y or n) ";
cin >> choice_end;
}
else if(player_total == opp_total)
//draw condition
{
cout << "it's a draw! do you wish to play again? (y or n) ";
cin >> choice_end;
}
}

} while (choice_end == 'y'); //end game loop

return 0;
}
[/code]

Share this post


Link to post
Share on other sites
I think your problem is here:
[quote name='Shruubi' timestamp='1307799633' post='4822070']
[code]
for(opp_total = 0; opp_total > 17 && opp_total < 21; opp_total = draw())/*opp sits if
[/code]
[/quote]
Notice the loop invariant is immediately false (0 > 17 && 0 < 21) and so execution never enters inside your for loop. Thus the statement: opp_total = draw() never executes as well.

Share this post


Link to post
Share on other sites
[quote name='loom_weaver' timestamp='1307800197' post='4822072']
I think your problem is here:
[quote name='Shruubi' timestamp='1307799633' post='4822070']
[code]
for(opp_total = 0; opp_total > 17 && opp_total < 21; opp_total = draw())/*opp sits if
[/code]
[/quote]
Notice the loop invariant is immediately false (0 > 17 && 0 < 21) and so execution never enters inside your for loop. Thus the statement: opp_total = draw() never executes as well.
[/quote]

ok, so i changed the logic too:

[code]
for(opp_total = 0;(opp_total < 21) || (opp_total > 17 && opp_total < 21); opp_total += draw())
[/code]

that works, but running it, nothing really seems to work as it should

-choosing sit does not cause the player to sit
-choosing n when asking to play again does not close the loop
-it seems like choosing draw causes the game to go to instant check if player won or lost

i keep reading over the code and everything seems to be where it should be, so i don't understand why it doesn't work....

Share this post


Link to post
Share on other sites
[quote name='Shruubi' timestamp='1307801143' post='4822078']
that works, but running it, nothing really seems to work as it should

-choosing sit does not cause the player to sit
-choosing n when asking to play again does not close the loop
-it seems like choosing draw causes the game to go to instant check if player won or lost

i keep reading over the code and everything seems to be where it should be, so i don't understand why it doesn't work....
[/quote]

Personally, I wouldn't use a for loop for your inner loop. Instead I would do something like:

[code]
int hand_finished = 0;
while (!hand_finished) {
opp_total += draw();
...

// at the bottom of the loop determine if the hand is finished i.e. someone busted or won
}[/code]

Add lots of println statements too and use your debugger so that you can following the flow of the code.

Share this post


Link to post
Share on other sites
[quote name='loom_weaver' timestamp='1307801830' post='4822087']
[quote name='Shruubi' timestamp='1307801143' post='4822078']
that works, but running it, nothing really seems to work as it should

-choosing sit does not cause the player to sit
-choosing n when asking to play again does not close the loop
-it seems like choosing draw causes the game to go to instant check if player won or lost

i keep reading over the code and everything seems to be where it should be, so i don't understand why it doesn't work....
[/quote]

Personally, I wouldn't use a for loop for your inner loop. Instead I would do something like:

[code]
int hand_finished = 0;
while (!hand_finished) {
opp_total += draw();
...

// at the bottom of the loop determine if the hand is finished i.e. someone busted or won
}[/code]

Add lots of println statements too and use your debugger so that you can following the flow of the code.
[/quote]

that actually fixed my problems, thank you! :D

however, a new problem has emerged, this one i think people will find a little more interesting. the draw() function will give the same value to both the player and opponent so the game will always be a draw. how would i go about writing a rand function that returns a new value each time it is called?

EDIT: so i chucked a cout in the draw() function to see what Rand_Num was becoming, and the rand() function is indeed generating a new number every time draw is run, however it isn't returning the different number on the players turn.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this