Reputation: 113
I was reading my school book and when I read this. "Create a class named PlayingCard." It contains four fields- a value and suit in both numeric and string format."
When I read this I think that they're talking about class fields, (static) Question: but this would be larger than "four" wouldn't it?
Reading further. "Each PlayingCard is assigned its values upon construction." So it's wanting to use a contructor that gives the "PlayingCard" a suit and value for a 52 card deck? Question: But it isn't wanting me to make a deck of cards?
I am not sure how to even proceed.
The whole question from the book is:
"Create a class named "PlayingCard". It contains four fields- a value and suit in both numeric and string format(for example, 12 might be "Queen") for a standard playing card. Each PlayingCard is assigned its values upon construction. Create another class named "Hand" which represents a standard poker hand which consists of five "PlayingCard"s. The Hand Constructor should randomly assign a value to each of its five "PlayingCard"s, with no dublicates. In other words, there can be multiple 4's, and multiple hearts in a "Hand", but only one four of hearts. Write a main() function that declares a "Hand" and displays its values."
Upvotes: 0
Views: 502
Reputation: 10162
An example of what your PlayingCard
class may look like:
class PlayingCard
{
public:
// use this constructor to store your values
PlayingCard(int cardNum, string cardName, int suitNum, string suitName);
int GetNumericCardValue();
string GetCardName();
int GetCardSuiteNumericValue();
string GetCardSuiteName();
private:
int card_num;
string card_name;
int suite_num;
string suite_name;
}
I won't do everything for you, obviously... but it's a template to start off with.
There are a few ways of handling this. Easiest way is probably to generate a list of PlayingCard
s and at random select a card out of the list to be in your hand. I'm not sure if you're familiar with lists, or even vectors... if not, arrays will work just as well.
So, you can do something like this (based on most-used French variation):
PlayingCard deck[52] = {};
PlayingCard hand[5] = {};
string suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
string name[] = { "Two", "Three", "Four" }; // ...you get the idea (13 per suit)
...then simply generate four cards using random numbers for suit
and name
using a loop.
// int randSuit = random number from 0 to 3
// int randCard = random card from 0 to 12
hand[0] = new PlayingCard(randCard+2, name[randCard], randSuit+1, suit[randSuit]);
Just confirm that the card doesn't already exist in your hand, prior to assigning it to the target index. If it doesn't, assign it to that index.
You may also generate an entire deck and then simply select an index at random, confirming that that index was not already selected. Conceptually, that may be easier for you. I just prefer not to create objects if they're not used right away.
By the way, in some regions A (Ace)
also counts as 1
, so, adjust appropriately. Above is just the general schema to get you started.
A related SO question you may be interested in: Generating a Deck of Cards
Oh and... whenever you get stuck on problems like this, start small. Little by little you get there. If you try to lay out the whole thing right away, you'll get frustrated. Obviously, have a general idea where you're heading, to minimize restructuring.
Upvotes: 1
Reputation: 578
Your Hand Class will have an array of five PlayingCard objects. So when you create a Hand object, in your constructor you randomly select 2 values for each card - one for the value and one for the suit. Then you create a PlayingCard object and pass these values to it's constructor. You need to create only one Hand object in your main()
function. Now you can find out how you can randomly select values without having duplicates in your hand of cards and do the rest. Hope this is clear.
Upvotes: 2