|
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: 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 |