Question

In: Computer Science

C++ Question Create a class for a card in a deck of playing cards. The object...

C++ Question

Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run.

This is more basic programming so please keep it simple and use things like:

namespace std, use cout/cin instead of printf, and please leave simple comments explaining what you're doing in the code. Please just label which part of the code it is since you can't send files on here.

Solutions

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

Screenshot of Output:


Related Solutions

Write a class whose instances represent a single playing card from a deck of cards. Playing...
Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Use an enum type to represent rank and another enum type to represent suit. Write another class whose instances represent a full deck of cards. Both these classes should have toString methods. Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and...
(In C++) Write a class that will represent a card in a standard deck of playing...
(In C++) Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Write methods to • Initialize the deck of cards • Perform a perfect shuffle In a perfect shuffle, the deck is broken exactly in half and rearranged so that...
C++ In this assignment you will use your Card class to simulate a deck of cards....
C++ In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows: The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers. The destructor will free the...
Question # 6. (a) From a standard 52 card deck of playing cards, how many 5...
Question # 6. (a) From a standard 52 card deck of playing cards, how many 5 card hands, containing exactly 3 Kings, are possible? (b) From a standard 52 card deck of playing cards, how many 5 card hands, containing exactly 1 Diamond and at most 2 Spades, are possible? (c) From a standard 52 card deck of playing cards, how many 5 card hands, containing exactly 1 Spade and twice as many Hearts as Clubs, are possible? (d) From...
The following question involves a standard deck of 52 playing cards. In such a deck of...
The following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
he following question involves a standard deck of 52 playing cards. In such a deck of...
he following question involves a standard deck of 52 playing cards. In such a deck of cards there are four suits of 13 cards each. The four suits are: hearts, diamonds, clubs, and spades. The 26 cards included in hearts and diamonds are red. The 26 cards included in clubs and spades are black. The 13 cards in each suit are: 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace. This means there are four...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a three-of-a-kind hand if it contains exactly three cards of the same rank (e.g. 3 aces and 2 other cards). How many distinct three-of-a-kind hands can be dealt with? Calculate a numeric answer.
Your friend has chosen a card from a standard deck of 52 playing cards and no...
Your friend has chosen a card from a standard deck of 52 playing cards and no one knows the card except himself. Now you have to guess the unknown card. Before guessing the card, you can ask your friend exactly one question, the question must be either Q1, Q2 or Q3 below: Q1. whether the chosen card is an ace (A)? Q2. whether the chosen card is a spade (♠)? Q3. whether the chosen card is the ace of spades...
1. A card is drawn at random from an ordinary deck of 52 playing cards. Describe...
1. A card is drawn at random from an ordinary deck of 52 playing cards. Describe the sample space if consideration of suits (a) is not, (b) is, taken into account. 2. Answer for b: both a king and a club = king of club. 3. A fair die is tossed twice. Find the probability of getting a 4, 5, or 6 on the first toss and a 1, 2, 3, or 4 on the second toss. 4. Find the...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the...
A 5-card hand is dealt from a perfectly shuffled deck of playing cards. What is the probability of each of the following events: a) The hand has at least one club b) The hand has at least two cards with the same rank c) The hand has exactly one club or exactly one spade d) The hand has at least one club or at least one spade
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT