Forecast table tennis competition results

We downwards, from simple to difficult way step by step through the decomposition of a top-code design competition

The following are designed by analyzing the code pong game:

Random Random Import from
DEF printIntro ():
Print ( "This program simulates two table tennis players A and B")
Print ( "procedures required value (between 0 and capabilities. 1 A and B)")
DEF getInputs ( ):
a = the eval (iNPUT ( "Please enter the ability value (0-1) of the player a:"))
B = the eval (iNPUT ( "Please enter the player's ability value B (0-1):"))
n-= eval (input ( "Event simulation game:"))
return a, B, n-
DEF simNGames (n-, Proba, probB):
winsA, winsB = 0,0
for I in Range (n-):
scoreA, scoreB simOneGame = ( Proba, probB)
IF scoreA> scoreB:
winsA + =. 1
the else:
winsB + =. 1
return winsA, winsB
DEF gameOver (A, B):
return A ==. 11 or B ==. 11
DEF simOneGame (Proba, probB):
scoreA, scoreB 0,0 =
Serving = 'A'
the while Not gameOver (scoreA, scoreB):
IF Serving == 'A':
IF Random () <Proba:
scoreA + =. 1
the else:
Serving = 'B'
the else:
IF Random () <probB:
scoreB + =. 1
the else:
Serving = 'A'
return scoreA, scoreB
DEF printSummary (winsA, winsB):
n-+ = winsA winsB
Print ( "start Competitive analysis, total simulation games {}" .format (n-))
Print ( "{} A player wins the game, the proportion of {:} .1% ".format (winsA, winsA / n-))
Print (" B player wins games {}, accounted {:} .1% "the format (winsB, winsB / n-.))
DEF main ():
printIntro ()
Proba , probB, n-getInputs = ()
winsA, winsB simNGames = (n-, Proba, probB)
printSummary (winsA, winsB)
main ()

Try ability value gap more subtle case

Qiju four wins singles competition system when the system table tennis, doubles is five games system, so let's use the following code to predict the situation singles:

from random import random
def printIntro():
print("这个程序模拟两个选手A和B的乒乓球比赛")
print("程序运行需要A和B的能力值(0到1之间)")
def getInputs():
a=eval(input("请输入选手A的能力值(0-1):"))
b=eval(input("请输入选手B的能力值(0-1):"))
return a,b
def simNGames(probA,probB):
winsA,winsB=0,0
for i in range(7):
scoreA,scoreB=simOneGame(probA,probB)
if scoreA>scoreB:
winsA+=1
if winsA==4:
break
else:
winsB+=1
if winsB==4:
break
return winsA,winsB
def gameOver(a,b):
return a==11 or b==11
def simOneGame(probA,probB):
scoreA,scoreB = 0,0
serving = 'A'
while not gameOver(scoreA,scoreB):
if serving == 'A':
if random()<probA:
scoreA+=1
else:
serving='B'
else:
if random()<probB:
scoreB+=1
else:
serving='A'
return scoreA,scoreB
def printSummary(winsA,winsB):
n=winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n))
print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n))
def main():
printIntro()
probA,probB = getInputs()
winsA,winsB = simNGames(probA,probB)
printSummary(winsA,winsB)
main()

试着运行一下

接下来是双打时,我们将刚刚的A/B选手抽象为A/B队伍,能力值为队伍的平均能力值

from random import random
def printIntro():
print("这个程序模拟两个队伍A和B的乒乓球比赛")
print("程序运行需要A和B的能力值(0到1之间)")
def getInputs():
a=eval(input("请输入选手A的能力值(0-1):"))
b=eval(input("请输入选手B的能力值(0-1):"))
return a,b
def simNGames(probA,probB):
winsA,winsB=0,0
for i in range(5):
scoreA,scoreB=simOneGame(probA,probB)
if scoreA>scoreB:
winsA+=1
if winsA==3:
break
else:
winsB+=1
if winsB==3:
break
return winsA,winsB
def gameOver(a,b):
return a==11 or b==11
def simOneGame(probA,probB):
scoreA,scoreB = 0,0
serving = 'A'
while not gameOver(scoreA,scoreB):
if serving == 'A':
if random()<probA:
scoreA+=1
else:
serving='B'
else:
if random()<probB:
scoreB+=1
else:
serving='A'
return scoreA,scoreB
def printSummary(winsA,winsB):
n=winsA+winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:.1%}".format(winsA,winsA/n))
print("选手B获胜{}场比赛,占比{:.1%}".format(winsB,winsB/n))
def main():
printIntro()
probA,probB = getInputs()
winsA,winsB = simNGames(probA,probB)
printSummary(winsA,winsB)
main()

运行

 

Guess you like

Origin www.cnblogs.com/komei/p/10929816.html