Question
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
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 games 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 1s name (a single word) 2. Second line: Player 2s 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 players 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 players 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
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
#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
#include
#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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started