War C Program Classes 1The CardDeck class which will create

War (C++ Program)

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

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

***** I have to use arrays to implement the CardDeck class instead of Vectors.

Thank you ahead of time.

Solution

Play WAR!

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 52;


class card
{
public:
//default constructer
card();
card(string cardFace, string cardSuit);
string print();

private:

string face;
string suit;

};

//create a class for deckOfCards
class deckOfCards
{
public:
// assigns the 52 cards to deck
CardDeck();

void shuffle();
//deals out one card from the deck of 52, refrences class card
card deal();

private:
//variable card with a pointer to deck
card deck[SIZE]; // an array of cards of size SIZR
//keep track of what card you are dealing with
int currentCard;
};

int main()
{

//declare deckofcards(from class) called deck
CardDeck deck;
deck.shuffle();
//calculates how many cards you want to print
//now it is two because its decided that each player will get two cards when they start
for( int i = 0; i <= 2; i++)
{
//set current card equal to deck.dealcard and return value will replace it
currentCard = deck.dealCard();
//actually print out current card in the format we want
cout << currentCard.print() << endl;

}


return 0;
}

//file implementation

//default constructor
card::card()
{

}

//constructor with two parameters
card::card(string cardFace, string cardSuit)
{
//assigns the paraments above to the two variables face and suit
face = cardFace;
suit = cardSuit;
}

string card::print()
{
//return the way the card will be displayed
return (face + \" of \" + suit);
}

//assigns the 52 cards to deck
CardDeck::CardDeck()
{
//put all the face values in an array as strings
string faces[] = {\"Ace\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\", \"10\", \"Jack\", \"Queen\", \"King\"};
//put all the suit values in an array as strings
string suits[] = {\"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"};
//initilize deck from the card class to a new array
deck = new card[SIZE];

currentCard = 0;

for(int count = 0; count < SIZE; count++)
{
//deck at postion count will be equal to card, each with a different face and suit
deck[count] = card(faces[count % 13], suits[count / 13]);
}

}


//shuffles the deck once all the cards are assigned
void CardDeck::shuffle()
{

currentCard = 0;
//create a for loop so all 52 cards will be shuffled
for(int first = 0; first < SIZE; first++)
{
int second = (rand() + time(0)) % SIZE;
//create a temp var. and set it equal to the deck at the first postiion
card temp = deck[first];
//swap deck at first and second postion
deck[first] = deck[second];
//swap deck and temp
deck[second] = temp;
}
}


card CardDeck::deal()
{
//if we are out of cards
if(currentCard > SIZE)
{
shuffle();
}
//if its out of cards
if( currentCard < SIZE)
{
return (deck[currentCard++]);
}
return (deck[0]);
}

War (C++ Program) 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 i
War (C++ Program) 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 i
War (C++ Program) 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 i
War (C++ Program) 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 i

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site