CSCI 240 Spring 2025

Assignment 8
Classes
(100 points)


Due: Friday, April 18 on the autograder and Blackboard by 11:59 PM

Overview

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

The Die class

The Die class is used to represent a multi-sided (minimum of 6 sides) die.

The cpp file that is submitted for grading must be named assign8.cpp.

Data Members

The class contains three data members.

Constructors

The first constructor for the class is a default constructor, which means that it takes no arguments. It should initialize the number of sides to the minimum of 6, using the constant. The value of the face of the die that is up should be set by simply calling the roll method that is described below.

The alternate constructor for the class takes one argument: an integer that represents the number of sides. It should use the passed in number of sides if the value is greater than or equal to the minimum number of sides. Otherwise, use the minimum number of sides, making sure to use the constant where needed. The value of the face of the die that is up should be set by simply calling the roll method that is described below.

Methods

The following methods are required for the Die class.

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 face of the die that is up. The value should be between 1 and the number of sides.

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

int getFace()

This public accessor method returns the current face of the die. It takes no arguments and returns an integer.

int getNumSides()

This public accessor method returns the number of sides of the die. It takes no arguments and returns an integer.

Part 1: Testing the Die class

DO NOT submit this version to the autograder.

Before using the Die class as part of a larger project, it should be tested to make sure that the constructors and all the methods work. A short program has been written that will test each of the methods individually.

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

The WINDOWS output that is produced by the test program:

There are 6 sides and the current face is 1

Roll 1:
You rolled a 2

Roll 2:
You rolled a 4

Roll 3:
You rolled a 6

Roll 4:
You rolled a 1

Roll 5:
You rolled a 3


die2 has 8 sides and the current face is 8

die3 has 6 sides and the current face is 4

The MAC output that is produced by the test program:

There are 6 sides and the current face is 6

Roll 1:
You rolled a 6

Roll 2:
You rolled a 5

Roll 3:
You rolled a 3

Roll 4:
You rolled a 1

Roll 5:
You rolled a 4


die2 has 8 sides and the current face is 1

die3 has 6 sides and the current face is 2

The onlinegdb output that is produced by the test program:

There are 6 sides and the current face is 2

Roll 1:
You rolled a 6

Roll 2:
You rolled a 1

Roll 3:
You rolled a 1

Roll 4:
You rolled a 3

Roll 5:
You rolled a 5


die2 has 8 sides and the current face is 1

die3 has 6 sides and the current face is 4

If the output, using the 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 version of the program that will be submitted for grading on Blackboard and on the autograder.

The cpp file that is submitted for grading must be named assign8.cpp.

For this part of the assignment, write a CPP program that will use the Die class to simulate a single game of Craps.

Craps is a game of chance where a player (the shooter) will roll 2 six-sided die. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues.

If the sum of the first roll of the dice is equal to 7 or 11, the player wins immediately.

If the sum of the first roll of the dice is equal to 2, 3, or 12, the player has rolled "craps" and loses immediately.

If the sum of the first roll of the dice is equal to 4, 5, 6, 8, 9, or 10, the game will continue with the sum becoming the "point". The object of the game is now for the player to continue rolling the dice until they either roll a sum that equals the point or they roll a 7. If the player "makes their point" (i.e. rolls a sum that equals the point), they win. If they roll a 7, they lose.

Implementing the game

Ask the user for the seed value for the random number generator and use it to seed the random number generator.

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

Next, roll the dice and calculate the sum of the faces. Display the sum in the format:

Roll: [face of Die object 1] + [face of Die object 2] = [sum of Die object faces]

where the three [---] are replaced by the specified values.

If the sum of the dice is equal to 7 or 11, the game is over and the player has won. Display a congratulatory message of "Seven! Winner!".

If the sum of the dice is equal to 2, 3, or 12, the game is over and the player has lost. Display a message indicating the player has lost because they rolled craps -- "Craps! You lose!".

For any other sum, the sum is now the point and the game should continue until the user rolls the point again or rolls a 7. Use the message "The point was made. Winner!" when the user rolls the point again and the message "Seven'd out! You lose!" when the user rolls a 7.

Programming Requirements

  1. Each method MUST have a documentation box like a function.

  2. Hand in a copy of the source code (the CPP file) from Part 2 of the assignment on the autograder and Blackboard.

Output

Windows PC

Seed value? 6

Roll: 6 + 2 = 8


The point is 8


Roll: 5 + 4 = 9

Roll: 5 + 5 = 10

Roll: 3 + 3 = 6

Roll: 5 + 4 = 9

Roll: 1 + 2 = 3

Roll: 5 + 4 = 9

Roll: 2 + 5 = 7

Seven'd out! You lose!

Mac

Seed value? 4

Roll: 6 + 2 = 8


The point is 8


Roll: 3 + 3 = 6

Roll: 1 + 1 = 2

Roll: 1 + 2 = 3

Roll: 6 + 1 = 7

Seven'd out! You lose!

onlinegdb

Seed value? 12

Roll: 3 + 5 = 8


The point is 8


Roll: 2 + 4 = 6

Roll: 6 + 4 = 10

Roll: 2 + 1 = 3

Roll: 5 + 4 = 9

Roll: 2 + 3 = 5

Roll: 5 + 5 = 10

Roll: 6 + 2 = 8

The point was made. Winner!