Question

In: Computer Science

C++ Please Do not use loops!! Assignment : Dice Roll Redux In "Dungeons & Dragons" and...

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.

Solutions

Expert Solution

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


Related Solutions

THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if...
THE STRING MATCH PROBLEM C++ only. Must use loops. Please do not use sequence of if statements. . Given 2 strings, a and b, set result to the number of the positions where they contain the same length 2 substring. So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. • for input of "xxcaazz", "xxbaaz" → 3 • for input of "abc", "abc" → 2 • for input...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program...
Please complete in only C++, using loops Assignment: For this assignment you’ll be designing a program which can take the input of a decimal number and a numerical base, and convert the decimal number to that base. For example, if given the decimal number seven and the base two, your program should output it as 111, which is how seven is represented in binary. Another example, 8,943 in base 10, is 13,236 in base 9. You’ll need to perform these...
role playing games like dungeons & dragons use many different types of dice. suppose that a...
role playing games like dungeons & dragons use many different types of dice. suppose that a SIX sided die has faces marked 1,2,3,4,5,6. The intelligence of a character is determined by rolling this die twice and adding 1 to the sum of the spots. The faces are equally likely and the two rolls are independent. what is the average (mean) intelligence for such characters? how spread out are their intelligence , as measured by the standard deviation of the distribution?(round...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I...
THE NOT REPLACE PROBLEM Must use loops. Please do not use sequence of if statements. I need it in C++. Given an input string, set result to a string where every appearance of the lowercase word "is" has been replaced with "is not". The word "is" should not be immediately preceded or followed by a letter -- so for example the "is" in "this" does not count.   • for input of "is test" → "is not test" • for input...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Please using python to do the following code: You roll two six-sided dice, each with faces...
Please using python to do the following code: You roll two six-sided dice, each with faces containing one, two, three, four, five and six spots, respectively. When the dice come to rest, the sum of the spots on the two upward faces is calculated. • If the sum is 7 or 11 on the first roll, you win. • If the sum is 2, 3 or 12 on the first roll (called “Mygame”), you lose (i.e., the “house” wins). •...
A pair of dice are rolled. Let A be the event as “the first dice roll...
A pair of dice are rolled. Let A be the event as “the first dice roll is 3” and event B as “the second dice roll is 4”. Let event C be as “the sum of the dice rolls are 7.” a) Show that A and B are independent, that A and C are independent, and that B and C are independent. b) Show that A, B, and C are not mutually independent.
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Design an application that uses the C# random generator to randomly roll a six face dice...
Design an application that uses the C# random generator to randomly roll a six face dice ( no 1 - no 6) . If the user rolled a 3, the user gets to roll the six face die again. You must use a switch statement to write the result /s of the random dice roll/s to the screen. Hint: You should have nested switches in your program Using: Console App(.NET Framework)
You roll two fair dice. Let A be the event that the sum of the dice...
You roll two fair dice. Let A be the event that the sum of the dice is an even number. Let B be the event that the two results are different. (a) Given B has occurred, what is the probability A has also occurred? (b) Given A has occurred, what is the probability B has also occurred? (c) What is the probability of getting a sum of 9? (d) Given that the sum of the pair of dice is 9...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT