Challenge 22

Description

The purpose of this challenge is to define several aggregated classes with overloaded operators. This challenge simulates playing cards and a modified poker game.

 Requirements

  1. Define a class called Card as below. Complete the as_string() function and the overloaded operators as described in the comments below.
    class Card
    {
      private: 
        // suit contains values 1-4, to represent suits below
        // 1 - Diamonds, 2 - Hearts, 3 - Clubs, 4 - Spades
        int suit; 
        // value contains values 1 - 13
        // Ace - 1, 2-10, J - 11, Q - 12, K - 13
        int value;
      public:
        // you will need to write:
        //  - a default constructor (used in Step 2)
        //  - an overloaded constructor (used in Step 1)
    
        // set values - this will be used later in Step 2
        void set(int s, int v)
        {
          suit = s;
          value = v;
        }
    
        // function to return a string that describes the card
        string as_string()
        {
          // for example, if suit == 1 and value == 1
          // this function would return "Ace of Diamonds"
        }  
    
        // write an overloaded operator for less-than-or-equal-to 
        // this function allows you to compare two cards. 
        // despite an Ace having a value of 1, it is 
        // actually greater in value than all the other cards
        // make sure your overload for less-than-or-equal-to takes
        // this into account
    
        // write an overloaded operator for << that will allow
        // you to cout a Card object. This overload will call the 
        // as_string() function to display its description
    };
  2. Define a class called Deck as below. Complete the constructor and the shuffle() functions as described in the comments below.
    class Deck
    {
      private: 
        Card cards[52];
        // the inplay array holds a true/false value to indicate
        // if the card in the same position is currently in play
        // a card is "in play" if it has been drawn out of the deck
        bool inplay[52];
      public:
        // constructor will set suit and value for each of the 
        // cards in the array 
        Deck()
        {
        }
    
        void unplay()
        {
          for (int i = 0; i < 52; i++)
            inplay[i] = false;
        }
    
        // randomize/shuffle the deck of cards so that the 
        // cards in the array are in a random order
        void shuffle()
        {
          for (int i = 0; i < 52; i++)
          {
            // identify a card at position i
            // generate a random number between 0 and 51
            //    int random = rand() % 52;
            // the rand() function needs the <cstdlib> header file
            // use random as an index into the card array
            // swap the card values for the cards at positions i
            // and random
          }
    
          unplay();  // set all cards to "not in play"
        }
    
        Card draw()
        {
          Card drawn;
    
          // returns a Card object. randomly pick a card out of the 
          // deck that is not currently in play. A card is in play
          // if its corresponding value in the inplay array is true
          // Once a random card has been picked out of the deck,
          // set the corresponding inplay value to true
          
          return drawn;
        }
    };
  3. Write a class Game as below. Complete the is_flush() and deal() function as described in the comments.
    class Game
    {
      private:
        Deck deck;
      public:
        // constructor will shuffle the deck
        Game()
        {
          deck.shuffle();
        }
    
        // this function will determine if the 5-card hand passed 
        // as the parameter is a flush. A flush is a hand where
        // all the cards have the same suit
        bool is_flush(Card hand[])
        {
        }
    
        // OPTIONAL - this function will determine if the 5-card
        // hand passed as the paremeter is a Four-Of-A-Kind. A FOAK
        // has 4 cards with the same value. The 5th card can be 
        // any value
        bool is_foak(Card hand[])
        {
        }
    
        void deal(Card hand[])
        {
          // this function will select 5 cards out of the 
          // deck using the Deck's draw() function. Remember that
          // arrays passed as parameters can be set in the function
          // and made available to the caller's scope
        }
    
    };

Sample main() for Step 1

// class definitions here

// Use this main() function for Step 1
int main()
{
  Card test1(1,1);   // Ace of Diamonds
  Card test2(1,10);  // 10 of Diamonds

  cout << test1 << endl;    // should show "Ace of Diamonds"
  
  if (test2 <= test1)
  {
    // should show "Ace of Diamonds beats 10 of diamonds"
    cout << test1 << " beats " << test2 << endl;
  }
  else
  {
    cout << test2 << " beats " << test1 << endl;
  }
}

Sample main()

// class definitions here

// replace the main() used in Step 1 above with this main()
int main()
{
  Game poker;
  Card hand[5];

  poker.deal(hand);

  for (int i = 0; i < 5; i++)
  {
    cout << hand[i] << endl;
  }

  if (poker.is_flush(hand))
    cout << "Your hand is a flush" << endl;
  else
    cout << "No flush" << endl;

  Deck deck1;  
  deck1.shuffle();
  Card card1, card2;

  card1 = deck1.draw();
  card2 = deck1.draw();

  if (card1 <= card2)
    cout << card2 << " beats or matches " << card1 << endl;
  else
    cout << card1 << " beats " << card2 << endl;

  return 0;
}

Sample Interaction

Ace of Diamonds
9 of Diamonds
3 of Diamonds
Queen of Diamonds
8 of Diamonds

Your hand is a flush!

8 of Spades beats or matches 3 of Diamonds
9 of Hearts
King of Spades
King of Hearts
Ace of Clubs
6 of Clubs

No flush

Ace of Hearts beats 10 of Spades

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0022

Print Requirements