Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Solution should not be too complicated and make sure there is code for main.cpp, card.h, card.cpp, deck.h, deck.cpp In this homework, you will make a

(Solution should not be too complicated and make sure there is code for main.cpp, card.h, card.cpp, deck.h, deck.cpp

In this homework, you will make a simualtion of the card game War. You can read more about war at here.

War is a card game played by 2 people. This card game is entirely random. There is no point where the player's decisions change the outcome of the game. This means that we can have a computer program play the game against itself and the outcome will be the same as with two people.

A Single Card

The game is played with standard poker cards. Make card.h and card.cpp files. A card has a Suit and a Rank. There are four suits: Clubs, Spades, Diamonds, and Hearts. There are thirteen ranks in order from smallest to largest 2-10, Jack, Queen, King, and Ace.

You must use the below enum types for the Ranks and Suits.

 enum Rank {TWO,THREE,FOUR,FIVE,SIX,SEVEN,EIGHT,NINE,TEN,JACK,QUEEN,KING,ACE}; enum Suit {CLUBS,SPADES,DIAMONDS,HEARTS}; 

Create a card class that has a rank and suit. Your class MUST meet the following requirements. You may add additional functionality to complete the assignment, but there will be a penalty if any of these functions are not defined.

Default Constructor

Accessor Methods for the Rank and Suit

Overloaded output operator <<

Overloaded comparison operators ==,<.>

A Deck of Cards

The game is played with a deck of cards. At the start, there is one deck. This deck is shuffled. Then the deck is cut in half and each player gets their own deck with half the cards.

Create a deck class in deck.h and deck.cpp. The deck class must meet the following requirements (although you will likely decide on additional methods/attributes):

Deck can be shuffled.

Deal a single card from the TOP of the deck. (return a Card)

Deal a stack of int n cards from the TOP of the deck. (return a Deck)

Find the number of cards in the deck.

Add a new card to the BOTTOM of the deck.

How to Shuffle a Vector

An easy shuffle to implement is the Fisher-Yates Shuffle. This will work to shuffle your deck.

You can get random numbers by using the rand command. Learn more about it here.

The Game of War!

The game will be played in main.cpp. Here is an overview of how the game should be run. An example output is attached in war_game.txt.

Create a new Deck with 52 cards.

Shuffle the deck and deal each player half the cards.

While both player still have Cards do the following:

Each player takes the top card from their deck.

The player with the larger card (by rank) takes both cards and puts them at the bottom of their deck.

If both cards have equal ranks, then War breaks out.

Both players draw two new cards from the top of the deck.

The first card is placed face down, the second face up.

If either play does not have enough cards, they immediately lose.

The player that has the highest face up card wins.

If the face up cards have the same value, then the War continues....

When a winner is decided, they get all the cards that have been draw since the original tie.

The player that ran out of cards is the loser, announce which player was victorious.

Your main.cpp will print out every card battle that takes place. It will also print out a line to tell the user when a war has broken out. Finally, your program will print out the winner of the game (Player 1 or Player 2). Refer to the sample output file at the top of this assignment.

WARNING: Depending on how you put the cards back on the bottom of the deck you can create an infinite loop. One way to help avoid this is to always put the larger of the two cards on the bottom first when you have a "non-war" case. In other words, say you have cards c1 and c2. If c1>c2, then put c1 first on the bottom of your deck, then c2. Otherwise first put c2 on the bottom followed by c1.

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

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions