Question Problem 1 In a source file carddeckcpp provide an i
Question:
Problem 1: In a source file carddeck.cpp, provide an implementation for the Card class, i.e., please provide an implementation for all the member functions defined in the class. Observe that there are two constructors: a default constructor to create the card representing Ace of Spades (or AS) and a second constructor that defines a custom card with given rank and suit. The member function toString() should return a two character string representation of a card, i.e. it would return QH for Queen of Hearts, TD for Ten of Diamonds, 5S for Five of Spades, etc.
The objective of this assignment is to write classes to simulate a deck of cards. Recall that a deck of cards consists of 52 cards consisting of the 13 ranks (2, 3, 4, 5, 6, 7, 8, 9, T(ten), J(jack), Q(queen), K(kind), and A(ace) paired with the four suits CLUBS, DIAMONDS, HEARTS, and SPADES. Consider the following header file carddeck.h:
#ifndef CARDDECK_H
#define CARDDECK
#include <string>
using namespace std;
enum Suit
{
CLUBS, DIAMONDS, HEARTS, SPADES
};
class Card
{
private:
char rank; // use digit (e.g., 4) for cards valued 2 through 9, \'T\' for 10,
// \'J\',\'Q\',\'K\',\'A\' for Jack, Queen, King, and Ace
Suit suit;
public:
Card(); // creates Ace of Spades as default card
Card(char,Suit); // creates card with given rank and suit
char getRank() const;
Suit getSuit() const;
void setRank(char);
void setSuit(Suit);
string toString() const; // creates text representation of card, e.g. \"2C\" for 2
// of Clubs, \"KH\" for King of Hearts, etc.
};
class CardDeck
{
private:
Card* deck;
Card* next_card; // keeps track of where we are in a deck after dealing some cards
//(initially, next_card == deck)
public:
CardDeck(); // sets member variable deck to be an array of 52 Card objects
// representing deck of cards
~CardDeck();
void shuffle(); // randomly shuffles the deck (you will need to think about how
// to do this). also, this function should reset next_card =
//deck
Card* dealHand(int); // deals a hand of N cards where N is the input to the
// function. this function should also increment the
// next_card pointer by N cards.
};
#endif
Solution
card.h
#ifndef CARD_H
#define CARD_H
#include <string>
const int SUIT_MAX(4);
const int RANK_MAX(13);
class Card
{
friend class Deck; // Deck Class needs to access to Card Class but not vice versa
public:
explicit Card();
explicit Card(const int &suit, const int &rank);
std::string Card2Str() const;
private:
int generate_suit();
int generate_rank();
int get_suit() const;
int get_rank() const;
int m_suit;
int m_rank;
};
#endif
card.cpp
#include <stdlib.h> /* srand, rand */
#include \"card.h\"
#include <iostream>
const std::string SUIT[SUIT_MAX] = {\"S\", \"H\", \"D\", \"C\"};
const std::string RANK[RANK_MAX] = {\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"J\",\"Q\",\"K\",\"A\"};
Card::Card()
{
m_suit = generate_suit();
m_rank = generate_rank();
}
Card::Card(const int &suit, const int &rank) : m_suit(suit), m_rank(rank)
{
}
int Card::generate_suit()
{
return rand() % (SUIT_MAX-1) + 0;
}
int Card::generate_rank()
{
return rand() % (RANK_MAX-1) + 0;
}
std::string Card::Card2Str() const
{
return SUIT[get_suit()] + RANK[get_rank()];
}
int Card::get_suit() const
{
return m_suit;
}
int Card::get_rank() const
{
return m_rank;
}
deck.h
#ifndef DECK_H
#define DECK_H
#include <vector>
#include <iostream>
#include <fstream>
#include \"card.h\"
using namespace std;
class Deck
{
public:
explicit Deck();
void print_Deck() const;
void getOneCard();
private:
std::vector<Card> m_deck;
};
#endif
deck.cpp
#include <iostream>
#include \"deck.h\"
Deck::Deck()
{
for (unsigned int i(0); i < SUIT_MAX; ++i)
{
for (unsigned int j(0); j < RANK_MAX; ++j)
{
Card card(i, j);
m_deck.push_back(card);
}
}
}
void Deck::print_Deck() const
{
unsigned int count(1);
for (unsigned int i(0); i < m_deck.size(); ++i)
{
std::cout << m_deck[i].Card2Str() << \" \";
if ( count == 13 )
{
std::cout << std::endl;
count = 0;
}
++count;
}
}
void Deck::getOneCard()
{
Card cd(m_deck.back().get_suit(), m_deck.back().get_rank());
m_deck.pop_back();
std::cout << cd.Card2Str() << std::endl;
}
main.cpp
#include <iostream>
#include <vector>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <string>
#include \"card.h\"
#include \"deck.h\"
int main()
{
srand (time(NULL));
Deck _deck;
_deck.print_Deck();
_deck.getOneCard();
std::cout << std::endl;
_deck.print_Deck();
std::cout << std::endl;
return 0;
}




