For my final project our group is suppose to simulate a game of table tennis this is what we have so far at the bottom. We followed the raquetball game setup for the code but we werent sure how to change this so that when a player scores it doesnt have to be if they serve to get a point, anyone have suggestions?
# tabletennis.py # ">Simulation of a Table Tennis game. . from random import random def printIntro(): # Prints an introduction to the program print "This program simulates a game of Table tennis between two" print 'players called "A" and "C". def getInputs(): # Returns probA, probC, 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, c, n def simNGames(n, probA, probC): # Simulates n games of Table Tennis 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 Table Tennis 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,c): # a and c are scores for players in a Table Tennix game # Returns true if game is over, false otherwise return a == 11 or b == 11 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()
Edited 7 Years Ago by aleezy: n/a