我是一个非常新手的程序员,我创建卡片和甲板课程的任务有点过头了......我只需要向正确的方向推进一点,因为我真的不知道我在做什么。
我的卡片头文件:
#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
My Deck头文件:
#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
My 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
答案 0 :(得分: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);
}
这不编译;它有一个bug。 在添加更多内容之前找到错误并进行修复。
如果遇到无法破解的问题,请随时寻求帮助。当你到达cut
和shuffle
之类的东西时,你就可以轻松解决许多小问题。
修改
您在Card()
中声明了默认构造函数(Card.h
),但未在Car.cc
中定义它。有些地方Deck
尝试构造一个没有参数的卡(例如Card c;
),并且链接器没有任何东西插入那里。将默认构造函数添加到Card.cc
。
修改
您的shuffleDeck
看起来像正在进行的工作。如果您遇到问题,我建议您先尝试实施swapCards(int, int)
。
修改
您发布的代码有两个类。是否有使用它们的主程序?如果某个地方没有main()
,则无法构建可执行文件(库是,可执行文件号)。
修改
您应该能够使用您拥有的内容构建目标文件(Card.o
和Deck.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);
}
您应该一直这样做,以便在添加时测试您的功能。
答案 1 :(得分: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。