Sale!

CSCI 240 Program 10 Classes and String Processing solution

$30.00 $25.50

Category:

Description

5/5 - (4 votes)

Part 1 Overview

For this assignment, implement the methods for a class called Card.

The Card class

The Card class is used to represent a single playing card. It contains two private data members:

  • an integer that holds the face value of the card
  • a character array that holds the suit of the card. It has the ability to hold 8 characters PLUS a null terminator.

The range of possible values for the integer data member (the face of the card) is 1 through 13 inclusive, with 1 representing an Ace, 11 representing a Jack, 12 representing a Queen, and 13 representing a King. All of the other values represent that specific number on the card.

The possible values for the character array data member are: “Clubs”, “Diamonds”, “Hearts”, and “Spades”.

Constructor

The class has one constructor, a default constructor (ie. one that takes no arguments). It should create a random card by using the random number generator.

For the face value (the integer), generate a random number between 1 and 13, inclusive.

For the suit value (the character array), generate a random number between 1 and 4, inclusive. If the random number is 1, use the strcpy function to put “Clubs” in the character array data member. If the random number is 2, use the strcpy function to put “Diamonds” in the character array data member. If the random number is 3, use the strcpy function to put “Hearts” in the character array data member. If the random number is 4, use the strcpy function to put “Spades” in the character array data member.

Note: DO NOT set the seed value for the random number generator in the constructor. It will be done in main.

Methods

The following methods are required for the Card class. They should all be public.

void setCard( int newFace, const char newSuit[] )

This method sets both the face and the suit for the Card object. It takes two arguments: an integer that represents the new face value for the card and a character array that represents the new suit value for the card. It returns nothing.

Error checking is required. The integer data member should have a value between 1 and 13, inclusive. If the passed in integer value is within the required range (1-13), use it to change the face data member. Otherwise, change the face data member to 1.

The character array data member should contain “Clubs”, “Diamonds”, “Hearts”, or “Spades”. Use the strcmp function to check the passed in character array argument. If the value is one of the possible suit values, use the strcpy function to copy it into the suit data member. Otherwise, use the strcpy function to change the suit data member to “Hearts”.

int getFace()

This accessor method returns the face value of the Card. It takes no arguments and returns an integer.

const char * getSuit()

This accessor method returns the suit value of the Card. It takes no arguments and returns a pointer to a constant character.

This method should simply return the name of the character array data member. Assuming the character array data member is named suit, the code for the method should be:

const char * Card::getSuit()
{
return suit;
}

void displayCard()

This method displays a text version of the Card. It takes no arguments and returns nothing.

For the face value, if the value is:

  • 1, display “Ace”
  • 11, display “Jack”
  • 12, display “Queen”
  • 13, display “King”
  • any other value, display the value

So if a Card object has a face value of 6 and suit value of “Hearts”,

6 of Hearts

should be displayed. Or if a Card object has a face value of 12 and suit value of “Clubs”,

Queen of Clubs

should be displayed.

Testing the Card class

Before using the Card class as part of a larger project (Part 2 of this assignment), it should be tested to make sure that the constructor and all of the methods work. A short program has been written that will test each method individually.

The test program can be downloaded from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/testCard.cpp

The output that is produced by the test program on Windows PC:

Test 1: Constructor and displayCard method

  The first card is the 10 of Clubs.

  The second card is the Queen of Diamonds.

  The third card is the Ace of Spades

Test 2: setCard method

  Change the first card to the Jack of Hearts. It is now the Jack of Hearts

Test 3: setCard method with invalid values

  The second card should be the Ace of Hearts. It is now the Ace of Hearts

  The first card should be the Ace of Hearts. It is now the Ace of Hearts

Test 4: getFace and getSuit methods

  The third card should have a face value of 3. It is 3
  The suit value should be Clubs. It is Clubs

The output that is produced by the test program on Mac:

Test 1: Constructor and displayCard method

  The first card is the 5 of Clubs.

  The second card is the 2 of Spades.

  The third card is the 10 of Hearts

Test 2: setCard method

  Change the first card to the Jack of Hearts. It is now the Jack of Hearts

Test 3: setCard method with invalid values

  The second card should be the Ace of Hearts. It is now the Ace of Hearts

  The first card should be the Ace of Hearts. It is now the Ace of Hearts

Test 4: getFace and getSuit methods

  The third card should have a face value of 3. It is 3
  The suit value should be Clubs. It is Clubs

