Sale!

CSCI240 Program 8 Classes 6-sided die solution

$25.00 $12.50

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (4 votes)

For this assignment, implement and use the methods for a class called Die.

Die class

The Die class is used to represent a 6-sided die.

Data Members

The class contains two data members.

  • An integer that holds the value of the side of the die that is up
  • An integer symbolic constant that holds the maximum number of sides on the die. Use a value of 6.

C++ 11 allows for symbolic constants to be initialized in the class definition. However, it’s not something that is supported by all compilers because class definitions are treated as patterns for what the class should contain, and therefore, they don’t take up any memory. To create a symbolic constant that will work for all compilers:

    1. In the class definition, define the symbolic constant with the keyword “static” before the definition. So:

static const int NUM_SIDES;

    1. Before main, initialize the symbolic constant with its value. So:

const int Die::NUM_SIDES = 6;

Note the dropping of the keyword “static” and the inclusion of the “Die::” in front of the constant name. This lets the compiler know that the constant belongs to the Die class.

Now, NUM_SIDES can be used in the class methods when needed.

Constructor

This class has one constructor, a default constructor (ie. one that takes no arguments). It should simply call the roll method that is described below to initialize the value of the side of the die that is up.

Methods

void roll()

This method simulates the rolling of the die. It takes no arguments and returns nothing.

The die will be “rolled” by using the rand() function to generate a random value for the side of the die that is up. The value should be between 1 and 6. Use the symbolic constant data member to help to limit the values. The result should be assigned to the integer data member.

DO NOT call the srand() function in this method.

int getValue()

This accessor method returns the current side of the die that is facing up. It takes no arguments and returns an integer.

Part 1: Testing the Die class

Before using the Die class as part of a larger project, 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 one of the methods individually.

The test program can be downloaded from Blackboard or the course website: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test8.cpp

The output that is produced by the test program:

The current side is 3

Roll 1:
You rolled a 3


Roll 2:
You rolled a 1


Roll 3:
You rolled a 1


Roll 4:
You rolled a 3


Roll 5:
You rolled a 5


Roll 6:
You rolled a 6


Roll 7:
You rolled a 4


Roll 8:
You rolled a 2


Roll 9:
You rolled a 5


Roll 10:
You rolled a 5

If the output, using your Die class, matches the above output, move on to part 2 of this assignment. Otherwise, fix the constructor/methods until the output does match.

Part 2: Using the Die class

This is the part of the assignment that will be submitted for grading.

For this part of the program, create a game that uses the Die class.

The concept of the game is fairly simple. A pair of standard six-sided die will be rolled by a dealer. A player will “wager” on whether the sum total of the dice will be even or odd. If the player wagered correctly, they win and receive a point. If the player wagered incorrectly, they lose and the dealer receives a point.

Implementing the game

Set the seed value that is used by the random number generator by using the srand() function. The program that is handed in should use a value of 35 but time(0) can be used while developing the game to get different results.

Create two Die objects that will be rolled by the dealer.

Ask the player for the number of times they would like to have the dice rolled by the dealer.

Ask the player whether the sum of the rolls will be even or odd. Have the player enter a character value of E to represent even or O to represent odd. NOTE 1: uppercase values are prefered but e and o should also be allowed. NOTE 2: you may assume that the user will enter only E, e, O, or o.

Write a loop that will execute the number of times the player wants to have the die rolled. Inside of the loop:

  • Roll the two Die objects.
  • Display the face value for each of the Die objects.
  • Calculate the sum of the face value of the two Die objects.
  • Check if the sum of the dice is even or odd. If the player wagered correctly, display a message that the player won and award him/her a point. If the player wagered incorrectly, display a message that the dealer won and give the dealer a point.

Once the dice have been rolled the correct number of times, display the player’s score, the dealer’s score, and who won the game or if it was a tie.

Programming Requirements

  1. Make sure to add #include statements for cstdlib (and ctime if time(0) is used while testing the program).
  2. Each method must have a documentation box like a function.
  3. Hand in a copy of the source code from part 2 of the assignment using Blackboard.

