Thread: Jumping into C++ chapter 8 question 5

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    6

    Exclamation Jumping into C++ chapter 8 question 5

    Hey there guys.
    i've been going through this book in all and finally hit chapter 8. But now i'm stuck on this practice problem.

    Practice Problem.
    Write a program to play poker! You can provide 5 cards to the player, let that player choose new cards, and then determine how good the hand is.


    I don't even know how i'll start doing this.
    Don't get me wrong i'm not asking you to do it for me, i'm just asking for assistance as to how to start. because i'm completely lost at this one. and won't go to other chapter till i complete it.

    Just in-case these are the topics covered so far in the book.chapter 1 - Intro and developer environment setup
    chapter 2 - The basics of C++
    chapter 3 - User interaction and working with variables
    chapter 4 - If statements
    chapter 5 - Loops
    chapter 6 - Functions
    chapter 7 - switch case and enums
    chapter 8 - randomizing your program
    Getting random numbers in c++, bugs and randomness.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Break the problem down into smaller pieces, then solve once piece at a time. I started breaking it down for you, but some of these steps need to be broken down even further.

    You can provide 5 cards to the player...
    So you would need (1) a way to represent cards, (2) a place to store those cards, and (3) a method of randomly distributing five cards. The first one seems like it might be the hardest - think about it and see what you come up with.

    ...let that player choose new cards...
    The player needs to (1) see their cards, (2) select cards they want to get rid of, and (3) receive new (randomly selected) cards for each one they get rid of. Note that combining #2 and #3 basically yields a simple replacement.

    ...and then determine how good the hand is.
    This step requires independent research (incidentally, this is how programming a game will teach you all about that game). You need to (1) find and understand the list of hands in poker, and (2) analyze the player's hand for the best possible hand.

    As I said, a lot of these should be broken down even further - I just wanted to show you the way, so you could follow the path yourself. Trying to come up with a program like this all at once in your head makes it seem overwhelming. But breaking the problem down into little pieces makes the whole process a lot more manageable.

    Planning and working out details with pencil and paper before committing it to code is also strongly recommended.

    If you haven't seen this link, it describes how to build up your program piece-wise, adding a little bit at a time and testing it before adding more: A development process

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    Quote Originally Posted by Matticus View Post
    So you would need (1) a way to represent cards l
    Well for that piece i was thinking of making an enum to represent the cards like this. considering their are 52 cards, and 13 of each type.

    Code:
    enum Deck    {
           Ace_of_spades = 1,
           Ace_of_hearts,
           Ace_of_clubs,
           Ace_of_diamonds,
           two_of_spades,
           two_of_hearts,
           two_of_clubs,
           two_of_diamonds,
           three_of_spades,
           three_of_hearts,
           three_of_clubs,
           three_of_diamonds,
           four_of_spades,
           four_of_hearts,
           four_of_clubs,
           four_of_diamonds,
           five_of_spades,
           five_of_hearts,
           five_of_clubs,
           five_of_diamonds,
           six_of_spades,
           six_of_hearts,
           six_of_clubs,
           six_of_diamonds,
           seven_of_spades,
           seven_of_hearts,
           seven_of_clubs,
           seven_of_diamonds,
           eight_of_spades,
           eight_of_hearts,
           eight_of_clubs,
           eight_of_diamonds,
           nine_of_spades,
           nine_of_hearts,
           nine_of_clubs,
           nine_of_diamonds,
           ten_of_spades,
           ten_of_hearts,
           ten_of_clubs,
           ten_of_diamonds,
           jack_of_spades,
           jack_of_hearts,
           jack_of_clubs,
           jack_of_diamonds,
           queen_of_spades,
           queen_of_hearts,
           queen_of_clubs,
           queen_of_diamonds,
           king_of_spades,
           king_of_hearts,
           king_of_clubs,
           king_of_diamonds = 52,
        };
    another option was making 4 separate enums for each group of cards - clubs, spades, diamonds and hearts.

    but after thinking it through i'm guessing that this wouldn't work out, because it wouldn't display the names of the cards.

    my third option was making each card into a string variable. though when i need to randomize which cards is displayed ( later ) i might have trouble

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    All you really need is a unique identifier that you can divide into four different suits and thirteen different ranks. It's possible to use math to figure out what each card is. You can also use separate values for these, but it is not as neat as the math trick: you end up creating 52 different objects, one for each card, instead of just using numbers.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Posts
    6
    Quote Originally Posted by whiteflags View Post
    All you really need is a unique identifier that you can divide into four different suits and thirteen different ranks. It's possible to use math to figure out what each card is. You can also use separate values for these, but it is not as neat as the math trick: you end up creating 52 different objects, one for each card, instead of just using numbers.
    so something like this then?

    Code:
    enum suits { Spade, Heart, Diamond, Club};
    enum ranks { Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King };
    and then making the 52 different objects by a combination of the suit and ranks?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I was thinking something more like this: C++ code - 40 lines - codepad

    Code:
    #include <iostream>
    #include <algorithm>
    #include <stdlib.h>
    
    void Dump (int *deck, int deckSize);
    void FisherYatesShuffle (int *deck, int deckSize);
    
    int main()
        {
        /* For this example, it doesn't matter how the rng is seeded. */
        srand (1);
    
        int deck[52] = {0,};
        for (int x = 0; x < 52; x++)
            {
            deck[x] = x;
            }
        FisherYatesShuffle (deck, 52);
        Dump (deck, 52);
        return 0;
        }
    
    void Dump (int *deck, int deckSize)
        {
        const char *suit[] = {"clubs", "hearts", "spades", "diamonds"};
        const char *face[] = {"ace", "deuce", "three", "four", "five", "six", "seven",
            "eight", "nine", "ten", "jack", "queen", "king"};
        for (int x = 0; x < deckSize; x++)
            {
            std::cout << face[deck[x]%13] << " of " << suit[deck[x]%4] << std::endl;
            }
        }
    
    void FisherYatesShuffle (int *deck, int deckSize)
        {
        for (int i = deckSize - 1; i > 0; i--)
            {
            std::swap (deck[i], deck[rand()%(i + 1)]);
            }
        }
    Which might give you some ideas about how a pack of cards works, and what you should do for a class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [jumping into c++] Trouble on pointer chapter.
    By CrimsonMagi in forum C++ Programming
    Replies: 14
    Last Post: 11-04-2013, 07:13 PM
  2. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  3. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM
  4. Jumping into C++ chapter 7 help
    By DarthOrmus in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2013, 01:48 AM
  5. Jumping into C++ Chapter 5 problem 6 - Critique please
    By Kranky in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2012, 05:44 PM