The output that is produced by the test program on CodeChef:

Test 1: Constructor and displayCard method

  The first card is the 9 of Hearts.

  The second card is the 8 of Diamonds.

  The third card is the 6 of Spades

Test 2: setCard method

  Change the first card to the Jack of Hearts. It is now the Jack of Hearts

Test 3: setCard method with invalid values

  The second card should be the Ace of Hearts. It is now the Ace of Hearts

  The first card should be the Ace of Hearts. It is now the Ace of Hearts

Test 4: getFace and getSuit methods

  The third card should have a face value of 3. It is 3
  The suit value should be Clubs. It is Clubs

Part 1 Programming Requirements

  1. Each method that is required must have a documentation box like a function.

Part 2 Overview

For part 2 of this assignment, write a modified/simplified version of the game Twenty-One. This will be done by using the Card class from Part 1 and using a pre-written class named DeckOfCards.

The concept of the simplified version of Twenty-One is, well, simple. It’s a two-player game that uses a deck of 52 cards (no jokers). Each player will draw at most 3 cards in an attempt to get a total of 21 or less. If the player draws a total of 21, they’ll be awarded 15 points. If the player draws a total less than 21, they’ll be awarded 10 points. A total greater than 21 means the player busted and will not get any points.

Once a player has completed their turn, the other player will take their turn. This process will continue until the deck of cards is empty (i.e. all the cards have been drawn from the 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.

Note 1: It is possible for a player to draw less than 3 cards. This can occur if a player draws a total of 21 before drawing 3 cards or if the all of the cards are drawn from the deck.

Note 2: For this game, an ace will always have a value of 11, the face cards (Jack, Queen, and King) will all have a value of 10, and the remaining cards will have a value equal to their face value.

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 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 shuffles the deck and 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 top of 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 (suggested logic)

Set the seed value that is used by the random number generator by using the srand() function. Pass the value 0 to the srand function.

Create a DeckOfCards object that will used for the game.

Create a Card object. This will be used to hold the card that is drawn from the deck while the game is being played.

Create at least 5 integers. They will be used to hold the number of the player who is currently drawing cards, the sum of the cards that the current player has drawn, the number of points that are won during a turn, player 1’s score, and player 2’s score.

Set the number of the player who is currently drawing cards to 1.

Set player 1 and player 2 scores to 0.

Write a loop that will execute as long as the deck of cards is not empty. Inside of the loop:

  • Display the number of the player who is currently drawing cards.
  • Initialize the sum of the cards that the current player has drawn to 0
  • Write a loop that will execute 3 times and as long as the deck of cards is not empty and as long as the sum of the cards that the current player has drawn is less than 21. Inside of the loop:
    • Draw a card from the deck of cards by calling the draw() method for the DeckOfCards object and saving the return value in the Card object
    • Display the card that was drawn by calling the displayCard() method for the Card object
    • Increment the sum of the cards that the current player has drawn based on the face value of card that was drawn. Remember that Note 2 from above lists the possible values associated with the face value of the cards.
    • Display the sum of the cards that the current player has drawn
  • If the sum of the cards that the current player has drawn is greater than 21, display a message telling the player that they’ve busted/lost and set the number of points that are won during a turn to 0. If the sum of the cards that the current player has drawn is less than 21, display a congratulatory message that includes the player number and set the number of points that are won during a turn to 10. If the sum of the cards that the current player has drawn is equal to 21, display a congratulatory message that includes the player number and set the number of points that are won during a turn to 15.
  • Increment the appropriate players score based on the number of the player who is currently drawing cards.
  • Change the number of the player who is currently drawing cards to the other player. (Hint: think about using a decision statement.)

Once the deck of cards is empty, display the scores for both players and declare a winner. If the scores are equal, declare the game a “draw”.

Part 2 Programming Requirements

  1. Each method that is required must have a documentation box like a function.
  2. Hand in a copy of the source code using Blackboard.

Output on Windows PC

Player 1:
2 of Diamonds   Total: 2
5 of Hearts   Total: 7
9 of Clubs   Total: 16

Congratulations player 1! 10 points awarded!
-------------------

Player 2:
Ace of Hearts   Total: 11
10 of Hearts   Total: 21

Congratulations player 2! 15 points awarded!
-------------------

Player 1:
6 of Diamonds   Total: 6
7 of Diamonds   Total: 13
Ace of Spades   Total: 24

Sorry player 1 -- Busted!
-------------------

Player 2:
3 of Clubs   Total: 3
Queen of Diamonds   Total: 13
Queen of Spades   Total: 23

Sorry player 2 -- Busted!
-------------------

Player 1:
Queen of Clubs   Total: 10
5 of Spades   Total: 15
Jack of Spades   Total: 25

Sorry player 1 -- Busted!
-------------------

Player 2:
3 of Spades   Total: 3
King of Spades   Total: 13
6 of Hearts   Total: 19

Congratulations player 2! 10 points awarded!
-------------------

Player 1:
Ace of Diamonds   Total: 11
10 of Clubs   Total: 21

Congratulations player 1! 15 points awarded!
-------------------

Player 2:
2 of Hearts   Total: 2
King of Hearts   Total: 12
Ace of Clubs   Total: 23

Sorry player 2 -- Busted!
-------------------

Player 1:
2 of Spades   Total: 2
5 of Diamonds   Total: 7
9 of Hearts   Total: 16

Congratulations player 1! 10 points awarded!
-------------------

Player 2:
8 of Clubs   Total: 8
King of Diamonds   Total: 18
6 of Spades   Total: 24

Sorry player 2 -- Busted!
-------------------

Player 1:
6 of Clubs   Total: 6
4 of Clubs   Total: 10
4 of Spades   Total: 14

Congratulations player 1! 10 points awarded!
-------------------

Player 2:
7 of Spades   Total: 7
8 of Diamonds   Total: 15
3 of Hearts   Total: 18

Congratulations player 2! 10 points awarded!
-------------------

Player 1:
4 of Hearts   Total: 4
9 of Diamonds   Total: 13
10 of Diamonds   Total: 23

Sorry player 1 -- Busted!
-------------------

Player 2:
4 of Diamonds   Total: 4
King of Clubs   Total: 14
7 of Clubs   Total: 21

Congratulations player 2! 15 points awarded!
-------------------

Player 1:
10 of Spades   Total: 10
3 of Diamonds   Total: 13
Queen of Hearts   Total: 23

Sorry player 1 -- Busted!
-------------------

Player 2:
9 of Spades   Total: 9
5 of Clubs   Total: 14
Jack of Hearts   Total: 24

Sorry player 2 -- Busted!
-------------------

Player 1:
7 of Hearts   Total: 7
Jack of Clubs   Total: 17
8 of Spades   Total: 25

Sorry player 1 -- Busted!
-------------------

Player 2:
2 of Clubs   Total: 2
Jack of Diamonds   Total: 12
8 of Hearts   Total: 20

Congratulations player 2! 10 points awarded!
-------------------


Player 1 score: 45
Player 2 score: 60

Player 2 won!

Output on MAC

Player 1: 
3 of Hearts   Total: 3
9 of Spades   Total: 12
8 of Spades   Total: 20

Congratulations player 1! 10 points awarded!
-------------------

Player 2: 
9 of Hearts   Total: 9
10 of Clubs   Total: 19
King of Hearts   Total: 29

Sorry player 2 -- Busted!
-------------------

Player 1: 
Jack of Spades   Total: 10
4 of Spades   Total: 14
7 of Spades   Total: 21

Congratulations player 1! 15 points awarded!
-------------------

Player 2: 
Ace of Diamonds   Total: 11
Jack of Hearts   Total: 21

Congratulations player 2! 15 points awarded!
-------------------

Player 1: 
8 of Clubs   Total: 8
Ace of Clubs   Total: 19
7 of Clubs   Total: 26

Sorry player 1 -- Busted!
-------------------

Player 2: 
Ace of Spades   Total: 11
10 of Spades   Total: 21

Congratulations player 2! 15 points awarded!
-------------------

Player 1: 
8 of Hearts   Total: 8
10 of Hearts   Total: 18
Queen of Clubs   Total: 28

Sorry player 1 -- Busted!
-------------------

Player 2: 
6 of Spades   Total: 6
3 of Diamonds   Total: 9
8 of Diamonds   Total: 17

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
Queen of Spades   Total: 10
Jack of Clubs   Total: 20
3 of Spades   Total: 23

Sorry player 1 -- Busted!
-------------------

Player 2: 
King of Spades   Total: 10
2 of Spades   Total: 12
4 of Clubs   Total: 16

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
Queen of Hearts   Total: 10
6 of Diamonds   Total: 16
Queen of Diamonds   Total: 26

Sorry player 1 -- Busted!
-------------------

Player 2: 
2 of Diamonds   Total: 2
7 of Diamonds   Total: 9
9 of Diamonds   Total: 18

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
2 of Hearts   Total: 2
Ace of Hearts   Total: 13
4 of Diamonds   Total: 17

Congratulations player 1! 10 points awarded!
-------------------

Player 2: 
10 of Diamonds   Total: 10
7 of Hearts   Total: 17
King of Clubs   Total: 27

Sorry player 2 -- Busted!
-------------------

Player 1: 
3 of Clubs   Total: 3
5 of Spades   Total: 8
6 of Clubs   Total: 14

Congratulations player 1! 10 points awarded!
-------------------

Player 2: 
2 of Clubs   Total: 2
5 of Hearts   Total: 7
4 of Hearts   Total: 11

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
9 of Clubs   Total: 9
5 of Clubs   Total: 14
Jack of Diamonds   Total: 24

Sorry player 1 -- Busted!
-------------------

Player 2: 
5 of Diamonds   Total: 5
6 of Hearts   Total: 11
King of Diamonds   Total: 21

Congratulations player 2! 15 points awarded!
-------------------


Player 1 score: 45
Player 2 score: 85

Player 2 won!

Output on CodeChef

Player 1: 
8 of Spades   Total: 8
10 of Clubs   Total: 18
8 of Hearts   Total: 26

Sorry player 1 -- Busted!
-------------------

Player 2: 
3 of Spades   Total: 3
9 of Spades   Total: 12
2 of Spades   Total: 14

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
King of Clubs   Total: 10
5 of Hearts   Total: 15
9 of Hearts   Total: 24

Sorry player 1 -- Busted!
-------------------

Player 2: 
4 of Spades   Total: 4
Ace of Clubs   Total: 15
2 of Hearts   Total: 17

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
Ace of Spades   Total: 11
Ace of Hearts   Total: 22

Sorry player 1 -- Busted!
-------------------

Player 2: 
2 of Diamonds   Total: 2
King of Diamonds   Total: 12
Queen of Clubs   Total: 22

Sorry player 2 -- Busted!
-------------------

Player 1: 
Queen of Spades   Total: 10
6 of Spades   Total: 16
6 of Diamonds   Total: 22

Sorry player 1 -- Busted!
-------------------

Player 2: 
5 of Diamonds   Total: 5
10 of Diamonds   Total: 15
9 of Diamonds   Total: 24

Sorry player 2 -- Busted!
-------------------

Player 1: 
10 of Hearts   Total: 10
7 of Clubs   Total: 17
3 of Clubs   Total: 20

Congratulations player 1! 10 points awarded!
-------------------

Player 2: 
6 of Hearts   Total: 6
7 of Diamonds   Total: 13
3 of Diamonds   Total: 16

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
8 of Clubs   Total: 8
Ace of Diamonds   Total: 19
Jack of Clubs   Total: 29

Sorry player 1 -- Busted!
-------------------

Player 2: 
Jack of Hearts   Total: 10
4 of Diamonds   Total: 14
5 of Clubs   Total: 19

Congratulations player 2! 10 points awarded!
-------------------

Player 1: 
4 of Clubs   Total: 4
4 of Hearts   Total: 8
10 of Spades   Total: 18

Congratulations player 1! 10 points awarded!
-------------------

Player 2: 
9 of Clubs   Total: 9
Queen of Hearts   Total: 19
2 of Clubs   Total: 21

Congratulations player 2! 15 points awarded!
-------------------

Player 1: 
6 of Clubs   Total: 6
5 of Spades   Total: 11
Jack of Diamonds   Total: 21

Congratulations player 1! 15 points awarded!
-------------------

Player 2: 
King of Hearts   Total: 10
Queen of Diamonds   Total: 20
King of Spades   Total: 30

Sorry player 2 -- Busted!
-------------------

Player 1: 
8 of Diamonds   Total: 8
3 of Hearts   Total: 11
7 of Spades   Total: 18

Congratulations player 1! 10 points awarded!
-------------------

Player 2: 
7 of Hearts   Total: 7
Jack of Spades   Total: 17

Congratulations player 2! 10 points awarded!
-------------------


Player 1 score: 45
Player 2 score: 65

Player 2 won!