Question

This needs to be done in c++11 and be compatible with g++ compiling Project description: Write...

This needs to be done in c++11 and be compatible with g++ compiling

Project description:
Write a C++ program to simulate a simple card game between two players. The game proceeds as follows: The 52
cards in a deck of cards are shuffled and each player draws three cards from the top of the deck. Remaining cards are
placed in a pile face-down between the two players. Players then select a card from the three in their hand. The player
playing the card with the highest point value wins, and collects both cards and adds them to their pile (score). If the
cards played have the same point value, the hand is a draw (tie) and no points are accumulated by either player. At the
completion of a hand, each player draws the top card from the deck to add to their hand (player 1 first then player 2).
Play continues until all cards have been played. The winner is the player with the most points at game’s end.

You will use a standard deck of 52 cards where there are thirteen cards from each of four suits: hearts, spades,
diamonds, and clubs. The thirteen cards in point order are the 2-10 numbered cards, the face cards (Jack, Queen, King),
and the Ace card. Points are distributed as follows: Ace=15, face cards=10, all other cards count as their numeric value.

Requirements:
1. Your program must be split into 7 files. There will be 3 classes (each with separate interface and implementation
files), and a driver file. The requirements for these are specified below:

a) The Card class – This class represents an individual card
-Files must be named card.h and card.cpp
-Class must be named Card
-The interface (header file) is provided.
-You should implement the interface file in a .cpp implementation file
-All data members must be of the type specified in the header file
-All member functions in the interface file must be implemented as declared – However you have
flexibility in how you choose to implement the body of each, consistent with the specifications

b) The Deck class – This class represents the deck of cards
- Files must be named deck.h and deck.cpp
-Class must be named Deck
-The interface (header file) is provided.
-You should implement the interface file in a .cpp implementation file
- All data members must be of the type specified in the header file
-All member functions in the interface file must be implemented as declared – However you have
flexibility in how you choose to implement the body of each, consistent with the specifications

c) The Player class – This class will represent the human and computer player – Play is autonomous
- Files must be named player.h and player.cpp
-Class must be named Player
-The interface (header file) is provided.
-You should implement the interface file in a .cpp implementation file
-All data members must be of the type specified in the header file
-All member functions in the interface file must be implemented as declared – However you have
flexibility in how you choose to implement the body of each, consistent with the specifications

d) A driver, or client, file
Must be named proj4.cpp
Reads an input configuration file named cardgame.txt, consisting of:
1. First line: Player 1’s name (a single word)
2. Second line: Player 2’s name (a single word)
3. Third line: A positive integer to be used as the random number seed
-Must contain the line srand(x); (where x is the seed read from the file), prior to below processing
- Must instantiate the players
-Must instantiate the card deck – The deck has 52 cards (13 per suit), in this suit order: Clubs, Hearts, Spades,
Diamonds, and card order: Ace card has face value 0, 2 card has face value 1, 3 card has face value 2, and so
on to the King card (face value 12). Order is from the bottom of the deck (Ace of Clubs → King of Diamonds).
- The deck array must be shuffled using std::random_shuffle (requires the algorithm header file be included)
– Use std::begin and std::end on the deck array to pass as iterators to this function. Do not use std::shuffle.
-Each player alternates taking a card from the top of the deck, until each has 3 cards
Game play proceeds as follows:
1. Each player selects the highest card by point value. If multiple cards in the player’s hand have the
same point value, then face value will be used to determine the highest card. If at this stage
multiple cards have the same face value, the suit will be used to determine which card is highest, by
using the following precedence (in decreasing order): Clubs, Hearts, Spades, Diamonds.
2. The player playing the card with the highest point value wins, and collects both cards and adds them
to their pile (score). If both cards played have the same point value, the hand is a draw (tie) and no
points are accumulated by either player.
3. Player 1 will replace the card just played by drawing a card from the top of the deck. Player 2 will
then do the same, and play continues.
Output for this program must clearly and neatly show that the program works and that it works correctly.
Your output must match the one provided. The suits, Clubs, Hearts, Spades, and Diamonds are shown as (C,
H, S, D) respectively, immediately following the card face. Point values follow in brackets. Your code must:
1. Display the entire deck of cards after it is instantiated (before shuffling it) – Top of deck displayed first.
2. Display the shuffled deck (prior to players picking their first 3 cards) – Top of deck displayed first.
3. Players alternate drawing the card at the top of the deck - player 1 then player 2 - to (re)fill their hand
4. For each turn of play, two lines are output (see sample output)
Pre-Play: Turn #, player name, card in each card slot of hand, and score (player 1 then player 2)
Post-Play: Turn #, player name, card in each card slot of hand, and score (player 1 then player 2) –
The winning player’s name should be followed by an asterisk (if the turn is a draw, no winner is
identified); the slot from which the card was played should be shown as empty; the scores should
reflect the outcome of the turn of play
5. At the end of the game, output one of the following:
The word “Winner” followed by the name of the player with the highest score, and their score
The word “Tie”, followed by the tie score
A sample output file of a complete game is provided. Your output must match this format exactly, as far as
spacing and content. You are not required to create this output file. It is an example for you to follow.

