Write a script/file/function/header file in C++ to fulfill requirements. In this
ID: 3730125 • Letter: W
Question
Write a script/file/function/header file in C++ to fulfill requirements.
In this homework, we will combine everything we've learned so far with a focus on vector, struct and string.
Overview
For this assignment, your job is to create a simple War game simulation.
The Specifics
You must first create a struct named Card to represent a playing card. It must have two attributes, a rankstored as an int and a suit stored as a string. Since this struct definition will be used in multiple files, you probably would want to create this in its own header file and then include this header in any other file that requires the use of the Card struct.
Before the gameplay portion of your program starts, create a vector of Cards called deck. This vector will hold the current cards to be drawn from. Populate the vector with all 52 card values (Ace of Hearts through King of Spades).
Create a function called printCard() that takes a Card parameter as input and has no output. The function must print "Ace", "Jack", "Queen", or "King" in place of 1, 11, 12, and 13 respectively. The function must print 2-10 for all other respective values.
At this point, print all cards in your vector to ensure you have all 52 cards represented. Once you are satisfied all 52 cards are there, comment out these print statements.
Now, for the most important part, create a function called shuffleDeck that takes a vector of Cards as input. This function randomly selects two cards from the deck and swaps them. This operation must be performed enough times to ensure the entire deck is thoroughly shuffled. To verify the cards have been shuffled, again print all cards in your array. If you are happy with the randomness of the card order, comment out these print statements as well.
Next, create a function called dealNextCard that takes the vector of cards passed by reference as input. The function must return a Card as output. The function must return the card at the back of the deck (i.e., the "top" card in the deck). The function then also needs to remove the last Card from the deck; that is, since we've now "dealt" this card, we don't want to deal it again! Hmm, why do we pass the deck parameter by reference to this function then?
To test our dealing function, create a loop that deals each card and then prints the size of the remaining deck. When you reach the end of the deck and all cards have successfully been dealt once, you can comment out these print statements and continue on.
Once the cards are shuffled and you can deal cards, create two new vectors called player and computer. Deal the deck into the player and computer vectors so that each holds 26 cards.
At this point, everything is in place to start our game. Follow this pseudocode to complete the remainder of the assignment:While the Player wants to play:
Deal the top card from the player deck, print the card
Deal the top card from the computer deck, print the card
If the player has the higher ranked card
Put both cards onto the bottom of the player's deck
If the computer has the higher ranked card
Put both cards onto the bottom of the computer's deck
If both cards have equal rank:Deal three cards from both the player and the computer (these cards do not need to be printed to the terminal)
If the player or computer does not have three cards to deal, then deal all but one card.
Deal the next card and compare
Return to step #4 and compare these two cards
If the player has zero cards or the computer has zero cards, then jump to step 17.
Ask Player if they want to play again, "Yes" or "No". The user must type in "Yes" or "No" and you must store their response as a string.
If they entered "Yes", go back to Step #2.
If they entered "No":
If the player has more cards, print "Player wins!"
If the computer has more cards, print "Computer wins!"
If the player and computer have the same number of cards, print "Draw"
One issue you may have pondered ... what happens if you play many rounds and the deck runs out of cards? Well, when the player or computer wins a hand we need to insert the cards into the vector. Think about which end of the vector we are dealing from. Where should we then insert cards to correspond to the bottom of the deck?
Also note that you now have the starting point to create any card based game (blackjack, poker, go fish, etc.). Hmmm ... could be useful for your final project?
Functional Requirements
Do not use global variables! If you are using a global variable in multiple functions, then you should be passing that variable into the function. If you use global variables, you will lose points so get out of the global variable shorcut habit.
You must declare a Card struct and use Card variables throughout your program and functions.
The user must be inputting strings and you must be comparing string values.
You must use vectors (not arrays) for this assignment.
All functions must be placed in a separate file.
Despite the psuedocode stating "goto step X", your code must not include a literal goto command. You will lose points for using goto. You must structure your loops and conditionals properly to receive full credit.
Hints
You will need to use loops (perhaps while and/or do-while), branching, and functions to complete this assignment.
Write out pseudocode before starting.
Dealing a card is nothing more than returning the back of our deck vector and removing it from the vector.
While it is possible to copy and paste a few lines of code (over and over) to generate all 52 cards, it will become quite tedious and you won't enjoy making sure all 52 cards are represented. You will find it much easier to use two nested for loops to accomplish this task.
Write out pseudocode before starting.
Please follow all directions and requirements and answer all parts.
War! What is it good for, absolutely nothin Computer shows 5 of Spades Player shows 8 of Diamonds Player wins! Do you want to "Play Again" or "Quit"? Play Again Computer shows Jack of Hearts Players shows Queen of Hearts Player wins! Do you want to "Play Again" or "Quit"? Play Again Computer shows Ace of Diamonds Player shows Ace of Hearts Computer deals 1, 2, 3... Player deals 1, 2, 3... Computer shows 7 of Clubs Player shows 3 of Hearts Computer wins! Do you want to "Play Again" or "Quit"? Quit Computer has 29 cards Player has 23 cards Computer wins!!Explanation / Answer
main.h:
#pragma once
#include <iostream>
#include <vector>
#include <random>
#include <ctime>
#include <exception>
#include "Card.h"
void FPopulateCards(std::vector<Card> &);
void FPrintDeck(std::vector<Card>);
void FShuffleDeck(std::vector<Card> &, int);
void FSwapCards(std::vector<Card> &, size_t, size_t);
Card FDealNextCard(std::vector<Card> &);
class EOD : public std::runtime_error
{
public:
EOD() throw() : runtime_error("End of deck!") {}
virtual char const* what() const throw() { return std::exception::what(); }
};
main.cpp:
#include "stdafx.h"
#include "main.h"
int main()
{
Card card{};
std::vector<Card> deck;
FPopulateCards(deck);
FShuffleDeck(deck, 300);
card = FDealNextCard(deck);
return 0;
}
void FPopulateCards(std::vector<Card> &deck)
{
std::string suits[4] = { "club", "diamond", "heart", "spade" };
int ranks[13] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
for (std::string suit : suits)
{
for (int rank : ranks)
{
Card c0 = { rank ,suit };
deck.push_back(c0);
}
}
return;
}
void FPrintDeck(std::vector<Card> deck)
{
std::cout << " All cards: " << std::endl;
for (size_t i{ 0 }; i < deck.size(); ++i)
{
std::cout << FCardString(deck.at(i));
std::cout << std::endl;
}
std::cout << std::endl;
return;
}
void FShuffleDeck(std::vector<Card> &deck, int times)
{
size_t NumberOfCards{ deck.size() };
srand(time(NULL)); // seed
for (size_t i{ 0 }; i < times; ++i)
{
size_t randNum = rand() % NumberOfCards;
FSwapCards(deck, 0, randNum);
}
return;
}
void FSwapCards(std::vector<Card> &deck, size_t index1, size_t index2)
{
Card temp;
temp = deck.at(index1); // now temp has 1
deck.at(index1) = deck.at(index2);
deck.at(index2) = temp;
return;
}
Card FDealNextCard(std::vector<Card> &deck)
{
if (deck.size() == 0) { throw EOD(); }
Card top = deck.back();
deck.pop_back();
return top;
}
Card.h:
#pragma once
#include <sstream>
#include <string>
struct Card
{
int rank;
std::string suit;
};
std::string FCardString(Card);
Card.cpp:
#include "stdafx.h"
#include "Card.h"
std::string FCardString(Card c0)
{
std::stringstream out{};
out << "rank = " << c0.rank << ", suit = " << c0.suit;
return out.str();
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.