Question

C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement...

C++ help

This assignment gives you practice with inheritance and pure virtual functions. You will implement some code that could be part of a program to play a popular card game called “Crazy 8’s”, which is the basis for the game “Uno”. You will start to implement the rules, as described at: https://www.pagat.com/eights/crazy8s.html . Note that the inheritance relationship below is also based on the organization of card games described on that website.

Requirements: your work will be split into 3 classes, which should be in the files “card.h”, “matchingCardGame.h” and “crazy8s.h”. There will be a test driver again in the file “hw3driver.cpp”. Please treat the file names and class/method names as case-sensitive.

The Card class (in “card.h”)

Private fields for rank and suit, both of which are of type string. Ex: “2” and “Hearts” – you are to assume the values are valid

The method functions, here and in the other classes, are all public

A constructor that takes the rank and the suit as parameters and initializes the fields from the parameters

sameRank(Card &) const : returns true if this and the parameter card have the same rank, false if not

sameSuit(Card &) const: returns true if this and the parameter card have the same suit, false if not

The MatchingCardGame class (in “matchingCardGame.h”)

This is supposed to only be a partial implementation – you will get to fill in the rest on a later assignment

A protected pointer to a Card variable that points to the top card of the deck – this is in place of the deck

A setter setTop(Card *) which initializes the field. Note that when you complete the implementation, you will be replacing the pointer and this setter.

A pure virtual function canPlay(Card &) which returns true if it is valid to play the parameter card on top of the above top card

The Crazy8s class (in “crazy8s.h”)

This is a subclass of MatchingCardGame

The only thing required here is an implementation of canPlay which satisfies the following:

A card can be played if it is of the same rank as the top card

A card can be played if it is of the same suit as the top card

A card can be played if it’s rank is “8” (there is a feature that whoever plays the 8 then chooses what the next suit must be, but we will skip that requirement for now)

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

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

To make sure you have implemented the inheritance relationship properly, declare a pointer to a MatchingCardGame and use that to point to a Crazy8s object. This would be like how the in-class exercise had you declare a pointer to a Polygon and use that to point to the Rectangle or the Triangle. Use this pointer to make the calls to test canPlay()

Grading:

Card 20%

MatchingCardGame 20%

Crazy8s 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%

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int shuffle(int [], int size);
int deal_card(int x);

int main()

