In: Computer Science
C++ Please
Do not use loops!!
Assignment : Dice Roll Redux
In "Dungeons & Dragons" and similar role-playing games, dice with different numbers of sides are used to determine the outcomes of events throughout the game.
There are different dice for different occasions: 4-sided, 6-, 8-, 10-, 12-, and 20-sided dice are common.
A required roll is typically designated <n>d<s> where <n> is the number of dice to throw and <s> is the number of sides on those dice. For example, 2d6 means throw two 6-sided dice and add the results, and 1d12 means throw one 12-sided die.
This lab requires you to write a program to read in such a designation, and then simulate throwing the appropriate dice, summing and displaying the result, three times in total.
NOTE: use the random number seed 1701, so that the test results will match when the program is implemented correctly.
We will simplify this a little by only dealing with 6, 10, 12 and 20 sided dice, and only allowing the user to specify 1-3 dice to be thrown.
For example, a program run might look like this:
Choose 1, 2, or 3 dice (6-, 10-, 12-, or 20-sided) Enter designation (e.g. '2d6'): 3d10 3d10 result: 8 + 9 + 9 = 26 3d10 result: 9 + 9 + 9 = 27 3d10 result: 5 + 1 + 9 = 15
The input should be validated, so if the number of dice is not 1, 2, or 3, or if the middle character is not 'd', or if the number of sides is not 6, 10, 12, or 20, the program should report the error:
Choose 1, 2, or 3 dice (6-, 10-, 12-, or 20-sided) Enter designation (e.g. '2d6'): 4x9 Not a valid designation
Hints:
1) Read the number of dice into an integer variable.
2) Read in (consume) the d character.
3) Read the number of sides into an integer variable.
4) An N sided die should return results in the range 1..N.
5) You might want to review the documentation on rand().
6) Do not roll more dice than specified, or it will change your random number sequence.
Note:- In the below i am taking input as string and split string to integer (d),character ,integer(s)...we shouldn't use loops here so i am taking if else instead of running loop
Code:-
#include<stdio.h>
#include<iostream>
#include<string>
#include<cstdlib>
#include<time.h> //including libraries
using namespace std;
int main(){
srand( time(0) ); //srand function to get new value
each time
int n,s; //integers n,s to store no of dice and no of
sides
char ch; //character to store 'd'
string str; //string to take input from user
cout<<"Choose 1, 2, or 3 dice (6-, 10-, 12-, or
20-sided)"<<endl; //printing
cout<<"Enter designation (e.g. '2d6'): ";
cin>>str; //getting string from user
n=(int)str[0]-48; //storing first character of string
into n value for(eg : 6d10 this line stores 6 to n as an
integer)
ch=str[1]; //appending character to ch
if(str[3]=='\0') //this condition is to check check
str[3] is present not..it is useful for knowing whether user enters
2 digit integer(10) or single digit(6)
s=(int)str[2]-48; //if single digit
(eg: 6d6 ... it store s=6)
else{
s=(int)str[2]-48; //first store
integer str[2] value to d (eg: 6d12 then s=1)
s=s*10 +((int)str[3]-48);
//multiply s with 10 and add str[3] (eg: 6d12 then s=1*10+2)
}
int rand1,rand2,rand3; //random numbers to store
values
if((n<1 || n>3) || (ch!='d') || (s!=6
&&s!=10 &&s!=12 && s!=20)){ //invalid
condition
cout<<"Not a valid
designation";
}
else{
if(n==1){ //if 1 die is
thrown
rand1=rand() % s
+ 1;
cout<<str<<" result: "<<rand1 <<" =
"<<rand1<<endl; //printing results
rand2=rand() % s
+ 1;
cout<<str<<" result: "<<rand2 <<" =
"<<rand2<<endl; //printing results
rand3=rand() % s
+ 1;
cout<<str<<" result: "<<rand3 <<" =
"<<rand3<<endl; //printing results
}
else if(n==2){ //if 2 dice is
thrown
rand1=rand() % s
+ 1;
rand2=rand() % s
+ 1;
cout<<str<<" result: "<<rand1<<" +
"<<rand2 <<" = "<<rand1+rand2<<endl;
//printing results
rand1=rand() % s
+ 1;
rand2=rand() % s
+ 1;
cout<<str<<" result: "<<rand1<<" +
"<<rand2 <<" = "<<rand1+rand2<<endl;
//printing results
rand1=rand() % s
+ 1;
rand2=rand() % s
+ 1;
cout<<str<<" result: "<<rand1<<" +
"<<rand2 <<" = "<<rand1+rand2<<endl;
//printing results
}
else{//if 3 dice is thrown
rand1=rand() % s
+ 1;
rand2=rand() % s
+ 1;
rand3=rand() % s
+ 1;
cout<<str<<" result: "<<rand1<<" +
"<<rand2 <<" + "<<rand3<<" =
"<<rand1+rand2+rand3<<endl; //printing results
rand1=rand() % s
+ 1;
rand2=rand() % s
+ 1;
rand3=rand() % s
+ 1;
cout<<str<<" result: "<<rand1<<" +
"<<rand2 <<" + "<<rand3<<" =
"<<rand1+rand2+rand3<<endl; //printing results
rand1=rand() % s
+ 1;
rand2=rand() % s
+ 1;
rand3=rand() % s
+ 1;
cout<<str<<" result: "<<rand1<<" +
"<<rand2 <<" + "<<rand3<<" =
"<<rand1+rand2+rand3<<endl; //printing results
}
}
}
Output1:-