0

Hi all,

I've been working on a program that simulates a racquetball match. The problem I am having is getting the match to end when one player is the clear winner of 'n' games. I've tried adding modules, altering loops, adding variables, etc.

Is anyone able to help by pointing me in the right direction? I've spent many days and sleepless nights on this and have just hit a brick wall. I don't want it to get the better of me.

:-|

Here is my code so far:

```
# rballmatch.py
# Simulation of a racquetball game. .
from random import random
def printIntro():
# Prints an introduction to the program
print "This program simulates a game of racquetball between two"
print 'players called "A" and "B". The abilities of each player is'
print "indicated by a probability (a number between 0 and 1) that"
print "the player wins the point when serving. Player A always"
print "has the first serve.\n"
def getInputs():
# Returns probA, probB, number of games to simulate
a = input("What is the prob. player A wins a serve? ")
b = input("What is the prob. player B wins a serve? ")
n = input("How many games to simulate? ")
return a, b, n
def simNGames(n, probA, probB):
# Simulates n games of racquetball between players whose
# abilities are represented by the probability of winning a serve.
# Returns number of wins for A and B
winsA = winsB = 0
if (winsA) or (winsB) = ((n / 2) + 1:
break
else:
for i in range(n):
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA = winsA + 1
else:
winsB = winsB + 1
return winsA, winsB
def simOneGame(probA, probB):
# Simulates a single game of racquetball between two players whose
# abilities are represented by the probability of winning a serve.
# Returns final scores for A and B
serving = "A"
scoreA = 0
scoreB = 0
while not gameOver(scoreA, scoreB):
if serving == "A":
if random() < probA:
scoreA = scoreA + 1
else:
serving = "B"
else:
if random() < probB:
scoreB = scoreB + 1
else:
serving = "A"
return scoreA, scoreB
def gameOver(a,b):
# a and b are scores for players in a racquetball game
# Returns true if game is over, false otherwise
return a == 15 or b == 15 and ((a - b) > 2 ) or ((a - b) < -2)
def printSummary(winsA, winsB):
# Prints a summary of wins for each player.
n = winsA + winsB
print "\nGames simulated:", n
print "Wins for A: %d (%0.1f%%)" % (winsA, float(winsA)/n*100)
print "Wins for B: %d (%0.1f%%)" % (winsB, float(winsB)/n*100)
def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
if __name__ == "__main__": main()
```