C ++-使用枚举生成一副纸牌 [英] C++ - using enums to generate a deck of cards

查看:83
本文介绍了C ++-使用枚举生成一副纸牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习C ++,但对头文件的概念和类的结构仍然是新手.我学习Java已有2年了,C ++语法和某些操作有不同的行为.

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.

我正在尝试制作一副纸牌.为了使代码更整洁,我想对卡片的花色和值使用枚举.但是,Deck类中的问题是我有一种使用 vector 用卡片填充卡片组的方法.

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.

我已经研究了,现在我意识到在c ++中您无法遍历枚举.在我所看到的所有地方,人们都建议使用枚举,但是避免了以下问题:如果我无法通过枚举进行迭代,怎么可能填充甲板.

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.

我有一个Card头文件和类,如下所示:

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;
}

Deck标头和类如下:

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);
        }
}

我知道 PopulateDeck()方法中的语法不正确.但这是我要解决的问题.

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?

推荐答案

您可以直接这样做:

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);
        }
    }
}

或者,使用强类型:

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);
        }
    }
}

演示

这篇关于C ++-使用枚举生成一副纸牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