Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part C: Simulated Dice In Part C, you will add constants and functions related to rolling dice. Put the constants and functions in Dice.h and

image text in transcribed

Part C: Simulated Dice

In Part C, you will add constants and functions related to rolling dice. Put the constants and functions in Dice.h and the function implementations in Dice.cpp. By the end of Part C, you will have functions with the following prototypes: void diceInit (); int diceRoll (); void dicePrint2and2 (int r1, int r2, int c1, int c2); Perform the following steps:

In Dice.cpp, include the , , and libraries and "Dice.h". The library (the C standard library) contains functions for generating random numbers, and the library will be needed for initialization. Remember to put using namespace std;

In Dice.h, define DICE_SIDE_COUNT as an integer constant with value 4.

Note: You will not need any #includes in Dice.h. However, you should still put #pragma once.

In Dice.h, copy in the function prototypes shown above.

In Dice.cpp, add an implementation for the diceInit function. The only thing it should contain is the line: srand((unsigned int)time(NULL));

Note: This line will seed the random number generator based on the current time. Without it, you would get the same "random" numbers each time you ran the program. You are not expected to understand how this line works.

Debugging: If you have an intermittent bug, this line may be why it only sometimes happens. Try commenting it out. If the bug now always happens, you will find it much easier to fix. If it never happens, try calling srand with a number as an argument (e.g. srand(4) or srand(123456789), etc.) and see if the bug happens reliably then. If it still never happens, try a few other numbers. If the bug still happens unpredictably, randomness is not the problem. Check if you ever use an invalid array index.

Add an implementation for the diceRoll function that returns a random number in the half-open interval [0, DICE_SIDE_COUNT).

Note: A half-open interval includes the lower bound but not the upper bound. So this function should sometimes return 0, but it should never return DICE_SIDE_COUNT.

Hint: You can use the rand() function to get a random number. You can then use the modulus operator (a % b) to get an integer from 0 to b - 1.

Add an implementation for the dicePrint2and2 function that prints a picture of four dice. Also print row and column labels, exactly as shown under The Dice above. The numbers shown on the dice should be the values passed in as the four parameters. Assume that the numbers will always have exactly one digit.

Hint: To allow for automatic marking, the output has to be exactly right. Make sure there are five blanks between the dice. Do not put any blanks after the last visible character on the line.

Part C: Simulated Dice [10% = 7% test program + 3% output] In Part C, you will add constants and functions related to rolling dice. Put the constants and functions in Dice.h and the function implementations in Dice.cpp. By the end of Part C, you will have functions with the following prototypes: void diceInit (); int diceRoll (); void dicePrint2and2 (int r1, int r2, int c1, int c2); Perform the following steps:

In Dice.cpp, include the , , and libraries and "Dice.h". The library (the C standard library) contains functions for generating random numbers, and the library will be needed for initialization. Remember to put using namespace std;

In Dice.h, define DICE_SIDE_COUNT as an integer constant with value 4.

Note: You will not need any #includes in Dice.h. However, you should still put #pragma once.

In Dice.h, copy in the function prototypes shown above.

In Dice.cpp, add an implementation for the diceInit function. The only thing it should contain is the line: srand((unsigned int)time(NULL));

Note: This line will seed the random number generator based on the current time. Without it, you would get the same "random" numbers each time you ran the program. You are not expected to understand how this line works.

Debugging: If you have an intermittent bug, this line may be why it only sometimes happens. Try commenting it out. If the bug now always happens, you will find it much easier to fix. If it never happens, try calling srand with a number as an argument (e.g. srand(4) or srand(123456789), etc.) and see if the bug happens reliably then. If it still never happens, try a few other numbers. If the bug still happens unpredictably, randomness is not the problem. Check if you ever use an invalid array index.

Add an implementation for the diceRoll function that returns a random number in the half-open interval [0, DICE_SIDE_COUNT).

Note: A half-open interval includes the lower bound but not the upper bound. So this function should sometimes return 0, but it should never return DICE_SIDE_COUNT.

Hint: You can use the rand() function to get a random number. You can then use the modulus operator (a % b) to get an integer from 0 to b - 1.

Add an implementation for the dicePrint2and2 function that prints a picture of four dice. Also print row and column labels, exactly as shown under TheDice above. The numbers shown on the dice should be the values passed in as the four parameters. Assume that the numbers will always have exactly one digit.

HINT To allow for automatic marking, the output has to be exactly right. Make sure there are five blanks between the dice. Do not put any blanks after the last visible character on the line.

The Dice The simulated dice have four sides with the values 0, 1, 2, and 3. When choosing a grid cell in the game, two dice are rolled and their values are added to give the row number. For example, if 1 and 3 were rolled, the row would be 1 3 4. Another two dice are rolled and added to choose the column. As a result, the cells near the center will be chosen more frequently. The program will use symbols to print the dice values so that they look like they are on dice. For example: + Row Column +---+ + -- - + +---+ +---+ | 2 | 3 | | 1 | 10 | +---+ +---+ +---+ + ---+ + = + 0 = 1 were Here, row 2 3 5 and column 1 rolled, so the cell chosen is F1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions