Thread: Lost on Assignment

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Lost on Assignment

    Hi. I am struggling with an assignment in a beginning-level C++ course. The course is going very fast, and I am one of the best in the class. As you might imagine, it's a horrible feeling to be stuck!

    Let me give you some of the details. The assignment is to build a program that simulates a card game. We are using four classes. They are Card, Deck, Player, and Game. The professor is teaching a method which has calls, "Class in class." It involves using instances of class objects as data members of other classes. I think that it is a form of inheritance.

    I am trying different things at the moment. Here is my Class and Deck definitions:

    class Card
    {
    public:
    Card();
    void setValue(int);
    int getValue();
    private:
    int value;
    };

    class Deck
    {
    public:
    Deck();
    Card randomCard;
    int shuffle();
    };

    I am trying to figure out how to put things together from here. I have a bunch of code that I will not yet post. I'm a bit worried about posting the code properly. Anyone feel like giving some input? I'll certainly appreciate it. Thanks, Steve

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >The professor is teaching a method which has calls, "Class in class."
    More commonly known as "Containment". It's not a form of inheritance, but you can simulate containment to an extent with private inheritance.

    >Here is my Class and Deck definitions
    Close. The cards in a deck should be private, and you need more of them:
    Code:
    class Card
    {
    public:
      Card();
      void setValue(int);
      int getValue();
    private:
      int value;
    };
    
    // A deck of Cards
    class Deck
    {
    public:
      Deck();
      int shuffle();
    private:
      Card deck[52];
    };
    Then you would have two Players, who have N cards, and a Game that has N players:
    Code:
    class Player
    {
    public:
      Player();
      int play();
    private:
      Card hand[5];
    };
    
    class Game
    {
    public:
      Game();
      void run();
    private:
      Player players[2];
      Deck gamedeck;
    }
    My best code is written with the delete key.

  3. #3
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Reply 1

    Thanks. Here is the code I have so far. I can show you the assignment, which pretty much states what we should use for data members and member functions. I have conformed to this. For instance, the assignment states that class Deck have no private data members:

    #include<iostream>
    #include<ctime>
    #include<string>

    using namespace std;

    class Card
    {
    public:
    Card();
    void setValue(int);
    int getValue();
    private:
    int value;
    };

    class Deck
    {
    public:
    Deck();
    Card randomCard;
    int shuffle();
    };

    class Player
    {
    public:
    Player();
    void enterInfo();
    void setName(string);
    string getName();
    int getTotal();
    int addCardValue(Card);
    private:
    string name;
    int total;
    };

    class Game
    {
    public:
    Game();
    void enterPlayerInfo();
    void playBlackJack();
    Player getWinner();
    void showGame(Player);
    private:
    void drawPlayer();
    void drawDealer();
    Player user;
    Player computer;
    };

    int main()
    {
    srand((unsigned int)time(NULL));
    Game game;
    game.enterPlayerInfo();
    game.playBlackJack();
    return 0;
    }

    Card::Card()
    {
    value=0;
    }

    void Card::setValue(int newValue)
    {
    value=newValue;
    }

    int Card::getValue()
    {
    return value;
    }

    Deck::Deck()
    {
    shuffle();
    }

    int Deck::shuffle()
    {
    return rand()%10;
    }

    Player::Player()
    {
    name="";
    }

    void Player::enterInfo()
    {
    cout<<"Please enter your name: ";
    cin>>name;
    }

    void Player::setName(string newName)
    {
    name=newName;
    }

    string Player::getName()
    {
    return name;
    }

    int Player::getTotal()
    {
    return total;
    }

    int Player::addCardValue(Card some)
    {
    return getTotal()+some.getValue();
    }

    Game::Game()
    {
    }

    void Game::enterPlayerInfo()
    {
    user.enterInfo();
    }

    void Game::playBlackJack() //work on later
    {
    drawPlayer();
    }

    Player Game::getWinner()
    {
    Player tmpWinner;
    //tmpWinner=
    return tmpWinner;
    }

    void Game::showGame(Player both) //work on later
    {
    }

    void Game::drawPlayer() //work on later
    {
    cout<<"Drawing a hand for "<<user.getName()<<endl; //?
    cout<<"Your card was "<<user; //?
    }

    void Game::drawDealer()
    {
    }

    Thanks, Steve

  4. #4
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Reply 2

    Here is the assigment. Let me know if you have any trouble with the MS Word file.

    Thanks,

    Steve

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Um, I doubt people will want to look at your assignment...Maybe you have a specific question/problem that you are having difficulty with?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Thank You

    Well, I started off by saying that I wasn't exactly sure how to post. Prelude answered me and gave his insight. I responded with further code outlining my specific problem. I also related how I felt that I needed to clarify the situation, in that my professor has given us a specific way to do this assignment. Therefore, Prelude's idea of using a private data member in class Deck (which I'm sure works well), is unfortunately not applicable.

    I appreciate your view that other members of this message board may not be "interested" in my assignment. You will hopefully understand that I thought that posting this would be the next step in giving Prelude insight into my specific area of difficulty.

    I will wait a little while to see if I get any other input along these lines. If I do not, I will formulate another approach to asking for advice.

    Thanks,

    Steve

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by smitsky
    Well, I started off by saying that I wasn't exactly sure how to post.
    It shows. What she neglected to mention was you need to use code tags. Very few of us will try to understand your program in its posted form -- with no formatting.

    Also, you only need to post the portions of the program you are having trouble with, not all 250 lines of code.

    Quote Originally Posted by smitsky
    I appreciate your view that other members of this message board may not be "interested" in my assignment. You will hopefully understand that I thought that posting this would be the next step in giving Prelude insight into my specific area of difficulty.
    Good assumtion. But as a volunteer army, for us it's sometimes better to zero in on a specific problem, explain what you're trying to accomplish in the associated code, and what it's doing wrong. Somewhat like your first post did. Small and concise.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM