• Write the Card class in file card.cpp as shown to the right, with a regression
ID: 3744761 • Letter: #
Question
• Write the Card class in file card.cpp as shown to the right, with a regression test in file test.cpp. 1 If the Card constructor’s Suit or Rank parameter is invalid, throw a runtime_error. •
Write the Deck class in file deck.cpp as shown to the right, with a regression test in file decktest.cpp. If Deck::deal is called when the deck is empty, throw a Deck_empty exception. • Write an application in file main.cpp that instances and prints out a deck of 20 random cards (you do NOT need to ensure that each card is unique
)
Deck:Deck empty I exception Deck Card enum Suit -suit:Suit Card suit):Suit card to stringl: string -deck: vectors Card Deck(cards: int-10)rank: Rank + deal): Card suit/U Card(suit: Suit, rank: Rank) +rank): Rank edatatype» Rank rankExplanation / Answer
********************************************
Card.h
********************************************
#ifndef CARD_H_
#define CARD_H_
#include <string>
using namespace std;
enum _suit
{
CLUBS = 1,
DIAMONDS,
HEARTS,
SPADES,
MAX_SUITS
};
enum _validRank
{
ACE = 1,
ONE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
MAX_RANK
};
string suits[MAX_SUITS-1] = {"CLUBS","DIAMONDS","HEARTS","SPADES"};
string ranks[MAX_RANK-1] = {"ACE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE","TEN","JACK",
"QUEEN","KING"};
typedef int _rank;
class Card {
_suit Suit;
_rank Rank;
public:
Card();
Card(_suit s, _rank r);
_suit suit();
_rank rank();
string card_to_string();
void error(const string& s){
throw runtime_error(s);
}
virtual ~Card();
};
#endif /* CARD_H_ */
*********************************************
Card.cpp
*********************************************
#include "Card.h"
Card::Card() {
Suit = CLUBS;
Rank = ACE;
}
Card::Card(_suit s, _rank r) {
if(s<CLUBS || s>SPADES) error("Value of suit is invalid");
if(r<ACE || r>KING) error("Value of Rank is invalid");
Suit = s;
Rank = r;
}
Card::~Card() {
}
_rank Card::rank()
{
return Rank;
}
_suit Card::suit()
{
return Suit;
}
string Card::card_to_string()
{
return ranks[Rank] + " of " + suits[Suit];
}
******************************************
Card_test.cpp
******************************************
#include "Card.h"
int test_card(_suit s, _rank r)
{
try{
Card c(s,r);
}catch(runtime_Exception &e)
{
cout<<e.what();
}
cout<<c.card_to_string();
}
int main()
{
test_card(CLUBS,10);
test_card(MAX_SUITS,14);
return 0;
}
******************************************
Deck.h
******************************************
#ifndef DECK_H_
#define DECK_H_
#include <vector>
#include <Card.h>
#include <time.h>
#include <exception>
using namespace std;
class Deck_Exception : public exception{
public:
const char * what() const throw()
{
return "Deck is Empty Exception ";
}
};
class Deck {
vector<Card> _deck;
public:
Deck_Exception d;
Deck(int num);
Card deal();
virtual ~Deck();
};
#endif /* DECK_H_ */
**************************************
Deck.cpp
**************************************
#include "Deck.h"
Deck::Deck(int num=10) {
srand (time(NULL));
_deck.reserve(num);
for(int i = 0; i<num; i++)
{
int r,s;
s = rand()%4 + 1;
r = rand()%13 + 1;
try{
Card c(s,r);
}catch(runtime_exception &e)
{
cout<<"Runtime Exception : "<<e.what();
}
_deck.push_back(c);
}
}
Deck::~Deck() {
}
Card Deck::deal()
{
if(_deck.empty()) throw d;
return _deck.pop_back();
}
***********************************
Deck_test.cpp
***********************************
#include "Deck.h"
int test_deck(int num)
{
Deck d(num);
for(int i =0; i < 10; i++)
{
try{
d.deal();
}catch(Deck_Exception &e)
{
cout<<"Exception: "<<e.what();
}
}
}
int main()
{
test_deck(10);
test_deck(9);
return 0;
}
************************************
main.cpp
************************************
#include <Deck.h>
int main()
{
Deck _deckObj(52);
for(int i = 0; i<20; i++)
{
Card &c = _deckObj.deal();
cout<<c.card_to_string();
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.