Reputation: 23
I have just started learning C++, and am still new to the concepts of header files and the structure of classes. I have been learning Java for 2 years and the C++ syntax and some operations have different behaviors.
I am trying to make a deck of cards. To make the code cleaner, I would like to use enumerations for my Suits and Values of the cards. However the problem in the Deck class is that I have a method to populate the deck with cards using a vector
.
I have researched and I now realize in c++ you cant iterate over enumerations. Everywhere I look people suggest using enumerations but avoid the question of how could I possibly populate the deck if I cant iterate through enumerations.
I have a Card header file and class as follows:
#ifndef CARD_H
#define CARD_H
class Card
{
public:
enum Suit{ HEARTS, CLUBS, SPADES, DIAMONDS };
enum Value{TWO ,THREE ,FOUR ,FIVE ,SIX ,SEVEN ,
EIGHT , NINE , TEN ,ACE,KING,QUEEN,JACK};
Card(Suit suit, Value value);
~Card();
Suit GetSuit();
void SetSuit(Suit suit);
Value GetValue();
void SetValue(Value value);
private:
Suit m_cardSuit;
short m_cardValue;
};
#endif
#include "Card.h"
Card::Card(Suit suit,short value){
this->m_cardSuit = suit;
this->m_cardValue = value;
}
void Card::SetSuit(Suit suit){
this->m_cardSuit = suit;
}
//this is needed to get enum from .h class!!
Card::Suit Card::GetSuit(){
return m_cardSuit;
}
short Card::GetValue(){
return m_cardValue;
}
void Card::SetValue(short value){
this->m_cardValue = value;
}
And a Deck header and class as follows:
#ifndef DECK_H
#define DECK_H
#include <vector>
#include "Card.h"
using namespace std;
class Deck
{
public:
Deck();
Card GetCard(Card& card);
void addCard(Card& card);
void shuffleCards();
vector<Card> drawCards(short cardsToDraw);
void PopulateDeck();
~Deck();
private:
vector <Card>* m_pCards;
short m_NumberOfCards;
short m_MaximumNumberOfCards;
Card m_card;
};
#endif
#include "Deck.h"
Deck::Deck()
{
m_MaximumNumberOfCards = 52;
m_NumberOfCards = 0;
m_pCards = new vector<Card>(52); //creates heap space for 52 cards.
PopulateDeck();
}
Deck::~Deck()
{
}
void Deck::addCard(Card& cardToAdd){
m_pCards->push_back(cardToAdd); //add a card to the deck. same as (*m_pCards).cardToAdd
}
void Deck::PopulateDeck(){
for (int i = 0; i < 13; ++i)
for (int j = 0; j < 4; ++j){
Card card(Card::Suit.[j],Card::Value.[i]);
m_pCards->push_back(card);
}
}
I know the syntax in the PopulateDeck()
method is incorrect. But this is the problem I am trying to solve.
Is it possible to create cards and add them to the deck in a similar manner or will I have to go another route and use arrays instead?
Upvotes: 1
Views: 6406
Reputation: 217245
You may directly do:
void Deck::PopulateDeck(){
for (int i = 0; i < 13; ++i) {
for (int j = 0; j < 4; ++j){
Card card(j, i);
// Or Card card(Card::Suit(j), Card::Value(i))
m_pCards->push_back(card);
}
}
}
Or, with strong type:
enum class Suit{ Begin, HEARTS = Begin, CLUBS, SPADES, DIAMONDS, End };
enum class Value{
Begin, TWO = Begin, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING, ACE, End
};
Card::Suit& operator ++(Card::Suit& e)
{
e = Card::Suit(int(e) + 1);
return e;
}
Card::Value& operator ++(Card::Value& e)
{
e = Card::Value(int(e) + 1);
return e;
}
void Deck::PopulateDeck(){
for (Card::Value i = Card::Value::Begin; i != Card::Value::End; ++i) {
for (Card::Suit j = Card::Suit::Begin; j != Card::Suit::End; ++j){
Card card(j, i);
m_cards.push_back(card);
}
}
}
Upvotes: 2