Question

Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...

Program 4: C++

The Game of War

The game of war is a card game played by children and budding computer scientists.

From Wikipedia:

The objective of the game is to win all cards [Source: Wikipedia].

There are different interpretations on how to play The Game of War, so we will specify our SMU rules

below:

1) 52 cards are shuffled and split evenly amongst two players (26 each)

a.

The 26 cards are placed into a “to play” pile

2) Each player draws a card from the top of their “to play” pile and placed in a “currently in

play” pile between the two players.

a.

The player who drew the higher card (cards are ranked from 2 to ace-high) gets to

keep both cards and the cards will be placed into their “played” pile.

i. In the event of a tie, players continue drawing cards until there is no longer

a tie. Once the tie is broken, the winning player gets to keep all the cards

from the “currently in play” pile.

b. When a player runs out of cards from their “to play” pile, they will take all the cards

from their “played” pile, shuffle them, and then put those cards into their “to play”

pile.

c.

When a player runs out of cards from both their “played” pile and their “to play”

pile, then they lose the game.

i. Note that in the unlikely event that there are 26 draws in a row, then both

players lose the game.

Implementation Details

You may not access any member variables directly.

o

You must use “get” and “set” functions

You may

ONLY

use old-school c-style arrays. You may not use the <array> class, <vector>,

<stack>, the <algorithm> library, or any other built-in c++ data structures.

All card objects must be dynamically allocated on the heap.

o

It is ok for arrays of pointers to cards to exist on the stack.

o

All dynamically allocated memory must be appropriately deleted/destroyed (in a

destructor perhaps).

Consider using enum for suit and rank

You must implement your own shuffle algorithm

o

You won’t be graded on algorithm performance (big-O). Instead, it should just be a

‘meaningful’ shuffle.

You should use at least three classes.

o

In the reference implementation, classes were used for “GameBoard”, “Card”, “Pile”,

and “Player”, but you can implement your own classes as you see fit.

o

Your class definitions should each be in their own .h file and the implementations should

be in their own .cpp file.

Hints

The following Pile class was created in the reference implementation. Feel free to take advantage of it.

You may notice that it implements a version of a

stack

class Pile{

public:

Pile(){

numCards = 0;

}

void AddCardToPile(Card * card){

// adds a card to the end of the array

cards[numCards] = card;

numCards++;

}

Card * RemoveTopCard(){

// removes a card from the end of the array

Card * cardToReturn = cards[numCards - 1];

numCards--;

return cardToReturn;

}

int GetNumCards(){

return numCards;

}

void Shuffle(){

// TODO

}

void PrintCards(){

// TODO

}

private:

Card * cards[MAXCARDS]; // an array of card pointers

int numCards;

};

Submission Details

You will split up your main function, class header files, and class cpp files into separate files.You will also

need to submit your executable.

To canvas, you must turn in a file called

<LastName_FirstInitial>Program4.zip

containing the files

described above.

Sample Output

Welcome to the Game of War!

Shuffling initial deck...

Initial 'to play' piles:

Player A:

There are 26 cards in the pile.

Card 1, five of clubs

Card 2, jack of spades

Card 3, ten of diamonds

Card 4, queen of clubs

Card 5, seven of spades

Card 6, six of diamonds

Card 7, five of diamonds

Card 8, ten of clubs

Card 9, ten of hearts

Card 10, three of hearts

Card 11, nine of hearts

Card 12, queen of diamonds

Card 13, three of spades

Card 14, king of diamonds

Card 15, seven of clubs

Card 16, eight of spades

Card 17, jack of diamonds

Card 18, jack of clubs

Card 19, three of clubs

Card 20, four of diamonds

Card 21, seven of hearts

Card 22, nine of clubs

Card 23, jack of hearts

Card 24, four of hearts

Card 25, king of spades

Card 26, five of spades

Player B:

There are 26 cards in the pile.

Card 1, king of clubs

Card 2, six of spades

Card 3, king of hearts

Card 4, four of spades

Card 5, two of clubs

Card 6, two of diamonds

Card 7, eight of clubs

Card 8, nine of spades

Card 9, ten of spades

Card 10, six of clubs

Card 11, ace of hearts

Card 12, four of clubs

Card 13, six of hearts

Card 14, ace of spades

Card 15, queen of spades

Card 16, seven of diamonds

Card 17, ace of diamonds

Card 18, nine of diamonds

Card 19, five of hearts

Card 20, ace of clubs

Card 21, two of spades

Card 22, three of diamonds

Card 23, eight of diamonds

Card 24, queen of hearts

Card 25, two of hearts

Card 26, eight of hearts

======== Round 1 ========

Player A plays five of spades

Player B plays eight of hearts

Player B wins the 'currently in play' pile.

Player A has 25 in their 'to play' pile and 0 in their 'played' pile.

Player B has 25 in their 'to play' pile and 2 in their 'played' pile.

