This is the first program for a class that I am taking. I am having trouble creating the classes. We were only taught briefly on the subject and then told to complete this project. I guess my first question is how should I set my class "deck" and "card" up? I don't want someone to set the classes up for me. I would only like help.

Create a program to simulate a card game as described below.  Use a deck of 30 cards, which contains 10 different values (numbered 1 through 10) and 3 different suits.  The suits may be any theme you like (does not have to be hearts, spades, etc.).

Make this a three player game, where two of the players are controlled by the computer, and one player is controlled by the user.

After shuffling, deal all of the cards to the three players.  Each player will get 10 cards.

Notes:
1.  Make a class named Card, as well as a class named Deck
2.  Shuffle the cards by randomly swapping 2000 pairs of Cards
3.  Store the players’ hands in arrays of type ‘Card’

The Game:
1.  Display the player's 10 cards, but do not show the 2 other hands
2.  Have the player 'bid' how many tricks they think they will win
3.  If bid is made, the player scores 10 points for each trick bid
4.  The computer scores 10 points for each trick made
5.  The player loses 10 points for each trick bid if the bid is not made
6.  Each time a computer controlled player leads a card, randomly choose a suit and lead the highest card of that suit
7.  Each time a computer controlled player has to follow suit, play the highest card if the computer could win the trick, lowest otherwise
8.  If a computer controlled player has to 'throw off' due to not having the suit lead, throw the lowest card from the suit it has the most of
9.  When any player reaches 100 points, the game ends

What to submit:
Submit source code via email.  If you are absent, it is still your responsibility to submit on time.  Put group members’ last names in the subject line.

Keep in mind the following:
•   Name your source files deck.h, deck.cpp, and main.cpp.
•   With invalid data, your program should respond appropriately.
•   Each function prototype should have a description, preconditions, and postconditions.
•   Test your code when you think you are finished.  Try to break it.  After you are done trying to break it, I will then try.

Recommended Answers

All 13 Replies

Wat is a card? It's an object that has a suit and a value from one to thirteen. So make a class named card that has two member variables; a value from one to thirteen (an int type springs to mind) and something to hold the suit value. Could be an enum, or an int, or a string, or anything else you feel like.

What is a deck? It's an object that is made up of 52 cards. So create a class that contains 52 cards. An array of card objects springs to mind.

This is programing. Thinking about the problem at hand in such a way that the solution lends itself to a programmatical expression. The rest is just memorising syntax.

Ok. We will be happy to help you, ONCE you have made an effort to solve the problem. Do some reading on the subject of C++ and (especially) object-oriented design. Consider: you have classes such as card, deck (which would be a collection of cards), etc. So, model it accordingly. Work out, in your mind, how these things relate, and how they interact. Then write the C++ classes that reflect that behavior. Operations (methods) that the 'deck' class would expose, would be things like "shuffle", "deal", etc. Obviously (to me, at least), shuffling would randomize the cards, so you need to incorporate that concept into your "shuffle" method code.

This is a great assignment, and will teach you a lot. However, PLEASE don't ask us to help you cheat on this assignment! :-)

This is what I have so far:

class card {
    public:
        card(); // constructor

    private:
        string suit;  //hearts, clubs, spades
        int value;  //1-10
};

class deck {
    public:
        deck(); //constructor
        shuffle(card); //shuffles the deck (type card)
    private:
        card deck[30]; //a variable of type card to store my deck
};

I will eventually need 3 hands. I am assuming that those hands will be 3 arrays of type card that are declared in main and will be filled by copying values from the deck into the hands one by one?

Your card class is ignoring the face cards (jack, queen, king), so you need 1-13... :-) Ace == 1, king == 13.

A deck (in traditional terms) == 4 suits of 13 cards == 52 cards in total. You are only allowing 30.

So, you have started with the class definitions, but there is still a lot of work to do, both in the interfacees, as well as in the implementation (which you don't provide any of yet).

Any suggestions so far?

class card {
    public:
        card(); // constructor
        void setSuit(string);
        void setValue(int);
        string getSuit();
        int getValue();
    private:
        string suit;  //hearts, clubs, spades
        int value;  //1-10
};
class deck {
    public:
        deck(); //constructor
        shuffle(card); //shuffles the deck (type card)
    private:
        card deck[30]; //a variable of type card to store my deck
};

card::void setSuit(string su) {
    suit = su;
}
card::void setValue(int val) {
    value = val;
}
card::string getSuit() {
    return suit;
}
card::int getValue() {
    return value;
}

Rubberman, the directions state:

Create a program to simulate a card game as described below. Use a deck of 30 cards, which contains 10 different values (numbered 1 through 10) and 3 different suits. The suits may be any theme you like (does not have to be hearts, spades, etc.).
Make this a three player game, where two of the players are controlled by the computer, and one player is controlled by the user.

Also, I stated very clearly that I wanted help. Not just the solution. With that said, how does my most recent post look?

It probably doesn't matter, but storing the suit as a string seems pointlessly wasteful. I might not go so far as to store the rank of the card in a short in the name of efficiency, but I would definitely store the suit as an enum. Your getSuit can convert the enum suit into a string, but your computer players are going to want to compare cards by suit and it would be better if they didn't need to compare strings to do that.

Premature optimization should be avoided, but that doesn't mean you should do everything in the most costly way you can find.

Am I completely lost in where I am going with my above code?

shuffle(card); //shuffles the deck (type card) doesn't make a lot of sense (if the aim is to shuffle the deck, what are you providing that card object for?) and you seem to have reflexively added a setter and getter for the card's internals without much justification, but I don't see anything that makes me really flinch (although now that I think about it, seeing setters and getters for a class' internals does make me flinch a bit).

ok, thanks for everyone's help. I have completed a lot of the code, I am not having problems with one part of logic:
If a computer controlled player has to 'throw off' due to not having the suit lead, throw the lowest card from the suit it has the most of

Here is what I have for the computer selection of a card

void comp1Turn(card *comp1Hand, card *pile) {

    card tempHi;
    card tempLo;
    for (int i = 0; i < 10; i++) {
        if(pile[0].getSuit() == comp1Hand[i].getSuit() && comp1Hand[i].getVal() != -1)  {
            if(pile[0].getVal() < comp1Hand[i].getVal() && tempHi.getVal() < comp1Hand[i].getVal()) {
                tempHi = comp1Hand[i];
            }
            else if (comp1Hand[i].getVal() < tempLo.getVal()) {
                tempLo = comp1Hand[i];
            }

        }
    }
    if (tempHi.getSuit() == pile[0].getSuit() || tempLo.getSuit() == pile[0].getSuit()) {
        if (tempHi.getVal() != 0) { 
            if (tempHi.getVal() > pile[0].getVal())
                pile[1] = tempHi;
            else
                pile[1] = tempLo;
        }
    }
    else {
        //here is where I need help
        comp throw lowest card it has the most of
    }
}

Do a loop that iterates over the possible suits, and inside that loop you can have a loop which counts the number of cards for each suit. Keep track of which suit scored highest and which card was lowest in that suit.

Do you still have this program?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.