# This program is a simple simulation of the racquetball game. # Racquetball is a sport played between two players using racquets # to strike a ball in a four-walled court. # To start the game, one of the players puts the ball into play # (called serving). The players then alternate hitting the ball # to keep it in play. This is a rally. The rally ends when one of # the players fails to hit a legal shot. The player who misses # the shot loses the rally. If the loser is the player who served, # then service passes to the other player. # If the server wins the rally, a point is awarded. # Players can only score points during their own service. # The first player to reach 15 points wins the game. # # # Convention: # in our simulation the ability-level of players # will be represented by the probability that the # player wins the rally when he or she serves. # # Pa – the probability that the player A wins on his/her serve # Pb – the probability that the player B wins on his/her serve # # Assumption: # in each game player A serves first def main(): Intro() # print an introduction Pa, Pb, n = getInput() # getting input from the user print("Got it! Starting the simulation....") # simulating n games, returning the result of simulation WinsA, WinsB = simNgames(n,Pa,Pb) report(n,WinsA,WinsB) # reporting back to the user def Intro(): print('Hello, this is a simulation of the racquetball game.') print('Racuetball is a game for two players: Player A and Player B') def getInput(): ProbA = float(input('Please, input the probability that\n player \ A wins on his/her serve (i.e. Ability-level) as %:')) print("You entered {0} %".format(ProbA)) ProbB = float(input('Please, input the probability that\n player \ B wins on his/her serve (i.e. Ability-level) as %:')) print("You entered {0} %".format(ProbB)) n = int(input('Input the number of games you\'d like to simulate:')) return ProbA/100, ProbB/100, n def report(n,wA,wB): print("\t REPORT:") print("{0:0d} games are simulated".format(n)) PercentA = wA*100.0/n print("Player A won {0} games, which is {1:0.1f} %".format(wA,PercentA)) PercentB = wB*100.0/n print("Player B won {0} games, which is {1:0.1f} %".format(wB,PercentB)) print("Have a good day!") def simNgames(n,Pa,Pb): #print("Entering simNgames....") WinsA = 0 WinsB = 0 for i in range(n): # loop n times to simulate n games result = simOneGame(Pa,Pb) # simulate one game, get the result #print("i=",i) if result == 1: # Player A won WinsA = WinsA+1 else: # Player B won WinsB = WinsB+1 return WinsA, WinsB def simOneGame(Pa,Pb): """ simulation of one game takes Pa (probability of Player A to win at his/her serve), and Pb (probability of Player B to win at his/her serve); returns 1 if Player A wins, and 0 is Player B wins """ #print("entering simOneGame...") from random import random serving = 'A' # Assuming that Player A serves at the beginning of each game scoreA, scoreB = 0, 0 # initializing scores to 0 while scoreA < 15 and scoreB < 15: # the game is played until a player reaches the score of 15 if serving == 'A': # A is serving if random() < Pa: # A wins the serve scoreA = scoreA+1 else: # A loses the serve serving = 'B' # change the serve else: # B is serving if random() < Pb: # B wins the serve scoreB = scoreB+1 else: # B loses the serve serving = 'A' # change the serve #print("scoreA={0:0d}, scoreB={1:0d}".format(scoreA,scoreB)) if scoreA == 15: return 1 else: return 0 main()