War C Program Classes are required not vectors Classes 1The
War (C++ Program)
*** Classes are required, not vectors.
Classes:
1)The CardDeck class which will create the deck of cards
2)The Card class which creates cards
The main logic of the program will be in the main class. You will use the Card class and the CardDeck class to play the game.
Here the methods you will need to build. Feel free to add more if you need them.
public class CardDeck
public CardDeck( // constructor which creates a deck of 52 cards
public void displayCardAt(int) // display the card at particular location (array index)
public int deal( ) // deal a card – return the point value of the card
public int cardsLeft( ) // how many cards are left in the deck
public void shuffle( ) // shuffle the cards in the deck
public class Card
public Card (char s, int r ) // constructor to create a card, setting the suit and rank
public int getValue( ) // return the point value of the card. Ace = 1, 2 thru 10, face cards = 10
public void displayCard( ) // display the card
*** Arrays are to be used to implement classes. Not vectors
You will need to provide a menu with the following choices:
1)Get a new card deck
2)Show all cards in the deck
3)Shuffle
4)Play WAR!
5)Exit
**** Please make sure it compiles. Thank you ahead of time
Solution
#include<iostream>
#include<stdlib.h>
using namespace std;
//Base class to create card
class Card
{
public:
char sym;
int val;
Card() {}
Card (char s, int r );
int getValue();
void displayCard( );
};
//Derived class
class CardDeck : public Card
{
public:
int counter;
Card **cd;
CardDeck();
void displayCardAt(int);
int deal( );
int cardsLeft( );
void shuffle( );
};
//Constructor of CardDeck to create array of Cards
CardDeck::CardDeck()
{
counter = 0;
cd = new Card*[52];
for(int c = 0; c < 52; c++)
{
*(cd + c) = new Card(c, c);
}
}
//Returns the card value
int CardDeck::deal()
{
return getValue();
}
//Returns the number of cards left and displays the number of cards lect
int CardDeck::cardsLeft()
{
for(int c = 0; c < 52 - counter; c++)
{
cd[c]->displayCard();
}
return counter;
}
//Shuffles the cards
void CardDeck::shuffle()
{
Card **t;
for(int c = 0; c < 52; c+=2)
{
**t = **(cd + c);
**(cd + c) = **(cd + (c + 1));
**(cd + (c + 1)) = **t;
}
}
//Displays the card at specified position
void CardDeck::displayCardAt(int x)
{
cd[x]->sym;
}
//Default position to create a card
Card::Card(char s, int t)
{
sym = s; val = t;
}
int Card::getValue()
{
return val;
}
//Displays the card symbol and value
void Card::displayCard()
{
cout<<\"\ Symbol = \"<<sym;
cout<<\"\ Value = \"<<val;
}
int main()
{
CardDeck ccd;
int ch, no;
//Menu operation
do
{
cout<<\"\ 1)Get a new card deck \ 2)Show all cards in the deck \ 3)Shuffle \ 4)Play WAR! \ 5)Exit\";
cout<<\"\ Enter your choice: \";
cin>>ch;
switch(ch)
{
case 1:
cout<<\"\ Enter the card number: \";
cin>>no;
ccd.displayCardAt(no);
ccd.deal();
break;
case 2:
ccd.cardsLeft();
break;
case 3:
ccd.shuffle();
break;
case 4:
cout<<\"\ Enter the card number: \";
cin>>no;
ccd.displayCardAt(no);
ccd.deal();
break;
case 5:
exit(0);
}
}while(1);
}


