Table Tennis Simulator google

Table Tennis simulator

Other = {'A':'B', 'B':'A'}

Then, if serving equals 'A', then other[serving] would equal 'B', and if serving equals 'B', then other[serving] would equal 'A'.

In [1]: import collections In [2]: score = collections.Counter In [3]: score['A'] += 1 In [4]: score['A'] += 1 In [5]: score['B'] += 1 In [6]: score Out[6]: Counter({'A': 2, 'B': 1})

Also notice how in this piece of code

if serving == "A": for i in range(2): if random < probA: scoreA += 1 else: scoreB += 1 else: for i in range(2): if random < probB: scoreB +=1 else: scoreA += 1

there are two blocks which are basically the same idea repeated twice. That's a sign that the code can be tightened-up by using a function. For example, we could define a function serve which when given a probability prob and a player (A or B) returns the player who wins:

def serve(prob, player): if random.random < prob: return player else: return other[player]

then the above code would become

for i in range(2): winner = serve(prob[serving], serving) score[winner] += 1

Thus, you can compactify your code quite a bit this way:

import random import collections other = {'A':'B', 'B':'A'} def serve(prob, player): if random.random < prob: return player else: return other[player] def simOneSet(probA, probB): prob = {'A':probA, 'B':probB} score = collections.Counter serving = "A" while not setOver(score['A'], score['B']): for i in range(2): winner = serve(prob[serving], serving) score[winner] += 1 if score['A'] == 10 and score['B'] == 10: winner = serve(prob[serving], serving) score[winner] += 1 serving = winner return score['A'], score['B'] def setOver(scoreA, scoreB): return max(scoreA, scoreB) >= 21 print(simOneSet(0.5, 0.5))

Source: stackoverflow.com
RELATED VIDEO
Tennis Table Simulation [HD]
Tennis Table Simulation [HD]
Table Tennis OpenGL Physics Simulation [ICT215 Promo Video]
Table Tennis OpenGL Physics Simulation [ICT215 Promo Video]
2008 Pre-Olympic Simulation Game - Table Tennis
2008 Pre-Olympic Simulation Game - Table Tennis
RELATED FACTS
Share this Post

Related posts

Table Tennis Ireland

Table Tennis Ireland

APRIL 22, 2018

Here you will find a directory of all the Table Tennis Clubs in Connaught, Leinster, Munster and Ulster. If your club is…

Read More
Table Tennis strategy

Table Tennis strategy

APRIL 22, 2018

SfC Home > Competition > Sports > In table tennis, the person serving the ball usually has a slight…

Read More