-
August 3rd, 2010, 08:34 PM
#1
Deck of card class
I am having trouble with a program I am trying to write and was wondering if I could get some help. I am supposed to use two arrays to store the suit and denomination of cards in a deck.
const String suits["Clubs","Diamonds","Hearts","Spades" ]; // initialise suit array
const String denomination["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]; // initialise denom array
Next I am supposed to have two constructors, the first takes an int between 0 and 51 inclusive as its argument, then it computes which card has that value in the deck based on the table below.
Card number Actual Card
----------- -----------
0 Ace of Clubs
1 2 of Clubs
2 3 of Clubs
.
.
.
9 10 of Clubs
10 Jack of Clubs
11 Queen of Clubs
12 King of Clubs
13 Ace of Diamonds
.
.
.
25 King of Diamonds
26 Ace of hearts
.
.
.
38 King of Hearts
39 Ace of Spades
.
.
.
51 King of Spades
Here is how I did it
if (number < 0 || number > 51)
{
printf("Invalid");
}
if (number >= 0 && number <= 12)
suit = suits[0];
else if (number >= 13 && number <= 25)
suit = suits[1];
else if (number >= 26 && number <= 38)
suit = suits[2];
else
suit = suits[3];
// int temp1 = number / 13;
int temp2 = number % 13;
value = denomination[temp2];
The problem I am having is with the second constructor, it accepts two strings as arguments, the denomination of a card and the suit ("King","Diamonds"), I am then supposed to generate the value of this card based on the same table above. Meaning the King of Diamonds would be 25.
-
August 3rd, 2010, 10:14 PM
#2
Re: Deck of card class
Use "King" and "Diamonds" to form the Actual Card entry. You get "King of Diamonds". Then scan the table from the beginning until you find this entry in the table. The corresponding Card Number entry is the number you're looking for.
Or use the suits and denomination arrays. Find "Diamond" in suits and "King" in denomination. Then use the array positions to construct the number, like
int number = suits_index * 13 + denomination_index;
In the particular example it's 1*13 + 12 = 25.
Last edited by nuzzle; August 3rd, 2010 at 10:19 PM.
-
August 4th, 2010, 02:15 AM
#3
Re: Deck of card class
To add to above comment:
You best would add two enumerations like
Code:
enum { CLUB, DIAMOND, HEART, SPADE,
MAX_SUIT };
enum { ACE, ONE, TWO, THRE, FOUR, FIVE,
SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING,
MAX_RANK };
Those enums are suitable for defining and indexing your arrays:
Code:
const char * suits[MAX_SUIT] =
{
"Clubs","Diamonds","Hearts","Spades"
};
const char * ranks[MAX_RANK] =
{
"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"
};
So for example
cout << ranks[JACK] << " of " << suits[HEART];
would print "Jack of Hearts";
To compute index of 'King of Diamonds' you then would have
index = DIAMOND * MAX_RANK + KING;
For your constructor you would calculate
int suit = number/MAX_RANK;
int rank = number%MAXRANK;
and access the string arrays using those indices.
Regards, Alex
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|