i have a project to create a deck of cards in a class (store them in an array) so i can use them later to create different card games, the class, i have been told, is right by my programming tutor, however i seem to have problems assigning the values to each card in the array.
the last for loop is just to see if they print and have stored ok.
Think about what you need to do. You need 13 cards or each suit. So the loop that selects the card value should go from 1 to 13 (or 0 to 12). Then for each iteration of that loop you need another loop selecting each suit for the current value.
1 2 3 4 5 6 7
for(int c = 0; c < 13; ++c) // select card value
{
for(int s = 0; s < 4; ++s) // select suit
{
// now make a Card using the values c and s
}
}
well i have very basic knowledge, reading through my code looks like it sound make sense, but i get errors on line 31 saying it cant call to my constructive function in the class
When you create an array there is no way to specify which values to give to the constructor. That is why you need to make a constructor that doesn't take any values.
The compiler is not confused. If you supply values then it will use the constructor that asks for arguments. If you don't supply values (like when you make an array) then the constructor without values is used.
When you make the array the values will have no meaning. It is only when you make a new card (with values) and copy that card into an element of the array that the card in the array gets proper values.
I think that's because you create your card outside the loop that set's its values. Also your outside loop j resets the entire pack over and over again 52 times. You don't need that outer loop. All you need is a variable j that you set to 0 before your two loops and increment it every time you add a new card.
I find something like this a bit cleaner than what you have. It is easy to see what it is doing and is only ~10 lines.
1 2 3 4 5 6 7 8 9 10
for(int i = 0; i < 52; i++){
switch( i / 12 ){
case 0: cards[i] = Card(i % 12, "Hearts"); break;
case 1: cards[i] = Card(i % 12, "Clubs"); break;
case 2: cards[i] = Card(i % 12, "Spade"); break;
case 3: cards[i] = Card(i % 12, "Diamond"); break;
default: cerr << "undefined behavior\n"; break;
}
}
Also rather than having the type as a string, you can create an enum:
1 2 3 4 5 6
enum card_type {
ACE,
SPADE,
HEART,
DIAMOND
};
Than for your get function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Card {
card_type suit;
constchar* getsuit()
{
switch(suit){
case ACE: return"Ace";
case HEART: return"Heart";
case DIAMOND: return"Diamond";
case SPADE: return"Spades";
}
return"Undefined";
}
};
This will be memory efficient compared to your other class, since you will be storing the string each time in memory for each card. With the above method the string is only stored in memory once, you could probably even do something a bit fancier with inheritence.
1 2 3 4 5 6 7 8 9 10
class BaseCard {
virtualconstchar* getsuit() = 0;
};
class SpadeCard : public BaseCard {
constchar* getsuit(){ return"Spade"; }
};
//same for heart, diamond, clubs...
Galik- After looking at my code, yes i can see where you are coming from about the j for loop resetting the pack, would it be something like this instead:
for(n = 0; n < 4; n++)
{
Card card (i, b);
switch (n)
{
case 1: b = "Hearts";
break;
case 2: b = "Spades";
break;
case 3: b = "Diamonds";
break;
case 4: b = "Clubs";
break;
};
if (i < 13)
{
++i;
}
c[j] = card;
++j;
}
xerzi- thank you for putting in time to help me, i really appreciate it, however i am relatively new to c++ and dont know what 'enum' is, i dont want to include anything fancy because i want to be able to understand it.
I can still see some problems with your code. You create your card before you set the suit, but you need to create it after you set the suit. Also your variable i is going to stick on 12.
Take another look at the loop I showed you, I've added some extra bits:
int i = 0; // index into array
for(int c = 1; c < 14; ++c) // select card value
{
for(int s = 0; s < 4; ++s) // select suit
{
// now make a Card using the values c and s
std::string suit;
switch(s)
{
case 0: suit = "Hearts";
break;
case 1: suit = "Spades";
break;
case 2: suit = "Diamonds";
break;
case 3: suit = "Clubs";
break;
};
c[i] = Card(c, suit);
++i; // ready for next position in the array
}
}
Note here we have two for() loops. The inner for() loop with execute entirely for every iteration of the outer for() loop. That means for every card value, we will loop through the different suits creating a card for each one.
i need the cards stored in order though i.e. ace of hearts, 2 of hearts....king of hearts, ace of spades ect, reading this seems it will store it; ace of hearts, ace of spades etc.