For this part of the program, create a simplified version of the card game War t
ID: 3766040 • Letter: F
Question
For this part of the program, create a simplified version of the card game War that uses the Card class and another pre-defined class named DeckOfCards.
The concept of our simplified version of War is, well, simple. It's a two-player game that uses a deck of 52 cards (no jokers). Each player will draw a card. If the face values of the cards are equal, both players will be awarded one point because the "war" was a draw. If player 1's card has the larger face value, player 1 will be awarded 2 points because they won the "war". If player 2's card has the larger face value, player 2 will be awarded 2 points because they won the "war". This process will continue until the deck of cards is empty (ie. all of the cards have been drawn from there deck and there are no more cards).
After all of the cards have been drawn, display the score for each player and declare a winner for the game. If the two players have the same score, declare the game a draw.
DeckOfCards class
This class has been pre-written and will be used to implement the game. The code can be found at: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign10.cpp
Add the code for the Card class where indicated in the CPP file.
The DeckOfCards class is used to simulate a deck of 52 playing cards. It contains the following data members:
MAX_CARDS an integer symbolic constant that represents the maximum number of cards in a deck of cards. Its value is 52.
NUM_SUITS an integer symbolic constant that represents the number of different suits for the cards in the deck. Its value is 4.
CARDS_PER_SUIT an integer symbolic constant that represents the number of different face cards for each suit in the deck. Its value is 13.
deck an array of 52 Card objects. It represents the actual deck of cards.
topCard an integer that represents the subscript of the card that is on the top of the deck. It's initial value is -1 to represent that no cards have been removed from the deck.
The constructor and methods for the DeckOfCards class are as follows:
DeckOfCards(): the class has a single constructor that takes no arguments. It initializes all 52 elements of the deck array to the 52 cards in a standard deck of playing cards. It also initializes the topCard data member to -1 to indicate that no cards have been removed from the deck.
Card draw(): this method draws a card from the deck. It takes no arguments and returns a Card object: the card that is drawn from the deck.
void shuffle(): this method shuffles all 52 cards in the deck. It takes no arguments and returns nothing.
bool isEmpty(): this method determines if all of the cards have been drawn from the deck. It takes no arguments and returns a boolean value: true if the deck is empty (no more cards to draw) or false is the deck is not empty (there are still cards that can be drawn)
Implementing the game
Set the seed value that is used by the random number generator by using the srand() function. Pass the value time(NULL) or time(0) to the srand function.
Create a DeckOfCards object that will used for the game.
Create two Card objects. These will be used to hold the cards that are drawn by the two people playing the game.
Create and initialize two integers to hold the game scores for the two players.
Shuffle the deck of cards by calling the shuffle method for the DeckOfCards object that was created earlier.
Write a loop that will execute as long as the deck of cards is not empty. Inside of the loop:
Player 1 should draw a card
Player 2 should draw a card
Display the two cards that were drawn
If the face values of the two cards are equal, awarded one point to both players and declare the "war" a draw. If player 1's card has the larger face value, award player 1 2 points and declare that player 1 won the "war". If player 2's card has the larger face value, award player 2 2 and declare that player 2 won the "war".
Once the deck of cards is empty, display the scores for both players and declare a winner. If the scores are equal, declare the battle a "draw".
Programming Requirements
Each method that is required must have a documentation box like a function.
Hand in a copy of the source code from part 2 of the assignment using Blackboard.
Note
For ease, an Ace is considered the smallest/lowest card in the deck.
Output
A couple runs of the program might resemble the following but keep in mind that the random number generator is being used so your output will probably not match this output completely.
Run 1:
Run 2:
Run 3:
Explanation / Answer
#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
using namespace std;
int array_sequence[] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
string array_suit[] = {"Heart","Clubs","Spades","Diamond"};
class Card{
private:
int Sequence;
string Suit;
public:
Card();
Card(int,string);
int get_sequence();
string get_suit();
void assign_values(int,string);
friend ostream &operator<<(ostream&, const Card&);
};
Card::Card(){
Sequence = 0;
Suit = "void";
}
Card::Card(int sequence, string suit){
Sequence = sequence;
Suit = suit;
}
int Card::get_sequence(){
return Sequence;
}
string Card::get_suit(){
return Suit;
}
void Card::assign_values(int i, string suit){
Sequence = i;
Suit = suit;
}
ostream &operator<<(ostream& os, const Card& card){
if (card.Sequence == 1) {
os << "Ace of " << card.Suit;
}
else if(card.Sequence == 11){
os << "Jack of " << card.Suit;
}
else if(card.Sequence == 12){
os << "Queen of " << card.Suit;
}
else if(card.Sequence == 13){
os << "King of " << card.Suit;
}
else{
os << card.Sequence << " of " << card.Suit;
}
return os;
}
class Deck {
private:
int x;
Card cards[52];
public:
Deck();
void print_card(int);
void print_Deck();
void shuffle();
Card get_card(int);
};
Deck::Deck(){
for (int n = 0; n < 13; n++) {
for (int seme = 0; seme < 4; seme++) {
cards[n*4+seme].assign_values(array_sequence[n], array_suit[seme]);
}
}
}
void Deck::print_card(int i){
cout << cards[i];
}
void Deck::print_Deck(){
for (int i = 0; i<52; i++) {
for (int a = 0; a < i; a++) {
cout << " ";
}
print_card(i);
cout << endl;
}
}
void Deck::shuffle(){
srand ( time(NULL) );
for (int i = 0; i<500; i++) {
int x = rand()%51;
int y = rand()%51;
Card cpy;
cpy.assign_values(cards[x].get_sequence(),cards[x].get_suit());
cards[x] = cards[y];
cards[y] = cpy;
}
}
Card Deck::get_card(int i){
return cards[i];
}
class Player{
private:
vector <Card> cards;
string name;
public:
Player();
Player(string);
string get_name();
void append_card(Card);
void print_player();
Card discard(int);
bool operator==(Player);
bool void_player();
void feed_from_back(Card);
int size_cards();
};
int Player::size_cards(){
return cards.size();
}
void Player::append_card(Card card){
cards.insert(cards.begin(),card);
}
Player::Player(){
name = "void";
}
Player::Player(string x){
name = x;
}
string Player::get_name(){
return name;
}
void Player::print_player(){
int a= 0;
vector <Card> ::iterator i;
for (i = cards.begin(); i != cards.end(); i++) {
a++;
for (int c = 0; c < a; c++) {
cout << " ";
}
cout << *i << endl;
}
}
Card Player::discard(int a){
Card c_return;
vector <Card>::iterator i;
i = cards.begin();
for (int f = 0; f < a; f++) {
i++;
}
c_return = *i;
cards.erase(i);
return c_return;
}
bool Player::operator==(Player other_player){
if (name == other_player.get_name()) {
return 1;
}
else {
return 0;
}
}
bool Player::void_player(){
if (cards.size()== 0) {
return 1;
}
else {
return 0;
}
}
void Player::feed_from_back(Card x){
cards.insert(cards.end(),x);
}
void check(Card card, Player &g1, Player &g2, Player &Deck_c){
cout << endl;
int a = card.get_sequence();
if (card.get_sequence() <= 3) {
cout << g2.get_name() << " discard: " << endl;
while (a > 0) {
if (g2.void_player()) {
return;
}
Card discarded_card = g2.discard(0);
cout << " ";
cout << discarded_card;
Deck_c.append_card(discarded_card);
check( discarded_card, g2,g1, Deck_c);
a--;
}
int size_Deck_c = Deck_c.size_cards();
for (int i = 0; i < size_Deck_c; i++) {
g1.feed_from_back(Deck_c.discard(0));
}
return;
}
return;
}
void turn(Player &g1, Player &g2, Player &Deck_c){
if (g1.void_player()) {
return;
}
Card discarded_card = g1.discard(0);
cout << g1.get_name() << " discard:" << endl << " ";
cout << discarded_card;
Deck_c.append_card(discarded_card);
check(discarded_card, g1, g2, Deck_c);
turn(g2,g1,Deck_c);
}
int main (int argc, char * const argv[]) {
Deck Deck1;
Deck1.shuffle();
Player player1("AAAA");
Player player2("BBBB");
Player central_deck;
for (int i = 0; i < 52/2; i++) {
player1.append_card(Deck1.get_card(i));
}
for (int i = 52/2; i < 52; i++) {
player2.append_card(Deck1.get_card(i));
}
turn(player1, player2, central_deck);
cout << "mazzo" << endl;
central_deck.print_player();
cout << "mano 1" << endl;
player1.print_player();
cout << "mano 2" << endl;
Pplayer2.print_player();
std::cout << "Hello, World! ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.