{
char choice;
int player_hand[52], i, play, in_play;
int comp_cards;
int deck[52];
int const deckSize = 52;
//while(choice != 'e' && choice != 'E')

{
printf("You and the computer will be dealt 7 cards each.\n");
printf("The next card in the deck becomes the in-play card.\n");
printf("You can play either a card of the same number or suit as the in-play card.\n");
printf("You may however play an 8 at any time and choose the suit.\n");
printf("The first player to use all the cards in his hand wins the round.\n");
printf("\n");
while(choice != 'P' && choice!='p' && choice !='e' && choice != 'E')

{
printf("Enter P to play or E to exit: ");
scanf(" %c",&choice);
}
if(choice == 'E' || choice == 'e')

{
printf("Thanks for playing!\n");
//break;
}
else if(choice == 'p' || choice == 'P')

{
printf("Begin Game.\n");
}
shuffle(deck, deckSize);
printf("Your hand:\n");
player_hand[0]=deal_card(deck[0]);
player_hand[1]=deal_card(deck[2]);
player_hand[2]=deal_card(deck[4]);
player_hand[3]=deal_card(deck[6]);
player_hand[4]=deal_card(deck[8]);
player_hand[5]=deal_card(deck[10]);
player_hand[6]=deal_card(deck[12]);
printf("\n");
printf("The in-play card is: ");
deal_card(deck[14]);
printf("Which card would you like to play?: ");
scanf("%d",&play);
if(play == 0)

{
printf("You drew: ");
deal_card(deck[15]);
}
else if(play == 1)

{
if(in_play<14){
if(player_hand[0]>=0 && player_hand[0]<=13)

{
in_play=deal_card(deck[0]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[0]>13 && player_hand[0]<=26)

{
in_play=deal_card(deck[0]);
}
else{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26)

{
if(player_hand[0]>26 && player_hand[0]<=39)

{
in_play=deal_card(deck[0]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[0]>39 && player_hand[0]<=52)

{
in_play=deal_card(deck[0]);
}
else

{
printf("You can't play that card.\n");
}
}
}


else if(play == 2)

{
if(in_play<14)

{
if(player_hand[1]>=0 && player_hand[1]<=13)

{
in_play=deal_card(deck[2]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[1]>13 && player_hand[1]<=26)

{
in_play=deal_card(deck[2]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26)

{
if(player_hand[1]>26 && player_hand[1]<=39)

{
in_play=deal_card(deck[2]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[1]>39 && player_hand[1]<=52)

{
in_play=deal_card(deck[2]);
}
else

{
printf("You can't play that card.\n");
}
}
in_play=deal_card(deck[2]);
}

else if(play == 3)

{
if(in_play<14)

{
if(player_hand[3]>=0 && player_hand[3]<=13)

{
in_play=deal_card(deck[4]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[3]>13 && player_hand[3]<=26)

{
in_play=deal_card(deck[4]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26)

{
if(player_hand[3]>26 && player_hand[3]<=39)

{
in_play=deal_card(deck[4]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[3]>39 && player_hand[3]<=52)

{
in_play=deal_card(deck[4]);
}
else

{
printf("You can't play that card.\n");
}
}
in_play=deal_card(deck[4]);
}

else if(play == 4)

{
if(in_play<14){
if(player_hand[4]>=0 && player_hand[4]<=13)

{
in_play=deal_card(deck[6]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[4]>13 && player_hand[4]<=26)

{
in_play=deal_card(deck[6]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26){
if(player_hand[4]>26 && player_hand[4]<=39)

{
in_play=deal_card(deck[6]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[4]>39 && player_hand[4]<=52)

{
in_play=deal_card(deck[6]);
}
else

{
printf("You can't play that card.\n");
}
}
in_play=deal_card(deck[6]);
}

else if(play == 5)

{
if(in_play<14)

{
if(player_hand[5]>=0 && player_hand[5]<=13)

{
in_play=deal_card(deck[8]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[5]>13 && player_hand[5]<=26)

{
in_play=deal_card(deck[8]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26)

{
if(player_hand[5]>26 && player_hand[5]<=39)

{
in_play=deal_card(deck[8]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[5]>39 && player_hand[5]<=52)

{
in_play=deal_card(deck[8]);
}
else

{
printf("You can't play that card.\n");
}
}
in_play=deal_card(deck[8]);
}

else if(play == 6)

{
if(in_play<14)

{
if(player_hand[6]>=0 && player_hand[6]<=13)

{
in_play=deal_card(deck[10]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[6]>13 && player_hand[6]<=26)

{
in_play=deal_card(deck[10]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26)

{
if(player_hand[6]>26 && player_hand[6]<=39)

{
in_play=deal_card(deck[10]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[6]>39 && player_hand[6]<=52)

{
in_play=deal_card(deck[10]);
}
else

{
printf("You can't play that card.\n");
}
}
in_play=deal_card(deck[10]);
}

if(in_play<14)

{
if(player_hand[7]>=0 && player_hand[7]<=13)

{
in_play=deal_card(deck[12]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<27 && in_play>13)

{
if(player_hand[7]>13 && player_hand[7]<=26)

{
in_play=deal_card(deck[12]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<40 && in_play>26)

{
if(player_hand[7]>26 && player_hand[7]<=39)

{
in_play=deal_card(deck[12]);
}
else

{
printf("You can't play that card.\n");
}
}
else if(in_play<53 && in_play>39)

{
if(player_hand[7]>39 && player_hand[7]<=52)

{
in_play=deal_card(deck[12]);
}
else

{
printf("You can't play that card.\n");
}
}
in_play=deal_card(deck[12]);
}
else

{
printf("Not a valid card.\n");
}
printf("In-play card is now: ");
deal_card(deck[in_play]);
//}
return 0;
}

int shuffle(int x[], int size){ //This function shuffles the deck
int i;
int index1;
int index2;
int diamond;
int heart;
int club;
int spade;
srand(time(NULL));
for(i=0;i<size;++i){ //This loop initaializes the deck.
x[i] = i+1;
}
for(i=0;i<size;++i)

{
index1 = rand()%size;
index2 = rand()%size;
//The following 3 lines swap pairs of cards.
int temporary = x[index1];
x[index1] = x[index2];
x[index2] = temporary;
}
return i;
}

int deal_card(int i){ //This function sets all cards to their proper suit and number.
if(i == 1)

{
printf("2 of Diamonds\n");
}
else if(i == 2)

{
printf("3 of Diamonds\n");
}
else if(i == 3)

{
printf("4 of Diamonds\n");
}
else if(i == 4)

{
printf("5 of Diamonds\n");
}
else if(i == 5)

{
printf("6 of Diamonds\n");
}
else if(i == 6)

{
printf("7 of Diamonds\n");
}
else if(i == 7)

{
printf("8 of Diamonds\n");
}
else if(i == 8)

{
printf("9 of Diamonds\n");
}
else if(i == 9)

{
printf("10 of Diamonds\n");
}
else if(i == 10)

{
printf("Jack of Diamonds\n");
}
else if(i == 11)

{
printf("Queen of Diamonds\n");
}
else if(i == 12)

{
printf("King of Diamonds\n");
}
else if(i == 13)

{
printf("Ace of Diamonds\n");
}
else if(i == 14)

{
printf("2 of Hearts\n");
}
else if(i == 15)

{
printf("3 of Hearts\n");
}
else if(i == 16)

{
printf("4 of Hearts\n");
}
else if(i == 17)

{
printf("5 of Hearts\n");
}
else if(i == 18)

{
printf("6 of Hearts\n");
}
else if(i == 19){


printf("7 of Hearts\n");
}
else if(i == 20)

{
printf("8 of Hearts\n");
}
else if(i == 21)

{
printf("9 of Hearts\n");
}
else if(i == 22)

{
printf("10 of Hearts\n");
}
else if(i == 23)

{
printf("Jack of Hearts\n");
}
else if(i == 24)

{
printf("Queen of Hearts\n");
}
else if(i == 25)

{
printf("King of Hearts\n");
}
else if(i == 26)

{
printf("Ace of Hearts\n");
}
else if(i == 27)

{
printf("2 of Clubs\n");
}
else if(i == 28)

{
printf("3 of Clubs\n");
}
else if(i == 29)

{
printf("4 of Clubs\n");
}
else if(i == 30)

{
printf("5 of Clubs\n");
}
else if(i == 31)

{
printf("6 of Clubs\n");
}
else if(i == 32)

{
printf("7 of Clubs\n");
}
else if(i == 33)

{
printf("8 of Clubs\n");
}
else if(i == 34)

{
printf("9 of Clubs\n");
}
else if(i == 35)

{
printf("10 of Clubs\n");
}
else if(i == 36)

{
printf("Jack of Clubs\n");
}
else if(i == 37)

{
printf("Queen of Clubs\n");
}
else if(i == 38)

{
printf("King of Clubs\n");
}
else if(i == 39){
printf("Ace of Clubs\n");
}
else if(i == 40)

{
printf("2 of Spades\n");
}
else if(i == 41)

{
printf("3 of Spades\n");
}
else if(i == 42)

{
printf("4 of Spades\n");
}
else if(i == 43)

{
printf("5 of Spades\n");
}
else if(i == 44)

{
printf("6 of Spades\n");
}
else if(i == 45)

{
printf("7 of Spades\n");
}
else if(i == 46)

{
printf("8 of Spades\n");
}
else if(i == 47)

{
printf("9 of Spades\n");
}
else if(i == 48)

{
printf("10 of Spades\n");
}
else if(i == 49)

{
printf("Jack of Spades\n");
}
else if(i == 50)

{
printf("Queen of Spades\n");
}
else if(i == 51)

{
printf("King of Spades\n");
}
else if(i == 52)

{
printf("Ace of Spades\n");
}
return i;
}

Add a comment
Know the answer?
Add Answer to:
C++ help This assignment gives you practice with inheritance and pure virtual functions. You will implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • hello there, i have to implement this on java processing. can someone please help me regarding...

    hello there, i have to implement this on java processing. can someone please help me regarding that? thanks War is the name of a popular children’s card game. There are many variants. After playing War with a friend for over an hour, they argue that this game must never end . However! You are convinced that it will end. As a budding computer scientist, you decide to build a simulator to find out for sure! You will implement the logic...

  • Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriat...

    Use inheritance and classes to represent a deck of playing cards. Create a Cardclass that stores the suit (e.g. Clubs, Diamonds, Hearts, Spades), and name (e.g. Ace, 2, 10, Jack) along with appropriate accessors, constructors, and mutators. Next, create a Deck class that stores a vector of Card objects. The default constructor should create objects that represent the standard 52 cards and store them in the vector. The Deck class should have functions to: • Print every card in the...

  • C++ Implement a templated class list and listnode. You may add methods/functions as you see fit....

    C++ Implement a templated class list and listnode. You may add methods/functions as you see fit. Test these classes. I have left all of the implementation as an exercise for you. template< class NODETYPE > class List;  // forward declaration template<class NODETYPE> class ListNode {    friend class List< NODETYPE >; // make List a friend public:    ListNode( const NODETYPE &newData);  // copy constructor    NODETYPE getData() const;      // return data in the node private:    NODETYPE data;                 // data    ListNode< NODETYPE > *nextPtr; // next node...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement...

    C++ Practice: Answer Whichever you can & it'll help a lot. Thank you! Question 1. Implement the constructors and member function of each of the classes (Marks 15) class Fraction{ private: int numerator; int denominator; public: Fraction(int, int); float fractionValue();//determines the value of numerator/denominator }; class Problem{ private: Fraction f[3]; public: Problem(int, int, int, int, int, int); Fraction largestFraction();//Returns the fraction having largest fraction value }; Question 2: In the following Inheritance problem #include<iostream> #include<string> using namespace std; class Animal{...

  • HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include...

    HELP NEED!! Can some modified the program Below in C++ So that it can do what the Problem below asked it today???? #include <iostream> #include <stdlib.h> #include <string> #include <time.h> /* time */ #include <sstream> // for ostringstream using namespace std; // PlayingCard class class PlayingCard{ int numRank; int numSuit; string rank; string suit; public: PlayingCard(); PlayingCard(int numRank,int numSuit); void setRankString(); void setSuitString(); void setRank(int numRank); void setSuit(int numSuit); string getRank(); string getSuit(); int getRankNum(); int getSuitNum(); string getCard(); };...

  • 4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a)...

    4.a) 4.b> 4.c) C++ Programming Lab Exercise 09 Inheritance. Friend Functions, and Polymorphism (virtual functions) 4.a) Run the following code and observe the output. #include <iostream> #include <string> using namespace std; class Vehicle public: void print() { cout << "Print: I am a vehicle. \n"; } void display() { cout << "Display: I am a vehicle. \n"; } }; class Car: public Vehicle { public: void print() { cout << "Print: I am a car.\n"; } void display() { cout...

  • Assignment Overview In Part 1 of this assignment, you will write a main program and several...

    Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT