Question

   Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON Write a program dicegame.py...

  

Dice Game – Accumulator Pattern and Conditionals (40 pts) IN PYTHON

Write a program dicegame.py that has the following functions in the following order: in python

  1. Write a function roll that takes an int n and returns a random number between 1 and n inclusive. Note: This function represents a n sided die so it should produce pseudo random numbers. You can accomplish this using the random module built into python.         (3 pts)

  1. Write a function scoreRound that takes two dice rolls r1, r2, and a number s, and returns the score for a round when r1, r2 is rolled on with s-sided dice as follows:
    1. Start the score with the sum of the r1 and r2
    2. If one die is 2, and the other a 3, then add to 5 the score
    3. If the two dice rolls r1, r2 match, and are not equal to s add 5 to the score. If both die rolls are s, then the score should be deducted by 3*s points.                            (5 pts)

  1. Write a test function test_scoreRound to test the scoreRound function.                    (5pts) Use the test function to make sure your scoreRound function has no logical errors before continuing.
  2. Write a function play that takes integers rounds and sides and does the following  Plays round number of rounds, rolling two s-sided dice per round (use roll).
    • Uses the previous functions to calculate the score of each round
    • Prints out the dice rolls and the score for each round
    • Accumulates the score over all rounds
    • Returns the final score over all rounds                                                             (5 pts)
  3. Write a function result that takes an arguments goal and score and              Prints the score. Then it prints one of the following:
    • If the score at least twice the goal then the function should print “Nailed it!” o If the score is between at least the goal print “So so performance”
    • Otherwise print “Not good… not good at all!”                                             (5 pts)

  1. Write a test function test_result to test the result function.                                        (5pts)

  1. Write a main function that
    1. Asks the user to enter the number of dice sides they like to use and the number of rounds they want to play.
    2. Set the goal as round*(s+1)/2
    3. Print a message to inform the user about their goal.
    4. Then call the previous functions to play the game and display the result.               (5 pts)

  1. Write a header comment for each function                                                                                      (7 pts)

                                                                                        Page 1 of 2

  

  

Homework Answers

Answer #1

The Python 3.x Program is given below within the table. You are requested to create a python file named dicegame.py within the python ide and copy the below codes and paste into it and run the Program.

dicegame.py
import random as r

# Function -> roll(n)
# This function calculates sudo random numbers in range 1 to n and return it, simulates rolling of n-sided dice.
def roll(n):
  value = r.randint(1, n)
  return value

# Function -> scoreRound(r1, r2, s)
# This function calculates the score based on values of Roll r1, r2 and side s.
# inputs: r1 = value of Roll 1; r2 = value of Roll 2; s = number of sides in dice
# returns: the calculated score
def scoreRound(r1, r2, s):
  score = r1 + r2 # calculate the score
  # check the conditions and add up to get the final score
  # if any of the values are 2 and 3
  if (r1 == 2 and r2 == 3) or (r2 == 2 and r1 == 3):
    score = score + 5
  # if both r1 and r2 are equal and are not equal to s
  elif (r1 == r2) and (r1 != s):
    score = score + 5
  # if all the three values r1, r2, and s are equal
  elif (r1==s) and (r2==s):
    score = score - (3*s)
  return score

# Function to test the scoreRound() function with different r1, r2, s values
def test_scoreRound():
  print('Score Test #1:\nr1 = 1, r2 = 3, s = 2;\nThe score should be 4\nThe actual score is = ', scoreRound(1,3,2),'\n')
  print('Score Test #2:\nr1 = 2, r2 = 3, s = 2;\nThe score should be 10\nThe actual score is = ', scoreRound(2,3,2),'\n')
  print('Score Test #3:\nr1 = 3, r2 = 2, s = 3;\nThe score should be 10\nThe actual score is = ', scoreRound(3,2,3),'\n')
  print('Score Test #4:\nr1 = 4, r2 = 4, s = 2;\nThe score should be 13\nThe actual score is = ', scoreRound(4,4,2),'\n')
  print('Score Test #5:\nr1 = 3, r2 = 3, s = 3;\nThe score should be (r1+r2)-(3*s) = 6-9 = -3. The actual score is = ', scoreRound(3,3,3))

# Function -> play(rounds, sides)
# Function to play the dice game with given number of rounds and number sides in die
# returns: total accumulated score of all the rounds played
def play(rounds, sides):
  totalAccumulatedScore = 0
  # loop for number of rounds
  for round_ in range(0, rounds):
    # roll the dice
    r1 = roll(sides)
    r2 = roll(sides)
    # get the score
    score = scoreRound(r1, r2, sides)
    # print the values
    print('Round #', (round_ +1))
    print('Roll 1 = ', r1, 'Roll 2 = ', r2)
    print('Score = ', score)
    print()
    totalAccumulatedScore = totalAccumulatedScore + score
  return totalAccumulatedScore

# Function -> goal(goal, score)
# Function to dispay the result message based on goal and score
def result(goal, score):
  if (score >= 2*goal):
    print('Nailed it!')
  elif (score >= goal):
    print('So so performance!')
  else:
    print('Not good… not good at all!')

# Function to test the result() function
def test_result():
  print('Result Test #1:\nGoal = 10', 'Score = 20', 'Result = ', end='')
  result(10, 20)
  print('\nResult Test #2:\nGoal = 10', 'Score = 21', 'Result = ', end='')
  result(10, 21)
  print('\nResult Test #3:\nGoal = 10', 'Score = 19', 'Result = ', end='')
  result(10, 19)
  print('\nResult Test #4:\nGoal = 10', 'Score = 15', 'Result = ', end='')
  result(10, 15)
  print('\nResult Test #5:\nGoal = 10', 'Score = 10', 'Result = ', end='')
  result(10, 10)
  print('\nResult Test #6:\nGoal = 10', 'Score = 9', 'Result = ', end='')
  result(10, 9)
  print('\nResult Test #7:\nGoal = 10', 'Score = -3', 'Result = ', end='')
  result(10, -3)

# calling the test_scoreRound() function
test_scoreRound()
print()
# calling the test_result() function
test_result()
print()
print('*************** G A M E   B E G I N S ******************')
# getting the number of sides from the user
sides = int(input('Enter the number of sides of dice: '))
# getting the number of rounds to be played from user
rounds = int(input('Enter the number of rounds to be played: '))
# calculating the goal value
goal = rounds * (sides+1)/2
# display the goal to achieve
print('The goal to achieve is: ', goal)
# call the play function to play the game supplying the number of rounds and die-sides
score = play(rounds, sides)
print('Total score is = ', score)
# display the result
result(goal, score)

OUTPUT

Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
========= RESTART: C:/Program Workspace/Python 3 Workspace/dicegame.py =========
Score Test #1:
r1 = 1, r2 = 3, s = 2;
The score should be 4
The actual score is = 4

Score Test #2:
r1 = 2, r2 = 3, s = 2;
The score should be 10
The actual score is = 10

Score Test #3:
r1 = 3, r2 = 2, s = 3;
The score should be 10
The actual score is = 10

Score Test #4:
r1 = 4, r2 = 4, s = 2;
The score should be 13
The actual score is = 13

Score Test #5:
r1 = 3, r2 = 3, s = 3;
The score should be (r1+r2)-(3*s) = 6-9 = -3. The actual score is = -3

Result Test #1:
Goal = 10 Score = 20 Result = Nailed it!

Result Test #2:
Goal = 10 Score = 21 Result = Nailed it!

Result Test #3:
Goal = 10 Score = 19 Result = So so performance!

Result Test #4:
Goal = 10 Score = 15 Result = So so performance!

Result Test #5:
Goal = 10 Score = 10 Result = So so performance!

Result Test #6:
Goal = 10 Score = 9 Result = Not good… not good at all!

Result Test #7:
Goal = 10 Score = -3 Result = Not good… not good at all!

*************** G A M E B E G I N S ******************
Enter the number of sides of dice: 5
Enter the number of rounds to be played: 3
The goal to achieve is: 9.0
Round # 1
Roll 1 = 4 Roll 2 = 3
Score = 7

Round # 2
Roll 1 = 2 Roll 2 = 4
Score = 6

Round # 3
Roll 1 = 4 Roll 2 = 4
Score = 13

Total score is = 26
Nailed it!
>>>

NOTE: Please comment your doubts(if any) within the comments section and please give a thumbs up if you liked the Program.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Python program using that takes a number as input and then prints if the...
Write a Python program using that takes a number as input and then prints if the number is even or odd. The program should use a function isEven that takes a number as a parameter and returns a logical value true if the number is even, false otherwise.
Bunco is a group dice game that requires no skill. The objective of the game is...
Bunco is a group dice game that requires no skill. The objective of the game is to accumulate points by rolling certain combinations. The game is played with three dice, but we will consider a simpler version involving only two dice. How do you play two dice Bunco? There are six rounds, one for each of the possible outcomes in a die, namely the numbers one through six. Going clockwise, players take turns rolling two dice trying to score points....
Implement the following problem as a self-contained Python program (module). Please write in beginner level code!...
Implement the following problem as a self-contained Python program (module). Please write in beginner level code! Thanks! The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die. After each roll: a) If the player rolls a 1 then the player gets no new points and it becomes the other player’s turn. b) If the player rolls...
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) 5 pts def readFloat(prompt) that displays the prompt string, followed by a space, reads a floating-point number in, and returns it. Below is how you’re going to call the function: salary = readFloat(“Please enter your salary:”) percentageRaise = readFloat(“What percentage raise would you like?”) print("The salary is", salary) print("The raise percentage is", percentageRaise) keep it simple, python
Python The following program contains a number of minor typos. Write the correct code and comments...
Python The following program contains a number of minor typos. Write the correct code and comments about what was wrong. The type function returns the type for the value specified as a parameter. lex = {} lex{’a’} = ’hej’ lex[’b’] = [5 9] for k in lex.keys: print(’Information for key {k}’) print f’Val: {lex[k]}, Type: {type(lex[k])}’
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
Use python to write the code You’re going to program a simulation of the following game....
Use python to write the code You’re going to program a simulation of the following game. Like many probability games, this one involves an infinite supply of ping-pong balls. No, this game is "not quite beer pong." The balls are numbered 1 through N. There is also a group of N cups, labeled 1 through N, each of which can hold an unlimited number of ping-pong balls (all numbered 1 through N). The game is played in rounds. A round...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...