I need help with a Blackjack game.

This is a discussion on I need help with a Blackjack game. within the C++ Programming forums, part of the General Programming Boards category; No, it initializes the array. It looks similar, but there is a formal difference. Some things can only be initialized ...

  1. #16
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    No, it initializes the array. It looks similar, but there is a formal difference. Some things can only be initialized but not assigned, like arrays and const variables. There's also a difference with classes concerning which functions are called.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #17
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Sorry if this is off-topic, but is there a way to efficiently check the sum of each value in an array without manually adding deck[1] + deck[2] + deck[3] etc., and a way to force a static variable to reset to its original value? I would like to add a line in the loop that chooses a card to check the sum, and if it's 0 it should reset the array. This would prevent infinite loops and reset the deck if it runs out of cards.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Quote Originally Posted by wipeout4wh View Post
    Sorry if this is off-topic, but is there a way to efficiently check the sum of each value in an array without manually adding deck[1] + deck[2] + deck[3] etc., and a way to force a static variable to reset to its original value? I would like to add a line in the loop that chooses a card to check the sum, and if it's 0 it should reset the array. This would prevent infinite loops and reset the deck if it runs out of cards.
    No and No. You'd need to loop through the whole array or keep a running total.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #19
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Quote Originally Posted by King Mir View Post
    No and No. You'd need to loop through the whole array or keep a running total.
    So there's no way to reset the deck? How would I make it possible to continue playing after the deck runs out of cards, and prevent the infinite loop?

  5. #20
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Like I said originally, you shuffle the deck before you start dealing.

    Shuffle: to arrange in a random order; pick two random card positions, and swap them over; repeat a number of times.

    Deal: take the next card from the deck; all you need to do here is keep a count of how many cards you've dealt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Quote Originally Posted by Salem View Post
    Like I said originally, you shuffle the deck before you start dealing.

    Shuffle: to arrange in a random order; pick two random card positions, and swap them over; repeat a number of times.

    Deal: take the next card from the deck; all you need to do here is keep a count of how many cards you've dealt.
    But that still doesn't solve the problem of getting a new, partially full deck (not including the cards that are in play) when every card is gone. Drawing a random card is pretty much the equivalent of drawing from a shuffled deck, so it makes shuffling pointless.
    Last edited by wipeout4wh; 06-07-2011 at 01:47 AM.

  7. #22
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    You can get a new shuffled deck by shuffling the deck again and reseting the next card pointer/index to the beginning.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #23
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Quote Originally Posted by King Mir View Post
    You can get a new shuffled deck by shuffling the deck again and reseting the next card pointer/index to the beginning.
    I don't know anything about pointers yet, so I don't know what any of that means. I still don't see why shuffling is necessary, either.

  9. #24
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Code:
    if ( numCardsDealt == 52 ) {
      // reshuffle the deck
      numCardsDealt = 0;
    }
    > I still don't see why shuffling is necessary, either.
    Have you ever played blackjack with a deck of real cards?

    Try to model the actions you take there.

    People do not pick random numbers up to 52, and then check to see if that card has already been drawn.

    No, they randomise the order to begin with, and then sequentially draw cards from the top.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #25
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Shuffeling is a more standard solution, and eliminates the need to repopulate the array. You can do it your way, but then you'll need to write a function to repopulate the deck. Also, as a lesser point if you use a shuffled deck, you automatically have the number of cards played.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #26
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Quote Originally Posted by Salem View Post
    Code:
    if ( numCardsDealt == 52 ) {
      // reshuffle the deck
      numCardsDealt = 0;
    }
    > I still don't see why shuffling is necessary, either.
    Have you ever played blackjack with a deck of real cards?

    Try to model the actions you take there.

    People do not pick random numbers up to 52, and then check to see if that card has already been drawn.

    No, they randomise the order to begin with, and then sequentially draw cards from the top.
    I guess, but drawing a random card is easier, and it accomplishes the same task in the long run. Is there really any benefit besides making the game's programming more realistic?

  12. #27
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Quote Originally Posted by King Mir View Post
    Shuffeling is a more standard solution, and eliminates the need to repopulate the array. You can do it your way, but then you'll need to write a function to repopulate the deck. Also, as a lesser point if you use a shuffled deck, you automatically have the number of cards played.
    How would it eliminate the need to repopulate the array? I would have to send the cards that are no longer in play to a discard pile array, then bring them back to the deck when it's reshuffled.

    Edit: Also, I could get the number of cards played by using a static int and adding 1 to it each time drawCard() is called.

  13. #28
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    You can just have an index to the current point in the deck. Every time you draw a card, you return the card at that index, and increment it by 1.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #29
    Registered User
    Join Date
    Apr 2006
    Posts
    2,037
    Quote Originally Posted by wipeout4wh View Post
    I guess, but drawing a random card is easier, and it accomplishes the same task in the long run. Is there really any benefit besides making the game's programming more realistic?
    The best reason is probably complexity. To get the 52nd card, you'll need to loop a worst case of 52 times, whereas with a shuffled deck, you don't need to loop at all.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #30
    Registered User
    Join Date
    Mar 2009
    Posts
    39
    Quote Originally Posted by King Mir View Post
    You can just have an index to the current point in the deck. Every time you draw a card, you return the card at that index, and increment it by 1.
    The index is just the part of an array that's in square brackets, right?

    If I do that, it would still reuse the cards that are still in play when I reshuffle the deck. If I have a 5 in my hand, there should only be 3 5's in the deck when it gets reshuffled. That means I would need to remove the card from the deck and add it to a discard pile when it's out of play.

    Edit: I decided to have it shuffle the deck first like you guys said. Here's drawCard() so far:

    Code:
    int drawCard() {
    
        static int deck[52] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13};
        static int cardToDraw = 0;
        int drawnCard;
    
        if (cardToDraw < 52) {
            drawnCard = deck[cardToDraw];
            deck[cardToDraw] = 0;
            cardToDraw++;
        }
        else {
            shuffleDeck();
            cardToDraw = 0;
        }
    
        cout<< "cardToDraw = "<< cardToDraw<< ".\n";//Debugging line.
        return drawnCard;
    }
    I'll have to learn about passing variables between functions, and I haven't written shuffleDeck() yet, but does this look good so far?

    Edit: Would it be better to just set deck[52] as a global variable? I don't really understand how to pass variables between functions. I wrote part of shuffleDeck(), but I can't continue until I figure this out.

    Code:
    void shuffleDeck() {
    
        int firstCard;
        int secondCard;
        int i;
    
        for (i = 0; i < 100; i++){
            firstCard = (rand() % 52);
            secondCard = (rand() % 52);
            
        }
    }
    Last edited by wipeout4wh; 06-07-2011 at 06:07 AM.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BlackJack Game
    By Squintz in forum C Programming
    Replies: 3
    Last Post: 11-12-2002, 02:31 PM
  2. blackjack game Please, please help!!
    By collegegal in forum C Programming
    Replies: 4
    Last Post: 03-11-2002, 07:02 PM
  3. blackjack game
    By collegegal in forum C Programming
    Replies: 1
    Last Post: 03-02-2002, 03:42 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21