mkjo0617
mkjo0617

Reputation: 33

Card and Deck classes C++

I am a very novice programmer and my assignment to create a Card and Deck class is a little bit over my head...I just need a little push in the right direction because I really have no clue what I am doing.

My Card header file:

#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

My Card implementation file:

#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 header file:

#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 implementation file:

#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

Upvotes: 1

Views: 18271

Answers (2)

Beta
Beta

Reputation: 99154

When you're starting a new codebase, start small and simple, add complexity a little at a time, test at every step, get every addition working perfectly before you introduce the next, and never add to code that doesn't work.

Let's start with Card:

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

 private:
  int value;
 };
#endif


//Card.cc

#include "Card.h"

This compiles. Now we add a non-default constructor, a "getter" for the value, and write a test:

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

This doesn't compile; it has a bug. Find the bug and fix it before you add anything more.

Feel free to ask for help if you run into a problem you can't crack. By the time you get to things like cut and shuffle, you'll have solved a lot of small problems with ease.

EDIT:

You declare a default constructor (Card()) in Card.h, but you don't define it in Car.cc. There are places where Deck tries to construct a card without arguments (e.g. Card c;), and the linker has nothing to plug in there. Add a default constructor to Card.cc.

EDIT:

And your shuffleDeck looks like a work in progress. If you're having trouble, I suggest you try implementing swapCards(int, int) first.

EDIT:

The code you've posted is of two classes. Is there a main routine that uses them? You can't build an executable without a main() in there someplace (library yes, executable no).

EDIT:

You should be able to build the object files (Card.o and Deck.o) with what you have. I'd advise you to write (and build, and run) a test routine, something like:

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

You should have been doing this all along, to test your functions as you added them.

Upvotes: 3

Thomas Matthews
Thomas Matthews

Reputation: 57749

I suggest augmenting your class by overloading the comparison operators:

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

With the above overloaded operators, you can use other algorithms easier, such as std::sort, or std::find.

Upvotes: 0

Related Questions