Question
Dice rolling class ----------------------- Start by downloading the dice.cpp file from the webpage. This will come with a small main() and a function to parse
Dice rolling class
-----------------------
Start by downloading the dice.cpp file from the webpage. This will come with a small main() and a function to parse the user input into an array of ranges for dice. The format expected for the user to input is either xdy, where x and y and integers or +x where x is an integer. For example: 2d6+1 will represent rolling two 6-sided dice then adding one to the result. Multiple additions or dice can be incorporated by spacing, for example: 2 d 4 + 1 3d6 +4 will be two 4-sided dice, three 6-sided dice plus 5. Your program must have the following: 1. Create a Dice class that has: 1. Two private integer variables to store the minimum and maximum roll possible. 2. Two constructors (default and one that takes two integers for min/max values of rolls) 3. Overload the class with cout to display a random roll of the dice. In your output, you should include the following line in main(): (it is the first line displayed in the examples after the prompts) cout << "Sample [1,6] roll: " << Dice(1,6) << endl; 4. Create a roll() function that returns a random number uniformly distributed between the minimum and maximum dice roll value. 2. Create a dynamic array of the Dice class created in #1, and use this to compute the statistics. (And properly delete it!) After the single getline() in the sample code, your program should ask the user how many rounds they wish to roll. You may assume that the user does not enter a malicious input. Then roll all the dice this number of times and display the following: (in the format and order shown in the example:
Example 1 (user input is underlined): What do you want to roll? 2d4
How many rounds do you want to roll? 2
Sample roll: 6
Minimum roll: 4
Maximum roll: 5
Average roll: 4.5
---------------------------------------------------------------------------------------
Example 2 (user input is underlined):
What do you want to roll? 2d4
How many rounds do you want to roll? 2
Sample roll: 2
Minimum roll: 6
Maximum roll: 7
Average roll: 6.5
--------------------------
here is the dice.cpp
---------------------------
#include#include using namespace std; int* startEndPairs(string s); int main() { srand(time(0)); // DO NOT WRITE THIS LINE AGAIN OR ANYWHERE ELSE cout << "What do you want to roll? "; string s; getline(cin, s); int* pairs = startEndPairs(s); // DO NOT FORGET TO DELETE ME // here is what you have for(int i=1; i < pairs[0]; i+=2) { cout << "["< (s.length()); i++) { if(s[i] == 'd' || s[i] == '+') { parts++; } } // ... so we can make the correct size array to store the info string* data = new string[2*parts]; int index=0; unsigned d = s.find('d'); unsigned p = s.find('+'); while(d != static_cast (-1) || p != static_cast (-1)) { bool dFirst = d < p; if(dFirst) { string before = s.substr(0,d); // part before the 'd' (should be just one number) // figure out what number is after 'd' int count = 0; bool foundDigit=false; for(int i=0; i< static_cast (s.length()-d-1); i++) { if(isdigit(s[count+d+1])) { foundDigit=true; } if(!isdigit(s[count+d+1]) && foundDigit) { break; } count++; } string after = s.substr(d+1,count); //should be just the number after 'd' // store these two parts data[index] = before; data[index+1] = after; index+=2; // remove this part from the string s s = s.substr(d+count+1); // discard these two parts } else // same idea for the '+' { // figure out what number is after '+' int count = 0; bool foundDigit=false; for(int i=0; i< static_cast (s.length()-p-1); i++) { if(isdigit(s[count+p+1])) { foundDigit=true; } if(!isdigit(s[count+p+1]) && foundDigit) { break; } count++; } string after = s.substr(p+1,count); //should be just the number after '+' // store this part data[index] = "+"; data[index+1] = after; index+=2; // remove this part from the string s s = s.substr(p+count+1); // discard these two parts } // update d and p for next loop interation d = s.find('d'); p = s.find('+'); } // now we need to figure out how many dice there are (as 2d4 is 2 dice) // we will treat "+2" as a die that rolls [2,2] int diceCount = 0; for(int i=0; i < parts*2; i+=2) { if(data[i][0] == '+') { diceCount++; } else { diceCount+=atoi(data[i].c_str()); } } int* dice = new int[diceCount*2+1]; // one extra to store the size dice[0] = diceCount*2+1; // put size in first index int ind=1; // index for the "dice" array (as not same as data array) for(int i=0; i < parts*2; i+=2) { // if we have a +, add a "Dice" that has a range of 0 if(data[i][0] == '+') { dice[ind] = atoi(data[i+1].c_str()); dice[ind+1] = atoi(data[i+1].c_str()); ind+=2; } else // otherwise add however many of the dice requested { for(int j=0; j < atoi(data[i].c_str()); j++) { dice[ind] = 1; dice[ind+1] = atoi(data[i+1].c_str()); ind += 2; } } } return dice; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started