Archived

This topic is now archived and is closed to further replies.

Confusion with Blackjack problem

This topic is 5624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m making my first ever game (from scratch) and I''m a little confused in a certain part of the the code. Now I know that I may be made fun of or even reamed for asking a question like this but I am really stumpped I have no teacher because I can''t afford to take any classes right now. The problem that I have is that the player is dealt a card from the deck: int g_Player; int g_Dealer; enum {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; enum {1,2,3,4,5,6,7,8,9,10,11}; unsigned short int cards[52]; //num is the number of the card and suit is suit of the card srand(num, suit) % 52; //Dealer pulls a card from the deck srand(num, suit) % 52; here is what I don''t understand. I know I''m doing something wrong I can see it. But I''m not sure what it is. I know that I''m missing code for the player and the dealer. Is it that I''m missing the logic for the player and dealer or what?? I''m not to sure where to put the actual dealer and player logic. Sorry for the nightmare.

Share this post


Link to post
Share on other sites
Let me see if i understand this. You haven't written the code yet, but something is wrong?

Why are you doing this:

"enum {1,2,3,4,5,6,7,8,9,10,11};"

enum is short for enumeration. It assigns a numerical value to it's contents. so essentially what you are doing with the above statement is asigning the valuse of '0' to the indicator '1'. This could get really confusing.


I am assuming you are using the console. Here is a SUGGESTION:

  
int main()
{
//do usual stuff here

bool bContinue = true;

while(bContinue)
{
//do the player stuff

//do the dealer stuff, or vice versa

//ask if they want to play another round

cout<<"DO you want to play again? YN";
/*
if the answer is no, set bContinue to false
else
resume
*/

}

return 0;
}


edit: added the source tags.



[edited by - MrBeaner on July 23, 2002 11:58:22 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Kaver
I''m making my first ever game (from scratch) and I''m a little confused in a certain part of the the code. Now I know that I may be made fun of or even reamed for asking a question like this but I am really stumpped I have no teacher because I can''t afford to take any classes right now. The problem that I have is that the player is dealt a card from the deck:

int g_Player;
int g_Dealer;

enum {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
enum {1,2,3,4,5,6,7,8,9,10,11};

unsigned short int cards[52];

//num is the number of the card and suit is suit of the card
srand(num, suit) % 52;

//Dealer pulls a card from the deck
srand(num, suit) % 52;

here is what I don''t understand. I know I''m doing something wrong I can see it. But I''m not sure what it is. I know that I''m missing code for the player and the dealer. Is it that I''m missing the logic for the player and dealer or what?? I''m not to sure where to put the actual dealer and player logic. Sorry for the nightmare.







  

// What does every card have? A suit and a face so lets make a deal!

#define NUMBER_OF_SUITS 4
enum Suit { Hearts, Diamonds, Clubs, Spades };
enum FaceValues { Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,Ace }

// Alright lets make a virtual card.

struct Card
{
Suit CardSuit;
FaceValues Value;
};

// 52 cards in a deck.

Card Deck[52];

// Ok lets fill the deck in order.

for(int suits=0;suits < NUMBER_OF_SUITS; suits++ )
{
for(int cards=0; cards < 52; cards++)
{
Deck[cards].CardSuit= suits;
Deck[cards].Value = cards;
}
}

// Seed the random number generator.

srand(TIME(NULL));

// Shuffle the deck 52 times may be a bit too much but what ever.

for(int i=0;i<52;i++)
{
Swap(Deck[rand()%52],Deck[rand()%52]);
}

// Our swap function.

void Swap( Card &card1, &Card card2)
{
Card tempCard;
tempCard = card1;
card1 = card2;
card 2 = tempCard;
}


It''s a little rough around the edges but it should get you a shuffled deck then all you need to do is to give both the player and the dealer 2 cards to start with. The dealer will usually stay if their hand is 18 or over. If not then they will hit. The player just needs to be asked if he''d like to hit. Then you count up the values and see who''s over 21 and BAM you''ve got blackjack!

Share this post


Link to post
Share on other sites
I''m bored, so the AP gets random suggestions:

1) The first enum. Try:

  
enum {
eSuit_Hearts,
eSuit_Diamonds,
eSuit_Clubs,
eSuit_Spades,

eSuit_Count
};

This way, eSuit_Count will == the number of suits, no matter how many you add.
2) The second enum only causes confusion. Why would you want "two" valued at 0? And the face cards should all have the same value. And the aces are special cases. And...
3) Your deck fill-in is buggy, unless you wanted a deck of all Spades valued from 0 - 51.
4) There is not item #4 in this list.
5) I could mention the STL random_shuffle, or why using % with rand() isn''t the happiest of ideas in applications that matter, but this is just a beginner''s program, so they''re fine.

Cheers,
-scott

Share this post


Link to post
Share on other sites
Hi,

Thanks for all of your replies. It was a great help clearing up my sloppy code.

As for the

"enum {1,2,3,4,5,6,7,8,9,10,11};"

part of the code I put there to try to use it as a reference for the value of the cards which would later be put in arrays. But like you all said I was confusing the issue.

Again thanks for the help.


Share this post


Link to post
Share on other sites
By the way. Most of time I tend to babble so just ignore it. Coffee has some side effects especially when you have two to three cups in a matter of hours.

Share this post


Link to post
Share on other sites
I have question about loops. My question is what type loop should I use for the player selection/decision when the cards are being pulled. Right now I''m using an if statement but it keeps going and going. I can''t see how to end it. Here is what I have so far for the player loop:

.
.
.
else
if(NUM_CARDS < 21)
{
//hit the player with another card if selected
for(CS_CLK.HIT = TRUE)
{
player->cards.deal = show.cards[TRUE];
NUM_CARDS + NEW_CARD = NEW_TOTAL;
//are the three cards > 21? If YES player busts, if NO
//repeat.
if(NEW_TOTAL > 21)
{
player[bet].money;
money --;
cout << "You Busted!";
return 0;
}
else
if(NEW_TOTAL == 21)
{
player->cards.deal = FALSE;
return 0; //I''m not sure if I should do this or
//if I should return some other value?
}
else
//Here is where I''m stumpped.
if(NEW_TOTAL < 21)
{
CS_CLK.HIT = TRUE || CS_CLK.STAND = TRUE;

I''m not sure what to do after that point because it leads into another NEW_TOTAL < 21 loop and it will keep going and going. Any suggestions on how to fix this problem would be greatly apperitated (sp?). I was thinking of changing it and using a switch()
case
type format.

Share this post


Link to post
Share on other sites
Here is the way I would do it (in pseudocode). This all goes inside your main game loop.



    

char input;
while ( playerScore < 21 ) {

cout << "Would you like to hit or stand? Press h to hit or s to stand";
//take the user input, most likely with a cin and a char

// you can use another code

cin >> input;
if ( input == 'h') {
// come up with a random card

// add its value to playerscore

}
if (input == 's' ) {
// break out of the loop

break;
}
}
//here test to see why he left the loop

//there are 3 ways:

// 1) he busted so the score is more than 21

// 2 ) he got blackjack so the score is equal to 21

// 3) He stood at a number less than 21


if (playerscore > 21) {
//user busted and thus loses

}
else if (playerscore == 21) {
// user got blackjack and thus wins

}
else if (playerscore < 21) {
//do what the dealer has to do

}




Im not positive about the rules of blackjack, but I believe this is using the right rules.

If you need more help, feel free to email me at NYYanks432@hotmail.com


edit: Put source tags in

[edited by - gamedev135 on July 24, 2002 4:38:54 PM]

Share this post


Link to post
Share on other sites