1

我是一个非常新手的程序员,我创建 Card and Deck 类的任务有点过头了……我只需要朝着正确的方向努力,因为我真的不知道自己在做什么。

我的卡头文件:

#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

我的卡实现文件:

#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

我的甲板头文件:

#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

我的 Deck 实现文件:

#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
4

2 回答 2

3

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

让我们从Card

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

 private:
  int value;
 };
#endif


//Card.cc

#include "Card.h"

这编译。现在我们为值添加一个非默认构造函数,一个“getter”,并编写一个测试:

// in Card.h:

Card(int v);
int getValue();

// in Card.cc:

Card::Card(int v)
{
  value=v;
}

int Card::getValue()
{
  return(v);
}

// in testCard.cc:

#include <iostream>
#include "Card.h"
using namespace std;

int main()
{
  Card X(5);

  cout << "card is " << X.getValue() << endl;

  return(0);
}

这不编译;它有一个错误。在添加更多内容之前找到错误并修复它。

如果遇到无法破解的问题,请随时寻求帮助。等到你遇到 和 之类的东西时cutshuffle你就会轻松解决很多小问题。

编辑:

您在 中声明了默认构造函数 ( Card()) Card.h,但未在 中定义它Car.cc。有些地方会Deck尝试构建不带参数的卡片(例如Card c;),而链接器没有可插入的内容。将默认构造函数添加到Card.cc.

编辑:

shuffleDeck看起来像一个正在进行的工作。如果您遇到问题,我建议您swapCards(int, int)先尝试实施。

编辑:

您发布的代码分为两类。是否有使用它们的主要例程?main()如果没有 a在某个地方,您将无法构建可执行文件(库是,可执行文件否)。

编辑:

您应该能够使用您所拥有的构建目标文件 (Card.oDeck.o)。我建议您编写(并构建和运行)一个测试例程,例如:

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

您应该一直这样做,以便在添加功能时测试您的功能。

于 2012-10-04T20:50:14.130 回答
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); }
};

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

于 2012-10-05T19:56:01.197 回答