0

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 by aleezy: n/a

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by masterofpuppets
0

hi,
that's a nice project :)

just a simple comment of one of the functions though. In the gameOver function you're using a and c as parameters but inside of the function you're using a and b so it's probably gonna raise an exception that b is not defined. Better change it to c :) like this

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) becomes
    return a == 1 or c == 11 and ( ( a - c ) > 2 ) or ( ( a - c ) < -1 )

an alternative would be just to change the parameter from c to b :)

as for your question as I recall a serve changes every 5 times right ? So what I would do is to have a counter in the simOneGame function that counts the number of serves. When the counter becomes 5 change the serving player. Here's what I mean:

def changeServing( s ):
    if s == "A":
        return "B"
    else:
        return "A"

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"
    serveCount = 0
    scoreA = 0
    scoreB = 0
    while not gameOver(scoreA, scoreB):
        if servingCount == 5:
            servingCount = 0
            serving = changeServing( serving )
        if serving == "A":
            if random() < probA:
                scoreA = scoreA + 1
            else:
                serving = "B"
        else:
            if random() < probB:
                scoreB = scoreB + 1
            else:
                serving = "A"
        servingCount += 1

    return scoreA, scoreB

Of course this only works if you represent the serving with 'A' and 'B' otherwise you'll need to configure the changeServing function

hope this gives you some ideas :)

Edited by masterofpuppets: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.