Table tennis simulator

I edited my previous question because I came up with code that I think is correct. The logic of this should be: while the set is not over yet, and this is not a tie 10:10: player A starts to speak and does it twice, regardless of whether he wins points or not, then player B receives the serve and does it twice . It continues until the dialing is completed, with the exception that there is a 10:10 connection, when the servers change every clogged point.

Can someone check if the code is flawless? thanks.

def simOneSet(probA, probB): serving = "A" scoreA = scoreB = 0 while not setOver(scoreA, scoreB): if scoreA != 10 and scoreB != 10: if serving == "A": for i in range(2): if random() < probA: scoreA += 1 else: scoreB += 1 serving = "B" else: for i in range(2): if random() < probB: scoreB +=1 else: scoreA += 1 serving = "A" # when there is a tie 10:10 else: if serving == "A": if random() < probA: scoreA += 1 serving = "B" else: scoreB += 1 serving = "B" else: if random() < probB: scoreB += 1 serving = "B" else: scoreA += 1 serving = "A" return scoreA, scoreB 
+4
source share
4 answers

I would use a voice recorder to "switch" between players:

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

Then, if serving is equal to 'A' , then other[serving] will be equal to 'B' , and if serving is equal to 'B' , then other[serving] will be equal to 'A' .


You can also use collections.Counter to track scores:

 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 code snippet

  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 that basically repeat the same idea twice. This is a sign that the code can be tightened using the function. For example, we could define a serve function that, when prob and the 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 will become

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

This way you can simplify your code a bit like this:

 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)) 
+3
source

Here is a hint:

If you have a round number in the set and know which player started working, you have everything you need to know who serves.

Then a simple if statement at the beginning or end of the loop is enough.

If this is too complicated, try starting with a game simulation where the server starts every round.

0
source

Something that might help is the syntax

 var = 1 if var == 2 else 2 

Which will make var equal to 1 if var is 2, and var be 2 if var is 1. I feel like this is a school problem, so I don't want to give the answer completely :)

Hint: you are on the right track with your thinking.

0
source

from arbitrary import *

 P1=P2=0 while 1 : p1=p2=0 while 2 : if random() < 0.5 : p1 +=1 else : p2 +=1 if(p1 >=11 or p2 >=11) and abs(p1-p2) > 1: break P1 += p1 > p2; P2 += p2 > p1 print "%2d : %2d (%d : %d)" % (p1, p2, P1, P2) if P1 == 4 or P2 == 4 : break 

Hope this helps, it worked for me.

0
source

All Articles