Here is the headers provided and makefile

CARD.h

#ifndef CARD_H

#define CARD_H


#include <iostream>

using std::ostream;

// Enumerated type that represents the card suits
enum
suit {clubs, hearts, spades, diamonds};


class Card
{
public:
//default constructor - required
  
Card();

//constructor that takes a card's face value (an integer) and its suit
   // card face example: Ace=0, 2=1, 3=2, ... Q=11, K=12
  
Card (int face, suit st);

// overload the << operator to display the card
  
friend ostream& operator << (ostream& os, const Card& cd);

// compare and return true if *this has a lesser point value than cd, false otherwise
  
bool operator < (const Card& cd) const;

// compare and return true if *this has a larger point value than cd, false otherwise
  
bool operator > (const Card& cd) const;

// compare and return true if *this has the same point value as cd, false otherwise
  
bool operator== (const Card& cd) const;

// return the point value of the card: Ace: 15, Faces: 10, Numbers: the number
  
int getPointValue() const;

// return the face value of the card: Ace: 0, 2: 1, 3:2, .... Queen:11, King:12
  
int getFaceValue() const;

// return the card's suit: clubs, hearts, spades, diamonds
   suit getSuit() const;


private:
  
suit  
cardSuit;       // card's suit
  
int cardFace;       // card's face value
  
int pointValue;       // card's point value (from its face)
};

#endif

DECK.h

#ifndef DECK_H

#define DECK_H


#include <iostream>

#include "card.h"


using std::ostream;


class Deck
{
public:

// default constructor
  
Deck();

// Remove the top card from the deck and return it.
  
Card dealCard();

// Shuffle the cards in the deck
  
void Shuffle();

// return true if there are no more cards in the deck, false otherwise
  
bool isEmpty();

//overload << operator to display the deck
  
friend ostream& operator << (ostream&, const Deck&);


private:

static const int numCards = 52;       // # of cards in a deck
   Card
theDeck[numCards];           // the array holding the cards
  
int topCard;                   // the index of the deck's top card
};

#endif

PLAYER.h

#ifndef PLAYER_H

#define PLAYER_H


#include <iostream>

#include <string>

#include "deck.h"

#include "card.h"


using std::ostream;

using std::string;


class Player
{

public:
static const int Max_Cards = 3; // # of cards a player can have in a hand

  
Player(string name="unknown");

// constructor - player's name defaults to "unknown" if not supplied

  
Card playCard();

// Simulates player removing one card from hand and playing it - returns the card
  