Output

A couple runs of the program might resemble the following but keep in mind that the random number generator is being used so your output will probably not match this output completely.

Run 1 srand(time(0)):

How many times should the dice be rolled? 5

Will the sum of the rolls will be (E)ven or (O)dd? O

Die 1:6
Die 2:6
Dealer won.

Die 1:1
Die 2:5
Dealer won.

Die 1:1
Die 2:6
You won!

Die 1:4
Die 2:3
You won!

Die 1:5
Die 2:2
You won!


Your final score is 3 points.
Dealer's final score is 2 points.
You won the game!

Run 2 srand(time(0)):

How many times should the dice be rolled? 3

Will the sum of the rolls will be (E)ven or (O)dd? E

Die 1:4
Die 2:4
You won!

Die 1:2
Die 2:3
Dealer won.

Die 1:1
Die 2:6
Dealer won.


Your final score is 1 points.
Dealer's final score is 2 points.
The dealer won the game.

Run 3 srand(time(0)):

How many times should the dice be rolled? 4

Will the sum of the rolls will be (E)ven or (O)dd? E

Die 1:6
Die 2:3
Dealer won.

Die 1:4
Die 2:6
You won!

Die 1:1
Die 2:4
Dealer won.

Die 1:1
Die 2:3
You won!


Your final score is 2 points.
Dealer's final score is 2 points.
The game was a tie.

Run 4 srand(35)):

How many times should the dice be rolled? 8

Will the sum of the rolls will be (E)ven or (O)dd? O

Die 1:1
Die 2:2
You won!

Die 1:4
Die 2:5
You won!

Die 1:2
Die 2:2
Dealer won.

Die 1:5
Die 2:3
Dealer won.

Die 1:5
Die 2:3
Dealer won.

Die 1:2
Die 2:3
You won!

Die 1:2
Die 2:1
You won!

Die 1:5
Die 2:5
Dealer won.


Your final score is 4 points.
Dealer's final score is 4 points.
The game was a tie.

Extra Credit

For up to 5 points of extra credit, add a method called draw to the Die class. The draw method should “draw” the side of the die that is currently up. For example, if 3 is the side that is up, then the draw method should “draw” something like:

 -----
|O    |
|  O  |
|    O|
 -----

on the screen.

In main, use the draw method inside of the loop. It should be called to display the two die objects rather than having that value displayed in text.

For reference, the sides of the die should resemble:

Image courtesy of http://www.clker.com/clipart-7188.html.

Note about extra credit: the points will ONLY be awarded if the required portions of the assignment work correctly. In other words, don’t take short cuts in the rest of the program because it is assumed that 5 extra points will be awarded.

Extra Credit Output

How many times should the dice be rolled? 8

Will the sum of the rolls will be (E)ven or (O)dd? o

 -----
|     |
|  O  |
|     |
 -----
 -----
|O    |
|     |
|    O|
 -----
You won!

 -----
|O   O|
|     |
|O   O|
 -----
 -----
|O   O|
|  O  |
|O   O|
 -----
You won!

 -----
|O    |
|     |
|    O|
 -----
 -----
|O    |
|     |
|    O|
 -----
Dealer won.

 -----
|O   O|
|  O  |
|O   O|
 -----
 -----
|O    |
|  O  |
|    O|
 -----
Dealer won.

 -----
|O   O|
|  O  |
|O   O|
 -----
 -----
|O    |
|  O  |
|    O|
 -----
Dealer won.

 -----
|O    |
|     |
|    O|
 -----
 -----
|O    |
|  O  |
|    O|
 -----
You won!

 -----
|O    |
|     |
|    O|
 -----
 -----
|     |
|  O  |
|     |
 -----
You won!

 -----
|O   O|
|  O  |
|O   O|
 -----
 -----
|O   O|
|  O  |
|O   O|
 -----
Dealer won.



Your final score is 4 points.
Dealer's final score is 4 points.
The game was a tie.