C++ 卡和卡组类别C++;

C++ 卡和卡组类别C++;,c++,C++,我是一个非常新手的程序员,我的任务是创建一个卡片和甲板类是有点超过我的头…我只需要在正确的方向上推动一点,因为我真的不知道我在做什么 我的卡头文件: #ifndef CARD_H #define CARD_H #include <string> #include <iostream> using namespace std; class Card { public: static const char SPADES = 'S'; static const

我是一个非常新手的程序员,我的任务是创建一个卡片和甲板类是有点超过我的头…我只需要在正确的方向上推动一点,因为我真的不知道我在做什么

我的卡头文件:

#ifndef CARD_H
#define CARD_H
#include <string>
#include <iostream>
using namespace std;
class Card
{
public:
    static const char SPADES = 'S';
    static const char CLUBS = 'C';
    static const char HEARTS = 'H';
    static const char DIAMONDS = 'D';

    Card();
    Card(int v, char s);

    const string toString();
    const int compareCard(const Card c);

private:
    int value;
    char suit;
};
#endif
#ifndef DECK_H
#define DECK_H
#include "Card.h"
#include <string>
using namespace std;
class Deck
{
public:
    Deck();
    Deck(int num);

    void addCard(Card c);
    Card getTopCard();
    Card peekTopCard();

    int getNumCards();
    bool isEmpty();

    void shuffleDeck(int list[], int size);
    void cutDeck();

private:
    static const int MAX = 52;
    static const int NUM_SUITS = 4;
    static const int NUM_VALUES = 13;
    int numCards;
    Card myCard[MAX];

};
#endif
\ifndef卡
#定义卡片
#包括
#包括
使用名称空间std;
班级卡
{
公众:
静态常量字符SPADES='S';
静态常量char lubs='C';
静态常量char HEARTS='H';
静态常量char DIAMONDS='D';
卡片();
卡片(整数v,字符s);
常量字符串toString();
常数int比较卡(常数卡c);
私人:
int值;
炭服;
};
#恩迪夫
我的卡实现文件:

#include "Card.h"
#include <iostream>
#include <string>

using namespace std;

Card::Card(int v, char s)
{
    if (v < 1 || v > 13)
    { 
            cerr << "Invalid Card value";
    }
    else
    {
        value = v;
    }

    if ((s != SPADES) && (s != HEARTS) && (s != DIAMONDS) && (s != CLUBS))
    {
        cerr << "Invalid Suit name";
    }
    else
    {
        suit = s;
    }
}

const string Card::toString()
{
    string str; 

    switch (value)
    {   
        case 1: str = "Ace of ";    
            break;
        case 2: str = "Two of ";    
            break;
        case 3: str = "Three of ";  
            break;
        case 4: str = "Four of ";   
            break;
        case 5: str = "Five of ";   
            break;
        case 6: str = "Six of ";    
            break;
        case 7: str = "Seven of ";  
            break;
        case 8: str = "Eight of ";  
            break;
        case 9: str = "Nine of ";   
            break;
        case 10: str = "Ten of ";   
                 break;
        case 11: str = "Jack of ";  
            break;
        case 12: str = "Queen of ";
            break;
        case 13: str = "King of ";  
            break;
        default: cerr << "Invalid Card value";
    }//switch

    switch (suit) 
    {   
        case SPADES: str += "Spades";   
            break;
        case HEARTS: str += "Hearts";
            break;
        case DIAMONDS: str += "Diamonds"; 
            break;
        case CLUBS: str += "Clubs"; 
            break;
        default: cerr << "Invalid Card suit";
    }//switch

    return str; 
}

const int Card::compareCard(const Card c) 
{
     int result;

     if (value == c.value)
     {
         result = 0;
     }
     else if (value == 1)
     {
         result = 1;
     }
     else if (c.value == 1)
     {
         result = -1;
     }
     else if (value < c.value)
     {
         result = 1;
     }
     else
     {
         result = -1;
     }

     return result;
}//compareCard
#include "Deck.h"
#include "Card.h"
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

Deck::Deck()
{
    char suits[4] = {Card::SPADES, Card::HEARTS, Card::DIAMONDS,   Card::CLUBS};
    int count = 0;  

    for (int s = 0; s < 4; s++) 
    {
        for (int v = 1; v <= 13; v++)
        {
            myCard[count] = Card(v, suits[s]); 
            count++;
        }           
    }//for
    int numCards = 0;
}//Deck

Deck::Deck(int num)
{
    char suits[4] = {Card::SPADES, Card::HEARTS, Card::DIAMONDS, Card::CLUBS};
    int count = 0;

    for (int s = 0; s < NUM_SUITS; s++) 
    {
        for (int v = 1; v <= NUM_VALUES; v++)
        {
            myCard[count] = Card(v, suits[s]);
            count++;
        }           
    }//for

    if ((num < 0) || (num > MAX))
    {
        cerr << "Invalid number of cards in the deck";
    }
    numCards = num;
}//Deck



void Deck::addCard(Card c)
{   
    if(numCards == MAX)
    {
        cerr << "Attempt to add to full deck";
    }//if
    else
    {
        myCard[numCards] = c;
        numCards++;
    }//else
}//addCard

Card Deck::getTopCard()
{
    Card c;

    if (numCards > 0) 
    {   
        c = myCard[0];  
        for (int i = 1; i < numCards; i++) 
        {
            myCard[i - 1] = myCard[i];
        }
    numCards--; 
    } 
    else 
    {
        cerr<<"Cannot add card from an empty deck.";
    }
    return c;   
}//getTopCard

