模拟体育竞技分析

一、下面是一个体育竞技模拟,就是输入比赛队伍的平均能力值,就可以判断出比赛的输赢。

下面来看一段代码(以兵乓球比赛规则为例)

 1 # -*- coding: utf-8 -*-
 2 from random import random
 3 def printIntro():
 4     print("这个程序模拟两个选手A和B的兵乓球比赛")
 5     print("程序运行需要A和B选手的能力值(以0到1之间的小数表示)")
 6 def getInput():
 7     probA=eval(input("请输入选手A的能力值(0-1):"))
 8     probB=eval(input("请输入选手B的能力值(0-1):"))
 9     n=eval(input("模拟比赛场次:"))
10     return probA,probB,n
11 def simGames(n,probA,probB):     #比赛双方各自胜利的场次数
12     winsA,winsB=0,0
13     for i in range (n):
14         c=simGame(probA,probB)
15         if c=='A':
16             winsA+=1
17         else:
18             winsB+=1
19     return winsA,winsB
20 def simGame(probA,probB):       #判断一场比赛的胜方
21     winsA1,winsB1=0,0
22     for i in range(7):
23         scoreA,scoreB=simoneGame(probA,probB)
24         if scoreA>scoreB:
25             winsA1+=1
26         else:
27             winsB1+=1      
28     if winsA1>=4:
29         return 'A'
30     return 'B'
31 def simoneGame(probA,probB):     #统计一场比赛下来的各自得分数
32     scoreA,scoreB=0,0
33     serving="A"
34     while not gameOver(scoreA,scoreB):
35         x=random()
36         if serving=="A":
37             if x < probA:
38                 scoreA+=1
39             else:
40                 serving="B"
41         else:
42             if x < probB:
43                 scoreB+=1
44             else:
45                 serving="A"
46     return scoreA,scoreB
47 def gameOver(a,b):
48     if (a>=11 and b<11) or (b>=11 and a<11) or (a>9 and b>9 and ((a-b)>=2 or (b-a)>=2)):
49         return True
50     return False
51 def printsummary(winsA,winsB):
52     n=winsA+winsB
53     print("竞技开始分析,共模拟了{}场比赛".format(n))
54     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
55     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
56     print("分析人:Yong No.10")
57 def main():
58     printIntro()
59     probA,probB,n=getInput()
60     winsA,winsB=simGames(n,probA,probB)
61     printsummary(winsA,winsB)
62 main()    

可以观看其效果:

二、下面以篮球的比赛规则为例

以下代码展示

 1 # -*- coding: utf-8 -*-
 2 from random import random
 3 def printIntro():
 4     print("这个程序模拟两个选手A和B的兵乓球比赛")
 5     print("程序运行需要A和B选手的能力值(以0到1之间的小数表示)")
 6 def getInput():
 7     probA=eval(input("请输入选手A的能力值(0-1):"))
 8     probB=eval(input("请输入选手B的能力值(0-1):"))
 9     n=eval(input("模拟比赛场次:"))
10     return probA,probB,n
11 def simGames(n,probA,probB):             #每一场比赛的各胜方的场数量
12     winsA,winsB=0,0
13     for i in range (n):
14         c=simGame(probA,probB)
15         if c=='A':
16             winsA+=1
17         else:
18             winsB+=1
19     return winsA,winsB
20 def simGame(probA,probB):              #每局比赛的胜方判断
21     winsA1,winsB1=0,0
22     for i in range(7):
23         scoreA,scoreB=simoneGame(probA,probB)
24         if scoreA>scoreB:
25             winsA1+=1
26         else:
27             winsB1+=1      
28     if winsA1>=4:
29         return 'A'
30     return 'B'
31 def simoneGame(probA,probB):          #每场比赛各方的得分统计
32     scoreA,scoreB=0,0
33     serving="A"
34     while not gameOver(scoreA,scoreB):
35         x=random()                    #产生的随机数与各方的概率比较
36         if serving=="A":
37             if x < probA:
38                 scoreA+=1
39             else:
40                 serving="B"
41         else:
42             if x < probB:
43                 scoreB+=1
44             else:
45                 serving="A"
46     return scoreA,scoreB
47 def gameOver(a,b):                #游戏结束的条件
48     if (a>=11 and b<11) or (b>=11 and a<11) or (a>9 and b>9 and ((a-b)>=2 or (b-a)>=2)):
49         return True
50     return False
51 def printsummary(winsA,winsB):
52     n=winsA+winsB
53     print("竞技开始分析,共模拟了{}场比赛".format(n))
54     print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA,winsA/n))
55     print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB,winsB/n))
56     print("分析人:Yong No.10")
57 def main():
58     printIntro()
59     probA,probB,n=getInput()
60     winsA,winsB=simGames(n,probA,probB)
61     printsummary(winsA,winsB)
62 main()    

再来观看其效果:

三、下面更高级的是晋级比赛(以四支队伍为例进行篮球比赛)

以下代码展示

 1 # -*- coding:utf-8 -*-
 2 from random import random
 3 def printIntro():
 4     print("这个程序模拟四支队伍A,B,C,D的篮球比赛,赛制采取晋级制")
 5     print("程序运行需要A,B,C,D队伍的平均能力值(以0到1之间的小数表示)")
 6 def getInput():
 7     probA=eval(input("请输入队伍A的平均能力值(0-1):"))
 8     probB=eval(input("请输入另一支队伍B的平均能力值(0-1):"))
 9     print("\n")
10     return probA,probB
11 def fourGames(x,y):          #一场比赛的胜利方判断
12     scoreA,scoreB=0,0
13     for i in range(4):
14         scoreA,scoreB=onejieGame(x,y)
15     if scoreA>scoreB:
16         return 'A'
17     return 'B'
18 def onejieGame(x,y):         #一小节比赛的比分总结
19     scoreA,scoreB=0,0
20     serving="A"
21     while not gameOver(scoreA,scoreB):
22         a=random()
23         if serving=="A":
24             if a<x:
25                 scoreA+=2
26             else:
27                 serving="B"
28         else:
29             if a<y:
30                 scoreB+=2
31             else:
32                 serving="A"
33     return scoreA,scoreB
34 
35 def gameOver(a,b):
36     return a==16 or b==16
37 def printmean(c):
38     print("竞技开始:")
39     print("经过激烈的比赛,最终队伍{}获得了比赛".format(c))
40     print("恭喜{}队伍晋级!".format(c))
41     print("\n")
42 printIntro()
43 x,y=getInput()
44 c=fourGames(x,y)
45 printmean(c)
46 print("请再次输入其余两支队伍的平均能力值")
47 x,y=getInput()
48 d=fourGames(x,y)
49 printmean(d)
50 print("晋级双方是{}和{}".format(c,d))
51 print("\n")
52 print("请输入两支晋级队伍平均能力值")
53 x,y=getInput()
54 e=fourGames(x,y)
55 print("恭喜{}获得本次比赛的总冠军!".format(e))

观看效果:

 以上就是体育竞技的一些例子,有兴趣的童鞋可以去看看其他比赛的效果哈。

 最后说一点就是我们可以通过运用pyinstaller库来进行程序的打包

操作就是在cmd命令符里先安装好这个库(pip install pyinstaller)

然后在输入pyinstaller D:/+文件位置+程序名   即可!

猜你喜欢

转载自www.cnblogs.com/liyanyinng/p/10852790.html