Assignment 4

Back Up Next

Home
Assignment 1
Assignment 2
Assignment 3
Assignment 4
Assignment 5
Assignment 6

simulating a rolling dice

For this assignment, you will create a program that simulates rolling dice. The program must be able to simulate any number of dice (one or more), and roll them any number of times (one or more). For each roll, the program generates a random value between one and six, inclusive, to represent each die. The program prints the value of each die, and then prints the total value of all dice. Below is a sample output:

How many dice? 5
How many rolls? 3
1 2 2 1 5 = 11
4 5 4 5 5 = 23
3 1 6 3 2 = 15
Thank you for playing.

Define a class, Die, to represent a single die. The Die class must have a constructor, which ensures that the value of the die is initialized to a legal value (1 to 6). It must have a roll() member function, to generate a new, random value. You must decide if the class needs any other member functions. Declare the class in the file die.h. Implement the member functions in the file die.cpp.

Define a class, Dice, to represent a set of dice. The constructor must take an integer argument, specifying the number of dice to create. The Dice constructor allocates an array of Die objects. The destructor deletes the array. Define a roll() member function for the Dice class, which in turn calls roll() for each Die object. Define any other member functions that you feel are necessary. Declare the class in dice.h, and implement the functions in dice.cpp.

You must also write a main program. The main program prompts the user for information, creates the Dice object, rolls the dice the specified number of times, and exits.

Turn in the all the project files on the diskette. Print all the source files and one or more transcripts, showing that the program works the way you want it to.

Here is a sample of the main program, please note that you do not have to implement your program this way, but you are required to include classes on your program.

 

 

// Name: Ray Lischner
// Date: 21 February 1997
// Simple dice demonstration.
// The user can choose how many dice to use (1 or more).
// Each die is an ordinary cubic die, with 1 to 6 spots
// on each face. This programs "rolls" the dice by choosing
// random numbers between 1 and 6 (inclusive).  
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "dice.h"  
int main()
{
   // Initialize the random number generator.
   time_t t = time(0);
   srand(t);  
   unsigned ndice;            // number of dice to roll  
   // Ask the user for the number of dice to use.
   do {
      cout << "How many dice? ";
        cin >> ndice;
      if (ndice < 1)
        cout << "Please choose a number >= 1." << endl;
   }
   while (ndice < 1);  
   unsigned nrolls;          // number of rolls  
   // Ask the user for the number of rolls.
   do {
           cout << "How many rolls? ";
        cin >> nrolls;
      if (nrolls < 1)
        cout << "Please choose a number >= 1." << endl;
   }
   while (nrolls < 1);  
   // Create the dice.
   Dice dice(ndice);  
   // Throw the dice the specified number of times.
   while (nrolls-- > 0)
   {
      dice.roll();
      cout << dice << endl;
   }  
   cout << "Thank you for playing." << endl;  
   // Windows: wait for user to close the DOS window
   char wait;
   cin >> wait;  
   } 

You will need to use the information mailed to you (batch file) to compile all the files together or create a project as indicated here.

 

Back to CS162 Homepage
This page was last modified May 08, 2000
wmorales@pcc.edu