Card Deck::peekTopCard()
{
    Card c;     

    if (numCards > 0)
    {   
        c = myCard[0];
    } 
    else 
    {
        cerr<<"Cannot add card from an empty deck.";
    }
    return c;
}//peekTopCard

int Deck::getNumCards()
{
    return numCards;
}//getNumCards

bool Deck::isEmpty()
{
    bool empty = true;
    if (numCards == 0)
    {
        empty = true;
    }
    else
    {
        empty = false;
    }

    return empty;
}//isEmpty

void Deck:: shuffleDeck(int list[], int size)
{
    srand(time(0));
    for(int i = 0; i < size; i++)
    {
        int index = rand() % MAX;
        int temp = list[i];
        list[i] = list[index];
        list[index] = temp;
    }//for
}//shuffleDeck
#包括“Card.h”
#包括
#包括
使用名称空间std;
卡片::卡片(整数v,字符s)
{
如果(v<1 | | v>13)
{ 

cerr当你开始一个新的代码库时,从小而简单开始,一次增加一点复杂度,在每一步都进行测试,在引入下一步之前让每一个加法都能完美地工作,并且永远不要添加不起作用的代码

让我们从卡片开始:

// Card.h
#ifndef CARD_H
#define CARD_H
class Card
{
 public:
  Card();

 private:
  int value;
 };
#endif


//Card.cc

#include "Card.h"
这就可以编译了。现在我们添加一个非默认构造函数,为值添加一个“getter”,并编写一个测试:


您应该一直这样做,以便在添加函数时测试它们。

我建议通过重载比较运算符来扩充类:

class Card
{
  public:
    bool operator==(const Card& other) const
    {
         return ((value == other.value) && (suite == other.suite));
    }

    bool operator< (const Card& other) const
    {
         bool is_less_than = false;
         if (suite == other.suite)
         {
             is_less_than = value < other.value;
         }
         return is_less_than;
    }
    bool operator!= (const Card& other) const
    { return !(*this == other); }
    bool operator<= (const Card& other) const
    { return (*this == other) || (*this < other); }
    bool operator>  (const Card& other) const
    { return !(*this <= other); }
    bool operator>= (const Card& other) const
    { return !(*this < other); }
};
类卡
{
公众:
布尔运算符==(常量卡和其他)常量
{
返回((value==other.value)和&(suite==other.suite));
}
bool操作员<(常数卡和其他)常数
{
bool小于等于false;
if(suite==其他.suite)
{
小于等于值<其他值;
}
回报小于;
}
布尔运算符!=(常数卡和其他)常数
{return!(*this==other);}
bool操作员(常数卡和其他)常数
{return!(*this=(const-Card和其他)const
{返回!(*此<其他);}
};

使用上述重载运算符,您可以更轻松地使用其他算法,例如std::sort或std::find。

由于某些原因,缩进没有正确发布。对此表示抱歉。您投入了很多机器,但没有任何目的。在开始实现之前,最好清楚地了解您希望类或函数做什么g it.你想让
compareCard
函数做什么?一旦你明确了这一点,编写函数就很容易了。shuffleDeck的参数应该是int list[],int size.我将函数原型保留为Deck&d.@Beta,compareCard函数比较两张卡的值并返回整数值,例如:负数表示调用对象小于参数,零表示两张卡的值相等(suit被compareCard忽略)个人的,我最喜欢的C++部分之一是覆盖运算符的能力。我认为 CARDA.CADBD/COD>比 CARDA(CARDB)更可读。<0
。因此,我会从重载开始,并=运算符。如果我有时间,我会将其全部删除,然后重新开始。但我没有时间,因为作业将于明晚11:59前完成。我只是没有掌握编写此作业所需的概念。这只是我的第二门编程课程,我想他对le的要求有点高相信我,这是最快的方法。把你写的(
Card.h
Card.cc
Deck.h
Deck.cc
)把它放到一个目录中保存,然后试试我的方法。你看到错误了吗?这是一个出现在你的代码中的错误,所以你必须解决它,这是最简单的方法。你试过编译它吗?编译器可以帮你发现错误。@mkjo0617,一个好的编译器会告诉你
v
没有在
getV范围内声明value(),实施并测试,重复。谢谢。我今天会尝试。昨晚我最终放弃了,因为我有点神经崩溃。你的比较不符合逻辑。两张不同西装的卡片总是被认为是“少”的根据您的实现,它们之间的顺序会不同。@Mosterd:比较取决于用户的定义。在我的回答中,排序首先取决于套装,然后取决于面值。这将确保套装的所有卡片都是一起排序的。您可能对“少于”有不同的解释您可以编写自己的不同比较方法。
#include "Deck.h"
#include <iostream>
using namespace std;

int main()
{
  Deck D;
  D.addCard(Card(10,'D'));
  D.addCard(Card(4,'C'));
  cout << D.getNumCards() << endl;

  D.cutDeck();

  while(!D.isEmpty())
    {
      Card C = D.getTopCard();
      cout << C.toString() << endl;
    }

  return(0);
}
class Card
{
  public:
    bool operator==(const Card& other) const
    {
         return ((value == other.value) && (suite == other.suite));
    }

    bool operator< (const Card& other) const
    {
         bool is_less_than = false;
         if (suite == other.suite)
         {
             is_less_than = value < other.value;
         }
         return is_less_than;
    }
    bool operator!= (const Card& other) const
    { return !(*this == other); }
    bool operator<= (const Card& other) const
    { return (*this == other) || (*this < other); }
    bool operator>  (const Card& other) const
    { return !(*this <= other); }
    bool operator>= (const Card& other) const
    { return !(*this < other); }
};