我是一个非常新手的程序员,我创建 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