Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ help Well continue the Crazy 8s implementation here (Actual code from previous homework 3 below instructions) Requirements: The discard pile in hw3 was, as

C++ help

Well continue the Crazy 8s implementation here (Actual code from previous homework 3 below instructions)

Requirements: The discard pile in hw3 was, as stated there, faked by the top card. The behavior of the discard pile is mostly that of a stack, so you will replace the top card by a Standard Template Library stack. The discard pile is characteristic of many matching card games, so you will continue to implement it there. You wont need to modify the other classes.

The MatchingCardGame class (in matchingCardGame.h)

Add a stack here to represent the discard pile (what should the element type be?)

A getter topCard() which returns the top Card.

A public function play(Card &) which calls canPlay() from hw3. If the Card is allowed, then add the card to the top of the discard pile. If not, throw a CardNotPlayable exception.

Implement CardNotPlayable as a child class of the standard< exception> class

The driver should, as usual, test the above code.

As with hw3, hard-code the test cases to save time and to make sure you cover the different scenarios

Try the same 4 cases as in hw3, but catch the CardNotPlayable exception for the not playable case and indicate that this is the desired result

Grading:

MatchingCardGame 50%

CardNotPlayable 10%

driver 20%

Comments count for 10%: something for each file/class, something for each method/function, other comments to explain parts of the code when it's not clear

Choosing appropriate names for variables, functions, constants, classes counts for 10%

Using Code from Previous HW3 below:

------------------------------------------------

card.h

---------

#ifndef CARD_H #define CARD_H #include

using namespace std;

// Simple version of a class for playing cards, like for poker or Hearts class Card { string rank, suit; public: // no default constructor to ensure both rank and suit are initialized Card(string _rank, string _suit) : rank(_rank), suit(_suit) {} // return true if same rank as parameter card bool sameRank(Card & other) const { return rank == other.rank; } // return true if same suit as parameter card bool sameSuit(Card & other) const { return suit == other.suit; } };

#endif // CARD_H

----------------------------------------------

crazy8s.h

-------------

#ifndef CRAZY8S_H #define CRAZY8S_H #include "matchingCardGame.h"

// For the Crazy 8's card game class Crazy8s : public MatchingCardGame { public: // Should return true if either rank or suit matches, or // if wild card (8) is played bool canPlay(Card &card) { static const Card EIGHT_CLUBS("8", "Clubs"); return (topCard->sameRank(card) || topCard->sameSuit(card) || EIGHT_CLUBS.sameRank(card)); } };

#endif // CRAZY8S_H

---------------------------------

hw3driver.cpp

------------------

#include "crazy8s.h" #include

// Driver to test Card class's sameRank, sameSuit work, // and if Crazy8s' method canPlay() works

using namespace std;

int main() { MatchingCardGame * game = new Crazy8s(); Card top("Ace", "Clubs"), toPlaySameRank("Ace", "Spades"), toPlaySameSuit("2", "Clubs"), toPlay8("8", "Diamonds"), toPlayNot("Jack", "Hearts"); game->setTop(&top);

// Check if sameRank works cout << boolalpha << top.sameRank(toPlaySameRank) << "Ace of Clubs and Ace of Spades have same rank" << endl; cout << boolalpha << top.sameRank(toPlaySameSuit) << "Ace of Clubs and 2 of Clubs have same rank" << endl; // Check if sameSuit works cout << boolalpha << top.sameSuit(toPlaySameRank) << "Ace of Clubs and Ace of Spades have same suit" << endl; cout << boolalpha << top.sameSuit(toPlaySameSuit) << "Ace of Clubs and 2 of Clubs have same suit" << endl;

// Check canPlay cout << game->canPlay(toPlaySameRank) << ": Card to play is same rank as top card" << endl; cout << game->canPlay(toPlaySameSuit) << ": Card to play is same suit as top card" << endl; cout << game->canPlay(toPlay8) << ": Card to play is wild card 8" << endl; cout << game->canPlay(toPlayNot) << ": Card to play is not ok to play" << endl;

return 0;

----------------------------------------------

matchingCardGame.h

---------------------------

#ifndef MATCHINGCARDGAME_H #define MATCHINGCARDGAME_H #include "card.h"

// Abstract class representing matching card games class MatchingCardGame { protected: Card *topCard; // temporary -- complete implementation should have a deck public: // Set displayed card that players are trying to match void setTop(Card * card) { topCard = card; } // Returns true if parameter card matches displayed card // false if not virtual bool canPlay(Card & card) =0;

};

#endif // MATCHINGCARDGAME_H

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sql All In One For Dummies 7 Books In One

Authors: Allen G Taylor ,Richard Blum

4th Edition

1394242298, 978-1394242290

More Books

Students also viewed these Databases questions

Question

1. What makes Zipcar an attractive employer for whom to work?

Answered: 1 week ago

Question

Compute the derivative f(x)=(x-a)(x-b)

Answered: 1 week ago