           // Play the card with the highest value, per the rules in the specification
   
void drawCard(Deck& dk);

// draw top card from the deck to replace played card in hand
  
  
void addScore(Card acard);

// add the point value of the card to the player's score

   
int getScore() const;

// return the score the player has earned so far



string getName() const;

// return the name of the player
  
bool emptyHand() const;

// return true if the player's hand is out of cards


  
friend std::ostream& operator << (std::ostream&, const Player&); // overload the << operator to display cards in player's hand (or _____ if no card)
private:
  
string name;       // the player's name
  
int score;       // the player's score
  
Card hand[Max_Cards];   // array holding the cards currently in the player's hand
  
bool hasPlayed[Max_Cards];   // hasPlayed[i] indicates that hand[i] (i.e., ith card in player's hand)
has been played

};


#endif


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

//main.cpp
#include <iostream>
#include "Card.h"
#include "Deck.h"
#include "Player.h"

using namespace std;

int main() {

    string playerName;

    cout << "Hello! Please enter your name: ";
    cin >> playerName;
    Player player(playerName);
    Player computer("Computer");

    //Display instructions to player
    cout << "OK " << playerName << "! Let the game begin! You will be given 3 cards, as will the computer.\n";
    cout << "One of these cards will be selected to play. The player who submits the highest cards will have \n";
    cout << "both cards added to their score. Whoever has the highest score wins!\n";
    cout << endl;

    //Display deck of cards
    cout << "The following deck will be used:\n";
    Deck deck;
    cout << endl;

    //Shuffle deck of cards and display the deck from the Shuffle function
    cout << "It will of course be shuffled:\n";
    deck.Shuffle();
    cout << endl;

    //While loop to step through the game. Continues as long as the deck is not empty
    while(!deck.isEmpty()){

        //Player and Computer draws cards into their hand
        player.drawCard(deck);
        computer.drawCard(deck);

        cout << "------------------------------------------------------------------------" << endl;
        cout << "\t\t" << player.getName() << "\t\t\t\t\t\t\t\t\t\t" << computer.getName() << endl;
        cout << "Hand: " << player << "\t\t\t" << computer << endl;
        cout << "Score: " << player.total() << "\t\t\t\t\t\t\t\t\t\t" << computer.total() << endl;


        cout << endl << player.getName() << " chooses " << player.playCard() << endl;
        cout << computer.getName() << " chooses " << computer.playCard() << endl;

        //If block for if Player and Computer cards have identical value
        if (player.playCard() == computer.playCard()){
            player.addScore(player.playCard());
            computer.addScore(computer.playCard());
            cout << "Both cards are equal, " << player.getName() << " gets " << player.playCard().getPointValue() << " points, and " << computer.getName() << " gets " << computer.playCard().getPointValue() << " points" << endl;
        }

        //If block for if Player's card is higher in value than Computer's card
        if(player.playCard() > computer.playCard()){
            player.addScore(player.playCard());
            player.addScore(computer.playCard());
            cout << player.getName() << " gets " << player.playCard().getPointValue() + computer.playCard().getPointValue() << " points!" << endl << endl;
        }
        //If block for if Computer's card is higher in value than Player's card
        else if (computer.playCard() > player.playCard()){
            computer.addScore(player.playCard());
            computer.addScore(computer.playCard());
            cout << computer.getName() << " gets " << player.playCard().getPointValue() + computer.playCard().getPointValue() << " points!" << endl << endl;
        }
    }

    //Display final scores and winner of game
    cout << "------------------------------------------------------------------------" << endl;
    cout << "------------------------------------------------------------------------" << endl;
    cout << endl << "The final score is: " << endl;
    cout << player.getName() << " with " << player.total() << endl;
    cout << computer.getName() << " with " << computer.total();
    cout << endl << endl;

    //if statement to handle winning Player
    if (player.total() > computer.total())
        cout << player.getName() << " wins!" << endl;
    else
        cout << computer.getName() << " wins!" << endl;

    cout << endl << "------------------------------------------------------------------------" << endl;
    cout << "------------------------------------------------------------------------";
    return 0;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
//Card.cpp
#include "Card.h"

using namespace std;

Card::Card() {
}

//Constructor accepting card number and suite type, makes appropriate card
Card::Card(int faceValue, suite type) {
    Card::faceValue = faceValue;
    Card::type = type;

    if(faceValue <= 10)
        Card::pointValue = faceValue;
    if(faceValue > 10)
        Card::pointValue = 10;
    if(faceValue == 15)
        Card::pointValue = 15;

    cout << faceValue;

    if(type == hearts)
        cout << "-hearts";
    else if(type == diamonds)
        cout << "-diamonds";
    else if(type == clubs)
        cout << "-clubs";
    else if(type == spades)
        cout << "-spades";
}

ostream& operator << (ostream& os, const Card& cd){
    os << cd.faceValue;
    if(cd.type == hearts)
        cout << "-hearts";
    else if(cd.type == diamonds)
        cout << "-diamonds";
    else if(cd.type == clubs)
        cout << "-clubs";
    else if(cd.type == spades)
        cout << "-spades";
    return os;
}

bool Card::operator<(const Card &cd) const {
    if(this->faceValue < cd.faceValue)
        return true;
    else
        return false;
}

bool Card::operator>(const Card &cd) const {
    if(this->faceValue > cd.faceValue)
        return true;
    else
        return false;
}

bool Card::operator==(const Card &cd) const {
    if(this->faceValue == cd.faceValue)
        return true;
    else
        return false;
}

int Card::getPointValue() const {
    return pointValue;
}
------------------------------------------------------------------------------------------------------------------------
//Card.h
#ifndef CARD_H
#define CARD_H

#include <iostream>
using std::ostream;

enum suite {clubs, hearts, spades, diamonds};

class Card
{
public:
    Card();

    Card (int faceValue, suite type);

    friend ostream& operator << (ostream& os, const Card& cd);

    bool operator < (const Card& cd) const;

    bool operator > (const Card& cd) const;

    bool operator== (const Card& cd) const;

    int getPointValue() const;

private:
    suite   type;
    int     faceValue;
    int     pointValue;
};
#endif
--------------------------------------------------------------------------------------------------------------------
//Deck.cpp
#include "Deck.h"

using namespace std;

Deck::Deck() {

    topCard = 0;
    for(int i = 1; i < 14; i++){
        for(int j = 0; j < 4; j++){
            int x = i;
            if(i == 1)
                x = 15;
            if(i > 10)
                x = 10;
            theDeck[topCard] = Card(x,(suite)j);
            topCard++;
            cout << " ";
        }
        cout << endl;
    }
}


Card Deck::dealCard() {
    int index = topCard;
    index--;
    topCard--;
    return theDeck[index];
}

void Deck::Shuffle() {
    random_shuffle(&theDeck[0], &theDeck[52]);
    int deckIndex = 0;
    for(int i = 1; i < 14; i++){
        for(int j = 0; j < 4; j++){
            cout << " " << theDeck[deckIndex];
            deckIndex++;
        }
        cout << endl;
    }
}

bool Deck::isEmpty() {
    if(topCard > 0)
        return false;
    else
        return true;
}

ostream& operator << (ostream& os, const Deck& deck){
    os << deck.theDeck;
    return os;
}

-------------------------------------------------------------------------------------------------------------
//Deck.h
#ifndef DK_H
#define DK_H

#include <iostream>
#include "card.h"

using std::ostream;

class Deck
{
public:

    Deck();

    Card dealCard();

    void Shuffle();

    bool isEmpty();

    friend ostream& operator << (ostream&, const Deck&);

private:
    static const int Card_Num = 52;

    Card    theDeck[Card_Num];
    int     topCard;
};

#endif

-----------------------------------------------------------------------------------------------------
//Player.cpp
#include "Player.h"

using namespace std;


Player::Player(string name) {
    Player::name = name;
    Player::score = 0;
    Player::hand[Max_Cards];
}


Card Player::playCard() {

    return Player::hand[0];
}

void Player::drawCard(Deck &dk) {

    int index = 0;
    if (dk.isEmpty())
        cout << "The deck is empty!" << endl;

    while (!dk.isEmpty() && index < 3) {
        if(!played[index])
            hand[index] = dk.dealCard();

        index++;
    }
}

void Player::addScore(Card acard) {
    score += acard.getPointValue();
}

int Player::total() const {
    return score;
}

string Player::getName() const {
    return name;
}

bool Player::emptyHand() const {
    if(played[0] || played[1] || played[2])
        return false;
    else
        return true;
}

std::ostream& operator << (std::ostream& out, const Player& player){
    out << player.hand[0] << " " << player.hand[1] << " " << player.hand[2];
    return out;
}
-------------------------------------------------------------------------------------------------------------------------------
//Player.h
#ifndef PL_H
#define PL_H

#include <iostream>
#include <string>
#include "deck.h"
#include "card.h"

using std::ostream;
using std::string;

class Player
{
public:
    static const int Max_Cards = 3;

    Player(string name="unknown");

    Card playCard();

    void drawCard(Deck& dk);

    void addScore(Card acard);

    int total() const;

    string getName() const;

    bool emptyHand() const;

    friend std::ostream& operator << (std::ostream&, const Player&);

private:
    string name;
    int     score;
    Card    hand[Max_Cards];
    bool    played[Max_Cards];
};

#endif

Add a comment
Know the answer?
Add Answer to:
This needs to be done in c++11 and be compatible with g++ compiling Project description: Write...
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....

  • //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards...

    //main.cpp #include <iostream> #include <iomanip> #include "deck-of-cards.hpp" void RunAllTests() { int count; std::cin >> count; DeckOfCards myDeckOfCards; for (int i = 0; myDeckOfCards.moreCards() && i < count; ++i) { std::cout << std::left << std::setw(19) << myDeckOfCards.dealCard().toString(); if (i % 4 == 3) std::cout << std::endl; } } int main() { RunAllTests(); return 0; } //card.hpp #ifndef CARD_HPP_ #define CARD_HPP_ #include <string> class Card { public: static const int totalFaces = 13; static const int totalSuits = 4; Card(int cardFace, int...

  • 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...

  • I've created a Card class and I'm asked to implement a class called DeckOfCards that stores...

    I've created a Card class and I'm asked to implement a class called DeckOfCards that stores 52 objects of the Card class. It says to include methods to shuffle the deck, deal a card, and report the number of cards left in the deck, and a toString to show the contents of the deck. The shuffle methods should assume a full deck. I also need to create a separate driver class that first outputs the populated deck to prove it...

  • 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...

  • CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing...

    CS102 : JAVA Object-Oriented Programming. 1 Write a class Card whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties an integer for the rank (1 (corresponding to Ace) ,2,3, 13 (correspond ing to King) and suit (Spades, Hearts, Diamonds, or Clubs). Make the suit an enumerated data type. Include getters and setters and a method that tests if a card is valid. Write a class named Deck whose instances are full...

  • NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET...

    NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...

  • 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...

  • Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack...

    Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card {    private final String face; // face of card ("Ace", "Deuce", ...)    private final String suit; // suit of card ("Hearts", "Diamonds", ...)    // two-argument constructor initializes card's face and suit    public...

  • Java Write a complete program that implements the functionality of a deck of cards. In writing...

    Java Write a complete program that implements the functionality of a deck of cards. In writing your program, use the provided DeckDriver and Card classes shown below. Write your own Deck class so that it works in conjunction with the two given classes. Use anonymous objects where appropriate. Deck class details: Use an ArrayList to store Card objects. Deck constructor: The Deck constructor should initialize your ArrayList with the 52 cards found in a standard deck. Each card is a...

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