在C ++中实现一张卡片 [英] Implementing a Deck of Cards in C++

查看:133
本文介绍了在C ++中实现一张卡片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想创建一副卡片,我还是一个新的C ++和指针,所以我有几个问题



所以我有 Card class getRank getSuit c $ c>卡对象。
他们的方式,我设置的甲板是1-13号为排名和1-4为西装。所以这是一个简单的比较,通过比较一张卡的排名+西装和另一张卡的排名+西装。



所以我初始化并声明了两个数组,如下:

  char Rank [13] = {1,... 13}; 
char Suit [4] = ...

/ p>

  char * getSuit(){return Suit; } 
char * getRank(){return Rank; }
int Comp(Card * card){
if(rank + suit< card-> rank + card-> suit)
return 1;
else if ...
else ...
}

我的问题是,是一个坏主意,有一个字符数组,因为我只存储整数?
后来我计划把这些数字输出三锹,这就是为什么我用char。



我的其他问题是,我的get方法看起来不错吗? get方法,将返回整个数组或数组的索引,这是我想要的。



我仍然在绘制过程中,这就是为什么我只有代码片段

 

code> class Card
{
public:
// TODO:提供合适的构造函数...

枚举Suit {Hearts,Diamonds,Clubs,Spades };
枚举Rank {A,K,Q,J,_10,_9,_8,_7,_6,_5,_4,_3,_2}

Suit getSuit(){return suit; }
Rank getRank(){return rank; }

// TODO:实现setters等等...

bool operator ==(const Card& c)const
{
return rank == c.rank&& suit == c.suit;
}

private:
西装;
排名;
};

所以我建议你使用枚举并使用准确的比较标准来比较两张牌。

类似地,可以实现operator>和operator <根据你的游戏规则和逻辑。



你有想法...


So I am trying to create a deck of cards and I am still kind of new to C++ and pointers so I have a few question

So I have Card class with getRank, getSuit, and comparison of two Card objects. They way I'm setting the deck is number 1-13 for the rank and 1-4 for the suit. So it's a simple compare by comparing one Card's rank+suit with another card's rank+suit.

So i initialize and declared the two arrays like this:

char Rank[13] = {1,...13};
char Suit[4] = ...

and my methods are like this:

char* getSuit(){ return Suit; }
char* getRank(){ return Rank; }
int Comp(Card *card) {
    if (rank+suit < card->rank+card->suit)
        return 1;
    else if...
    else ...
}

My question is, is it a bad idea to have a char array since i'm only storing integers? Later on i plan on converting these numbers to output "Three Spade" which is why i used char.

my other question is, does my get methods look right? The get methods, would it be returning the whole array or a index of the array, which is what I want. And am I using '->' correctly?

I still in the drawing process which is why i only have snippets of code

解决方案

Here is some draft for you to consider and probably build your class upon it:

class Card
{
public:
   // TODO: provide suitable constructor...

   enum Suit {Hearts, Diamonds, Clubs, Spades};
   enum Rank {A, K, Q, J, _10, _9, _8, _7, _6, _5, _4, _3, _2};

   Suit getSuit() { return suit; }
   Rank getRank() { return rank; }

   // TODO: implement setters, etc...

   bool operator==(const Card& c) const
   {
      return rank == c.rank && suit == c.suit;
   }

private:
   Suit suit;
   Rank rank;
};

So I suggest you to use enums and use exact comparison criterion to compare two cards.
Similarly you can implement operator> and operator< according to your game rules and logic.

You got the idea...

这篇关于在C ++中实现一张卡片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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