======== Round 2 ========

Player A plays king of spades

Player B plays two of hearts

Player A wins the 'currently in play' pile.

Player A has 24 in their 'to play' pile and 2 in their 'played' pile.

Player B has 24 in their 'to play' pile and 2 in their 'played' pile.

======== Round 3 ========

Player A plays four of hearts

Player B plays queen of hearts

Player B wins the 'currently in play' pile.

Player A has 23 in their 'to play' pile and 2 in their 'played' pile.

Player B has 23 in their 'to play' pile and 4 in their 'played' pile.

======== Round 4 ========

Player A plays jack of hearts

Player B plays eight of diamonds

Player A wins the 'currently in play' pile.

Player A has 22 in their 'to play' pile and 4 in their 'played' pile.

Player B has 22 in their 'to play' pile and 4 in their 'played' pile.

======== Round 5 ========

Player A plays nine of clubs

Player B plays three of diamonds

Player A wins the 'currently in play' pile.

Player A has 21 in their 'to play' pile and 6 in their 'played' pile.

Player B has 21 in their 'to play' pile and 4 in their 'played' pile.

======== Round 6 ========

Player A plays seven of hearts

Player B plays two of spades

Player A wins the 'currently in play' pile.

Player A has 20 in their 'to play' pile and 8 in their 'played' pile.

Player B has 20 in their 'to play' pile and 4 in their 'played' pile.

======== Round 7 ========

Player A plays four of diamonds

Player B plays ace of clubs

Player B wins the 'currently in play' pile.

Player A has 19 in their 'to play' pile and 8 in their 'played' pile.

Player B has 19 in their 'to play' pile and 6 in their 'played' pile.

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

# include <iostream.h>

# include <algo.h>

enum suits {diamond, club, heart, spade};

class Card {

public:

               // constructors

        Card    ( );            // initialize a card with default values

        Card    (suits, int);   // initialize a card with given values

               // data fields

        int    rank;           // hold rank of card

        suits   suit;           // hold suit of card

};

Card::Card     ( )

        // initialize a new Card

        // default value is the ace of spades

{

        rank = 1;

        suit = spade;

}

Card::Card     (suits sv, int rv)

        // initialize a new Card using the argument values

{

        rank = rv;

        suit = sv;

}

ostream & operator << (ostream & out, Card & aCard)

        // output a textual representation of a Card

{

               // first output rank

        switch (aCard.rank) {

               case 1: out << "Ace";   break;

               case 11: out << "Jack"; break;

               case 12: out << "Queen"; break;

               case 13: out << "King"; break;

               default:       // output number

                       out << aCard.rank; break;

               }

               // then output suit

        switch (aCard.suit) {

               case diamond: out << " of Diamonds"; break;

               case spade:   out << " of Spades";   break;

               case heart:   out << " of Hearts";   break;

               case club:    out << " of Clubs";    break;

               }

        return out;

}

class randomInteger {

        public:

               unsigned int operator () (unsigned int);

} randomizer;

unsigned int randomInteger::operator () (unsigned int max)

{

               // rand return random integer

               // convert to unsigned to make positive

               // take remainder to put in range

        unsigned int rval = rand();

        return rval % max;

}

class Pile {

public:

               // constructor

        Pile ( );

               // operations

        void    shuffle ( )

               { random_shuffle (cards, cards+52, randomizer); }

        bool    isEmpty ( )

               { return topCard <= 0; }

        Card    draw ( );

protected:

        Card    cards[52];

        int     topCard;

};

Pile::Pile ( )

        // initialize a Pile by creating all 52 cards

{

        topCard = 0;

        for (int i = 1; i <= 13; i++) {

               Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);

               cards[topCard++] = c1;

               cards[topCard++] = c2;

               cards[topCard++] = c3;

               cards[topCard++] = c4;

               }

}

Card Pile::draw ( )

        // return one card from the end of the Pile

{

        if (! isEmpty())

               return cards[--topCard];

        else { // otherwise return ace of spades

               Card spadeAce(spade, 1);

               return spadeAce;

               }

}

class Player {

public:

               // constructor

        Player (Pile &);

               // operations

        Card    draw ( );

        void    addPoints (int);

        int     score ();

        void    replaceCard (Pile &);

protected:

        Card    myCards[3];

        int     myScore;

        int     removedCard;

};

Player::Player (Pile & aPile)

        // initialize the data fields for a player

{

        myScore = 0;

        for (int i = 0; i < 3; i++)

               myCards[i] = aPile.draw();

        removedCard = 0;

}

Card Player::draw ( )

        // return a random card from our hand

{

        removedCard = randomizer(3);

        return myCards[removedCard];

}

void    Player::addPoints (int howMany)

        // add the given number of points to the current score

{

        myScore += howMany;

}

int     Player::score ( )

        // return the current score

{

        return myScore;

}

