码迷,mamicode.com
首页 > 其他好文 > 详细

乒乓球比赛模拟

时间:2020-04-20 23:50:17      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:rand   turn   game   eva   tin   get   port   false   mes   

 1 #羽毛球比赛规则
 2 from random import random
 3 def getInputs():
 4     a = eval(input("请输入选手A的能力值(0-1): "))
 5     b = eval(input("请输入选手B的能力值(0-1): "))
 6     n = 3
 7     return a, b, n
 8 def simNGames(n, probA, probB):
 9     winsA, winsB = 0, 0
10     for i in range(n):
11         scoreA, scoreB = simOneGame(probA, probB)
12         if scoreA > scoreB:
13             winsA += 1
14         else:
15             winsB += 1
16     return winsA, winsB
17 def gameOver(a,b):
18     flag = True
19     if (a==21 and b<20) or (b==21 and a<20):
20         flag = False
21     elif (a>21 and b>21):
22         if (a==29 and b==30) or (a==30 and b==29):
23             flag = False
24         elif abs(a-b)==2:
25             flag = False
26     elif(a==21 or b==21):
27         flag = False
28     return flag
29 def simOneGame(probA, probB):
30     scoreA, scoreB = 0, 0
31     serving = "A"
32     while gameOver(scoreA, scoreB):
33         if serving == "A":
34             if random() < probA:
35                 scoreA += 1
36             else:
37                 serving="B"
38         else:
39             if random() < probB:
40                 scoreB += 1
41             else:
42                 serving="A"
43     return scoreA, scoreB
44 def printSummary(winsA, winsB):
45     if winsA>winsB:
46         print("选手A赢{}场,选手B赢{}场,选手A获胜".format(winsA,winsB))
47     else:
48         print("选手A赢{}场,选手B赢{}场,选手B获胜".format(winsA,winsB))
49 def main():
50     probA, probB, n = getInputs()
51     winsA, winsB = simNGames(n, probA, probB)
52     printSummary(winsA, winsB)
53 main()

 

乒乓球比赛模拟

标签:rand   turn   game   eva   tin   get   port   false   mes   

原文地址:https://www.cnblogs.com/1234f/p/12741370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!