QuestionQuestion

Transcribed TextTranscribed Text

Program 2 Submit Assignment Crazy Eights For this assignment, you will write a program that allows one player to play a game of Crazy Eights against the computer. Crazy Eights is a game that uses a standard deck of 52 cards. Specifically, each card has a rank and a suit. There are 13 ranks: the numbers 2 through 10, Jack (usually represented with the letter J), Queen (Q), King (K), and Ace (A). There are 4 suits: clubs, diamonds, hearts, and spades. In a 52-card deck, there is one card of each rank for each suit. A game of Crazy Eights between two players proceeds as follows: The deck of cards is shuffled, randomizing the order of the cards. Each player is dealt 7 cards. The remaining cards are placed face-down (i.e. with their rank and suit hidden) in a stack on the table and becomes the "stock", with the top card turned over and displayed in a separate "pile". One of the players (player A) begins the game by playing a card from their hand that is the same suit or same rank as the top card on the pile, which becomes the new top of the pile. Player B then plays a card from their hand that is the same suit or rank as the top card, which becomes the new top of the pile. If a player does not have any cards of the required rank or suit, they must draw from the top of the deck (stock) and add cards to their hand until they draw a card that can be played on the pile, or until the deck runs out of cards. The game continues with the players alternating turns until one of the players gets rid of all the cards in their hand, or until all of the cards are drawn and no one can play a card. If neither player has zero cards at the end of the game, the player with the least amount of cards in their hand wins. Eights of any suit are considered "wild" and may be played on any turn. When an eight is played, the player must specify a suit for the next player to match. Needed classes To write your Crazy Eights game, you should implement the following classes, including the specified members and methods. You may also add more members and methods, as needed. class Card { private: 2/6 int rank; // Should be in the range 0-12. int suit; // Should be in the range 0-3. public: // must have constructors, destructor, accessor methods, and mutator methods }; In the Card class above, rank and suit are represented with int values, but you must also have some way to map those values to representations players will be familiar with (e.g. a string representation of the suit or rank). class Deck { private: Card cards[52]; int n_cards; // Number of cards remaining in the deck. public: // must have constructors, destructor, accessor methods, and mutator methods }; The Deck class is the source of all of the cards. Cards will initially start in a Deck object and then be transferred to players' hands. An important method that should be implemented for the Deck class is one to remove a card and return it so it can be placed in a player's hand (deal a card from the deck to the player). class Hand { private: Card* cards; int n_cards; // Number of cards in the hand. public: // must have constructors, destructor, accessor methods, and mutator methods }; The Hand class will hold the cards in one player's hand. The number of cards a player holds may change, so the size of the array of Card objects in a hand may also need to change. Cards may be added to a player's hand and removed from a player's hand, so the Hand class will need functions to do both of those things. Other useful methods might check for a given suit or rank. class Player { private: Hand hand; string name; public: // must have constructors, destructor, accessor methods, and mutator methods }; Program 2 3/6 The Player class represents a single player. Each Player will have a Hand object representing its hand and an array keeping track of the books the player has laid down. Depending on how you implement things, the Player class may need methods to add and remove cards from their hand or to check their hand for cards with a specific suit or rank. Another useful method the Player class might have is one to figure out what suit they want to ask for if they play an eight. Note that the Player class must represent both the human player and the computer player. You should write your class methods accordingly and add any extra data members needed to accomplish this. class Game { private: Deck cards; Player players[2]; public: // must have constructors, destructor, accessor methods, and mutator methods } The Game class represents the state of an entire game. It contains objects representing the deck of cards and both players. It would be useful to have methods in the Game class that check whether the game is over and that execute a player's turn. Program features Your program should do the following: Set up a deck of 52 cards as described above. Shuffle the deck (i.e. randomize the order of cards). Deal the cards to the two players. Play a game of Crazy Eights as described above, with the following gameplay features: Print the current state of the game (e.g. the cards held by the human player and the card of on the top of the pile) after each turn (keeping the computer player's cards hidden). On the human player's turn, prompt the user for a card to play. When they enter a card, either put the card on top of the pile or have them draw a card from the deck if they can't play. If the card is an 8, prompt the user to declare a suit for the next player. Once the game is over, you should announce the winner and ask the user if they want to play again. You should not have any memory leaks in your program. Note: You cannot use the vector class for handling dynamic memory Programming Style/Comments In your implementation, make sure that you include a program header. Also ensure that you use proper indentation/spacing and include comments! Below is an example header to include. Make sure you review the style guidelines for this class, and begin trying to follow them, i.e. don’t align everything on the left or put everything on one line! Program 2 4/6 Crazy 8s /****************************************************** ** Program: crazyeights.cpp ** Author: Your Name ** Date: ** Description: ** Input: ** Output: ******************************************************/ When you compile your code, it is acceptable to use C++11 functionality in your program. In order to support this, change your Makefile to include the proper flag. For example, consider the following approach (note the inclusion of -std=c++11): g++ -std=c++11 <other flags and parameters> In order to submit your homework assignment, you must create a tarred archive that contains your .h, .cpp, and Makefile files. This tar file will be submitted to Canvas. In order to create the tar file, use the following command: tar –cvf assign2.tar <list of all the .h and .cpp files> Makefile Crazy 8s

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/*
* File:   Hand.cpp
* Author:
*
*
*/

#include <iostream>
using namespace std;
#include "Hand.h"

/**
* constructor
*/
Hand::Hand() {
    cards = new Card[52];
    n_cards = 0;
}

/**
* copy constructor
* @param orig
*/
Hand::Hand(const Hand& orig) {
    n_cards = orig.n_cards;
    cards = new Card[52];
    for (int i = 0; i < n_cards; i++) {
       cards[i] = orig.cards[i];
    }
}

/**
* destructor
*/
Hand::~Hand() {
    delete [] cards;
}

/**
* add card to hand
* @param card a card
* @return true if the card could be added
*/
bool Hand::addCard(Card card) {
    if (n_cards >= 52) {
       return false;
    }
    cards[n_cards] = card;
    n_cards++;
    return true;
}

/**
* get number of card in the hand
* @return n_card
*/
int Hand::getN_card() const {
    return n_cards;
}

/**
* remove the last card
* @return a reference to the card
*/
const Card* Hand::removeCard() {
    if (n_cards <= 0) {
       return 0;
    }
    n_cards--;
    return &cards[n_cards];
}

/**
* overload assignment operator
* @param other a hand
* @return a hand
*/
Hand& Hand::operator=(const Hand& other) {
    if (this == &other) {
       return *this;
    }
    n_cards = other.n_cards;
    for (int i = 0; i < other.n_cards; i++) {
       this->cards[i] = other.cards[i];
    }
    return *this;
}

/**
* get top card
* @return top card in the hand
*/
const Card* Hand::getTopCard() {
    if (n_cards <= 0) {
       return 0;
    }
    return &cards[n_cards - 1];
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$50.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats