Question

In: Computer Science

In python 3.7: You’re going to program a simulation of the following game. Like many probability...

In python 3.7:

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 is composed of two phases: throwing and pruning.

  • During the throwing phase, the player takes balls randomly, one at a time, from the infinite supply and tosses them at the cups creating a stack of balls in each cup. The throwing phase is over when each cup contains at least one ping-pong ball.
  • Next comes the pruning phase. During this phase the player goes through all the balls in each cup and removes any ball whose number does not match the containing cup.

Every ball drawn has a uniformly random number, every ball lands in a uniformly random cup, and every throw lands in some cup. The game is over when, after a round is completed, there are no empty cups.

After each round, print the non-empty cups and number of balls each hold.

At the end of the simulation you will print the following:

How many rounds would you used to finish this game?

How many balls did you draw and throw to finish this game?

Sort the cups in descending order by the number of balls they hold.  

only importing random, no pygame

Solutions

Expert Solution

Hi there,

I have solved the question you provided with all the necessary steps given. I had to use loops to solve the question.

The code :

import random

#counts the number of balls used
no_of_balls=0
#input the number of balls
print("Enter the number of balls and cups : ",end="")
n=int(input())
print("")
cups=[0] * n
cup_has_req_ball = [0] * n
#keep count of the cups filled
cup_filled=0
#counts the number of rounds used
no_of_rounds=0
#to check wehter atleast a single cup is filled
unique_cup=0

#simulation starts
while(1):
   print("Starting a new round...")
   no_of_rounds+=1
  
   #Throwing starts
   while(cup_filled<n):
       x=random.randrange(0,n)
       y=random.randrange(0,n)
       no_of_balls+=1
       if cups[x]==0:
           cup_filled+=1
       cups[x]+=1
       if(x==y):
           cup_has_req_ball[x]+=1
           unique_cup=1
  
   #pruning starts      
   for i in range(len(cups)):
       if(cup_has_req_ball[i] == 0):
           cup_filled-=1
           cups[i]=0
  
   #print all the non empty cups      
   if(unique_cup==1):      
       print("All the non Empty cups and the number of balls they hold are : ")
       for i in range(len(cups)):
           if(cup_has_req_ball[i] != 0):
               print(str((i+1))+"->"+str(cups[i])+" ", end="")
       print("")
   else:
       print("No non-empty cups till now...")
  
   #get out of the loop if all the cups have been filled              
   if(cup_filled==n):
       print("Game Over")
       print("")
       break      
   print("Round over...")
   print("")      
  
#print the required information
print("Number of rounds used : "+str(no_of_rounds))
print("Number of balls used : "+str(no_of_balls))
cups.sort(reverse=True)
print("The number of balls in each cups in decending order are: ")
for i in cups:
   print(i,end=" ")
print("")
          

The Code (screenshot):
                         

The Output (screensot) :

Hope you liked the answer. If you have any doubts, do let me know in the comment section.

Happy coding :)


              
      
          
          
      


Related Solutions

In python please: You’re going to program a simulation of the following game. Like many probability...
In python please: 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 (a;ll numbered 1 through N). The game is played in rounds. A round is composed of...
Please use the python 3.7 for this assigments Q1 Write a program that accepts the lengths...
Please use the python 3.7 for this assigments Q1 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is an equilateral triangle. Q2 Write a program that accepts the lengths of three sides of a triangle as inputs. The program output should indicate whether or not the triangle is a right triangle. Recall from the Pythagorean theorem that in a right triangle, the square...
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
Python 1.Suppose you were going to design an event-driven application like the famous wack-a-mole game. Do...
Python 1.Suppose you were going to design an event-driven application like the famous wack-a-mole game. Do the following: a) draw a mockup of the layout of the screen--this is usually easiest by hand. Submit a legible picture of your drawing. b) List the events your game would have to respond to (such as when certain keys are pressed). 2.Explain why you should avoid using loops to repeat actions in a GUI application. What should you do instead?
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user for the number of GPAs to enter. The program should then prompt the user to enter the specified number of GPAs. Finally, the program should print out the graduation standing of the students based on their GPAs. Your program should behave like the sample output below. Sample #1: Enter the number of GPAs: 5 GPA #0: 3.97 GPA #1: 3.5 GPA #2: 3.499 GPA...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
Explain this python program as if you were going to present it to a class in...
Explain this python program as if you were going to present it to a class in a power point presentation. How would you explain it? I am having a hard time with this. I have my outputs and code displayed throughout 9 slides. #Guess My Number Program # Python Code is modified to discard duplicate guesses by the computer import random #function for getting the user input on what they want to do. def menu(): #print the options print("\n\n1. You...
Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode)...
Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack.   When the player attacks, it will pick a random number between 0 and up to the maximum damage the player does, and then subtract that...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT