Classes confuse me! For this particular assignment, we are to do the classic ‘Cards in a Deck’ program. We are to make two classes, one called ‘Card’ and the other ‘Deck’. Now I’ve gotten a large chunk of this done so far but a concept seems to be escaping me.
When we call a default constructor for the Card class, all 52 cards are to be created so that the Deck class may use these objects and… well create a deck! Now I attended a session with our TA and he stressed the fact that the default constructor is to initialize the values while the Deck class takes these objects and loops through them to use a function that assigns real card values to the Card objects.
My question is this: what is the best approach for creating these objects so that Deck may reference them? My idea right now is to create a fixed array called ‘initialCard[4][13]’. From there, the Deck class will pick a value – say ‘initialCard[0][0]’ and assign it to an Ace of Spades solely from the row and column of the two-dimensional array. However, my TA keep saying ‘create an array of Card objects’. What could he mean by that? I have a slight grasp but I’m just unsure… anyone have any ideas? Thanks!
Maybe this will help; straight from the assignment page:
Your card class should have public getter and setter functions for
both of these variables (getValue, setValue, etc), as well as a
function that will return a c-string representation of the card. (You
can call it toCString if you want.) It should work something as
follows:void toCString(char cardString[]) INPUT: A character array for storing
the output OUTPUT: NONE This function should return a character string
representing the card object. This string must have the following
form: two characters followed by a third null byte. The first
character represents suit; the second represents value. Suits should
be “S”, “H”, “C”, or “D” for spades, hearts,clubs, diamonds. The second character represents value: “A” for Ace,
2-9 for numbered cards, “T” for 10, “J” for Jack, “Q” for Queen”, “K”
for King. Some examples: 2 of spades = ‘S2’ 10 of hearts = ‘HT’ Jack
of diamonds = ‘DJ’ Queen of clubs = ‘CQ’ Ace of spades = ‘SA’ etc.In addition to these, you should have at least a default constructor to initialize the values. You are welcome to create other
constructors (perhaps one that initializes a card from it’s c-string
representation?) but it is not required.Deck Class: In addition to your card class, you will need to create a
Deck class to encapsulate all of your card objects. In this object you
should have a private member multi-dimensional array (of size SUITS by
VALUES) of your Card objects. When a Deck object is created, you
should have it initialize the multi- dimensional array of Cards. Card
suits are represented by an enumerated type; card values are
represented by an integer from 1 to 13 (1 is an Ace, 2-10 are numbered
values, 11 is the Jack, 12 is the Queen, and 13 is the King).Your constructor should loop over the multi-dimensional array and
initialize all values so that the complete deck of cards is
represented.
There are always a hundred ways to do something. For me, when creating classes and the structure of my program, I always think how things are related to each other.
In your problem, Cards in a Deck, the relationship is stated. Each Card object belongs to a Deck. I would start by creating the Card class as it has nothing lower than it then the Deck class. It sounds like you have gotten this far.
I would design the Card class to describe just a card, nothing else. The value and the suit. The Deck class i would have a private variable holding an array of cards. In the Deck classes constructor i would create the 52 cards. If you want to get fancy use a List rather than an array so you can add as many cards as you like. Not all card games use 52 cards in a deck.
Once the Deck is instantiated all of its methods will have access to the array of cards.