# 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 answer = 'yes' while answer[0]=='y' or answer[0]=='Y': Pa, Pb, n = getInput() # getting input from the user print "The Player's A ability: %0.2f" % Pa print "The Player's B ability: %0.2f" % Pb print "The number of games to be simulated: %d" % n # simulating n games, returning the result of simulation WinsA, WinsB = simNgames(n,Pa,Pb) report(n,WinsA,WinsB) # reporting back to the user answer = raw_input("Another run? ") print "Have a good day!" def Intro(): print 'Hello, this is a simulation of the racquetball game' print 'This is a game for two players: Player A and Player B' def getInput(): ProbA = input('Please, input the probability that\n player A wins on his/her serve (i.e. Ability-level):') ProbB = input('Please, input the probability that\n player B wins on his/her serve (i.e. Ability-level):') n = input('Input the number of games you\'d like to simulate:') return ProbA, ProbB, n def report(n,wA,wB): print '%d games are simulated' % n PercentA = wA*100.0/n print 'Wins for Player A: %d (%0.1f%%)' %(wA,PercentA) PercentB = wB*100.0/n print 'Wins for Player B: %d (%0.1f%%)' %(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 result=simOneGame(Pa,Pb) # simulate one game, get the result print "i=%d" %i if result==1: # Player A won WinsA=WinsA+1 else: # Player B won WinsB=WinsB+1 return WinsA, WinsB def simOneGame(Pa,Pb): print "entering simOneGame..." from random import random serving='A' scoreA=0 scoreB=0 while scoreA<15 and scoreB<15: if serving == 'A': # A is serving if random() < Pa: # A wins the serve scoreA=scoreA+1 else: # A loses the serve serving='B' else: # B is serving if random() < Pb: # B wins the serve scoreB=scoreB+1 else: # B loses the serve serving='A' print "scoreA=%d, scoreB=%d" % (scoreA,scoreB) if scoreA == 15: print "Player A won this game" return 1 elif scoreB == 15: print "Player B won this game" return 0 else: print 'Something went wrong' print 'None of the players collected 15 points, but the rally is over' main()