gpt4 book ai didi

C++ - 使用枚举生成一副纸牌

转载 作者:行者123 更新时间:2023-11-30 03:43:45 25 4
gpt4 key购买 nike

我刚开始学习C++,对头文件的概念和类的结构还是很陌生。我已经学习 Java 2 年了,C++ 语法和一些操作有不同的行为。

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

我已经研究过,现在我意识到在 C++ 中你不能迭代枚举。我看到人们到处都建议使用枚举,但避免了如果我不能遍历枚举我怎么可能填充甲板的问题。

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

#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 头和类如下:

#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() 方法中的语法不正确。但这是我要解决的问题。

是否可以创建卡片并将它们以类似的方式添加到牌组中,还是我必须走另一条路线并使用数组来代替?

最佳答案

你可以直接做:

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

Demo

关于C++ - 使用枚举生成一副纸牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35937285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com