void    Player::replaceCard (Pile & aPile)

        // replace last card played with new card

{

        myCards[removedCard] = aPile.draw();

}

void main() {

        Pile thePile; // create and shuffle the Pile

        thePile.shuffle();

        Player player1(thePile); // create the two

        Player player2(thePile); // players

               // play until Pile is empty

        while (! thePile.isEmpty() ) {

               Card card1 = player1.draw();

               cout << "Player 1 plays " << card1 << endl;

               Card card2 = player2.draw();

               cout << "Player 2 plays " << card2 << endl;

               if (card1.rank == card2.rank) { // tie

                       player1.addPoints(1);

                       player2.addPoints(1);

                       cout << "Players tie\n";

                       }

               else if (card1.rank > card2.rank) {

                       player1.addPoints(2);

                       cout << "Player 1 wins round\n";

                       }

               else {

                       player2.addPoints(2);

                       cout << "Player 2 wins round\n";

                       }

                       // now replace the cards drawn

               player1.replaceCard(thePile);

               player2.replaceCard(thePile);

        }

        cout << "Player 1 score " << player1.score() << endl;

        cout << "Player 2 score " << player2.score() << endl;

}

Add a comment
Know the answer?
Add Answer to:
Program 4: C++ The Game of War The game of war is a card game played by children and budding comp...
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
  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • War—A Card game Playing cards are used in many computer games, including versions of such classic...

    War—A Card game Playing cards are used in many computer games, including versions of such classics as solitaire, hearts, and poker. War:    Deal two Cards—one for the computer and one for the player—and determine the higher card, then display a message indicating whether the cards are equal, the computer won, or the player won. (Playing cards are considered equal when they have the same value, no matter what their suit is.) For this game, assume the Ace (value 1) is...

  • C++ programming: Card game Can same one help me out with a example finsih and working...

    C++ programming: Card game Can same one help me out with a example finsih and working programm ? Program the basis for a card game (as a class card game): ● A card is represented by its suit/symbol (clubs, spades, hearts, diamonds) and a value (seven, eight, nine, ten, jack, queen, king, ace). ● A deck of cards consists of a pile of 32 cards that are completely connected to four players are distributed. ● Implement the following menu: ===...

  • A standard 52-card deck has four 13-card suits: diamonds, hearts, 13-card suit contains cards numbered f...

    A standard 52-card deck has four 13-card suits: diamonds, hearts, 13-card suit contains cards numbered f probability of drawing a black king of hearts clubs, and spades. The diamonds and hearts are red, and the clubs and spades are black Each from 2 to 10, a jack, a queen, a king, and an ace. An experiment consists of drawing 1 card from the standard deck. Find the The probability of choosing a black king of hearts is ype an integer...

  • I need to build the card game of War in C++. It will be a 2...

    I need to build the card game of War in C++. It will be a 2 player game. Each player will have their own deck of 52 cards. 2-10, Jack=11, Queen=12, King=13, Ace=14. Each player will draw one card from their deck. The player with the higher card wins both cards. If the card drawn is the same, then each player will draw 3 cards and on the 4th card drawn will show it. The player that shows the higher...

  • Using Python 3.5.2 and IDLE Write the code for a modified game of the card game...

    Using Python 3.5.2 and IDLE Write the code for a modified game of the card game crazy eights (much like a game of UNO, where the card played is now on top of the deck and its number or face value is now what is playable for the next player). Write the code to be a 2 player game with any 8 card being able to input a new face value to play on the discard pile. Rules: Use a...

  • I am having problem understanding this problem. please explain it explicitly. its a discrete comp...

    I am having problem understanding this problem. please explain it explicitly. its a discrete computer science problem. thanks Exercises 27-32 concern a 5-card hand from a standard 52-card deck. A standard deck has 13 cards from each of 4 suits (clubs, diamonds, hearts, spades). The 13 cards have face value 2 through10, jack, queen, king, or ace Each face value is a "kind" of card. The jack, queen, and king are "face cards. 27. How many hands contain 4 queens?...

  • how many diamonds are in a deck of cards?

    A deck of cards contains 52 cards. They are divided into four suits: spades, diamonds, clubs and hearts. Each suit has 13 cards: ace through 10, and three picture cards: Jack, Queen, and King. Two suits are red in color: hearts and diamonds. Two suits are black in color: clubs and spades.Use this information to compute the probabilities asked for below and leave them in fraction form. All events are in the context that three cards are dealt from a...

  • Consider a standard 52-card deck of cards with 13 card values (Ace, King, Queen, Jack, and...

    Consider a standard 52-card deck of cards with 13 card values (Ace, King, Queen, Jack, and 2-10) in each of the four suits (clubs, diamonds, hearts, spades). If a card is drawn at random, what is the probability that it is a spade or a two? Note that "or" in this question refers to inclusive, not exclusive, or.

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