Code in Python
Posted: Thu May 05, 2022 1:33 pm
Code in Python
Problem 1 (25 pts) You and some friends recently watched Stranger Things and have decided you want to try the dice-based role playing game Dungeons & Dragons (DnD). In DnD, attacks consist of two phases: 1. Roll a 20-sided die (called a d20) to see if you hit the enemy you are trying to attack. A hit is achieved if you meet or exceed your enemy's armor class (AC). 2. Roll a given number of dice to determine what your attack damage is. Damage computations are the sum of: • The base damage, which is a fixed number that is always added to your damage (e.g., +3) The damage added to your base from rolling me 6-sided dice, written med6 (e.g., if you roll three 6-sided dice, we write 3d6) The damage added to your base from rolling m₁0 10-sided dice, written m₁ad10 (e.g., if you roll two 10-sided dice, we write 2d10) Critical: If you rolled a 20 on your initial roll of your d20 (Step 1 above), you score a critical hit and double the sum of the above damage For example, suppose your enemy has an AC of 10. You roll your d20 and obtain a 12, which is a hit. Your damage is 2d6+3d10+4, so you roll two 6-sided dice and three 10-sided dice, sum the total these dice and add 4 more. You will write a Python script to compute your damage for a given attack, as well as to compute some simple statistics of different attacks. Required Tasks: 1. (5 pts) Write a Python function called calculate attack that fulfills these specifications: Accepts four input arguments: (a) Your enemy's AC (b) Your base damage (c) The number of six-sided dice you will roll (d) The number of ten-sided dice as you will roll • Determines the total damage done to the enemy, accounting for misses and critical hits. You should make use of the np.random.randint function for this.
Returns the total damage done to the enemy 2. (20 pts) In the main section of the Python script, within a loop: • Displays a menu allowing the user to select from these options: (a) [a] Display the attack damage for a single attack, receiving the enemy's AC, the base damage, and the number of each type of die as user input. (b) [s] Display the mean, median, standard deviation, min, and max damage over N = 1000 attacks for a given AC, base damage, and number of each type of die as user input. (c) [q] Quit the program. If [a] is requested, calls your calculate attack function to do the actual calculation and then displays the result with no values after the decimal point. If [s] is requested, calls your calculate attack function to compute the statistics of the attack and then displays the result up to two decimal points. Save the script using the file name hw2_p1.py. An example output is given in Fig. below.
Problem 1 (25 pts) You and some friends recently watched Stranger Things and have decided you want to try the dice-based role playing game Dungeons & Dragons (DnD). In DnD, attacks consist of two phases: 1. Roll a 20-sided die (called a d20) to see if you hit the enemy you are trying to attack. A hit is achieved if you meet or exceed your enemy's armor class (AC). 2. Roll a given number of dice to determine what your attack damage is. Damage computations are the sum of: • The base damage, which is a fixed number that is always added to your damage (e.g., +3) The damage added to your base from rolling me 6-sided dice, written med6 (e.g., if you roll three 6-sided dice, we write 3d6) The damage added to your base from rolling m₁0 10-sided dice, written m₁ad10 (e.g., if you roll two 10-sided dice, we write 2d10) Critical: If you rolled a 20 on your initial roll of your d20 (Step 1 above), you score a critical hit and double the sum of the above damage For example, suppose your enemy has an AC of 10. You roll your d20 and obtain a 12, which is a hit. Your damage is 2d6+3d10+4, so you roll two 6-sided dice and three 10-sided dice, sum the total these dice and add 4 more. You will write a Python script to compute your damage for a given attack, as well as to compute some simple statistics of different attacks. Required Tasks: 1. (5 pts) Write a Python function called calculate attack that fulfills these specifications: Accepts four input arguments: (a) Your enemy's AC (b) Your base damage (c) The number of six-sided dice you will roll (d) The number of ten-sided dice as you will roll • Determines the total damage done to the enemy, accounting for misses and critical hits. You should make use of the np.random.randint function for this.
Returns the total damage done to the enemy 2. (20 pts) In the main section of the Python script, within a loop: • Displays a menu allowing the user to select from these options: (a) [a] Display the attack damage for a single attack, receiving the enemy's AC, the base damage, and the number of each type of die as user input. (b) [s] Display the mean, median, standard deviation, min, and max damage over N = 1000 attacks for a given AC, base damage, and number of each type of die as user input. (c) [q] Quit the program. If [a] is requested, calls your calculate attack function to do the actual calculation and then displays the result with no values after the decimal point. If [s] is requested, calls your calculate attack function to compute the statistics of the attack and then displays the result up to two decimal points. Save the script using the file name hw2_p1.py. An example output is given in Fig. below.