python简单体育竞技模拟_2019-05-12 Python之模拟体育竞赛

from random import *

#输出介绍信息

defprintIntro():print("这个程序模拟两个选手A和B的某种竞技比赛")print("程序运行需要A和B的能力值(以0到1之间的小数表示)")#输入,获取能力值和比赛场数

defgetInputs():#获取数据

a= 0.45b= 0.5n= 5

'''a = eval(input("请输入a选手的能力值:"))

b = eval(input("请输入b选手的能力值:"))

n = eval(input("模拟比赛场次:"))'''

returna,b,n#比赛全过程

defsimNGames(n,probA,probB):print("竞技分析开始,共模拟{}场比赛".format(n))

winsA, winsB=0,0

m=eval(input("1.排球,2乒乓球,3足球,4篮球,请输入选择的规则:"))for i in range(1, int(n/2)+2): #n为比赛场数

scoreA, scoreB=simOneGame(int(n/2)+1, probA, probB, m) #单场比赛过程

print("第{}场比赛得分情况,A:{},B:{}".format(i, scoreA, scoreB))if scoreA >scoreB:

winsA+= 1

else:

winsB+= 1

returnwinsA, winsB#单场比赛过程,返回比分

defsimOneGame(i, probA, probB, m):global k #记录局数

k += 1

if m == 1:

scoreA, scoreB=Volleyball_game(k, i, probA, probB)elif m == 2:

scoreA, scoreB=Table_Tennis_game(k, i, probA, probB)elif m == 3:

scoreA, scoreB=Football_game(i, probA, probB)elif m == 4:

scoreA, scoreB=Basketball_game(i, probA, probB)returnscoreA, scoreB#输出获胜场数和比例

defprintSummary(winsA, winsB):

n= winsA +winsBprint("选手A获胜{}场比赛,占比例{:0.2%}".format(winsA, winsA/n))print("选手B获胜{}场比赛,占比例{:0.2%}".format(winsB, winsB/n))#排球比赛单局

defVolleyball_game(k, i, probA, probB):

scoreA, scoreB=0,0

serving= 'A'

if k!=i: #不是最终局

while not ((scoreA == 25 and scoreA >= scoreB-2) or (scoreB == 25 and scoreB >= scoreA-2)) : #如果未达到单场结束条件

if serving == 'A':if random()

scoreA+= 1

else:

serving='B'

else:if random()

scoreB+= 1

else:

serving='A'

else: #最终局

while not ((scoreA == 15 and scoreA >= scoreB-2) or (scoreB == 15 and scoreB >= scoreA-2)):if serving == 'A':if random()

scoreA+= 1

else:

serving='B'

else:if random()

scoreB+= 1

else:

serving='A'

returnscoreA, scoreB#足球比赛一场

defFootball_game(i, probA, probB):

scoreA, scoreB=0, 0

a, b= randint(int(probA*7), int(probA*14)), randint(int(probB*7), int(probB*14)) #根据能力值随机出进攻次数

for i inrange(int(a)):if random() - 0.1 >probA:

scoreA+= 1

for i inrange(int(b)):if random() - 0.1 >probB:

scoreB+= 1

while scoreA == scoreB : #点球大战

if random() >probA:

scoreA+= 1

if random() >scoreB :

scoreB+= 1

returnscoreA, scoreB#乒乓球比赛单局

defTable_Tennis_game(k, i, probA, probB):

scoreA, scoreB=0, 0

serving= 'A'

while not ((scoreA == 11 and scoreA >= scoreB-2) or (scoreB == 11 and scoreB >= scoreA-2)): #如果未达到单场结束条件

if serving == 'A':if random()

scoreA+= 1

else:

serving= 'B'

else:if random()

scoreB+= 1

else:

serving= 'A'

returnscoreA, scoreB#篮球比赛一场

defBasketball_game(k, i, probA, probB):

scoreA, scoreB=0, 0

a, b= randint(int(probA * 150), int(probA * 170)), randint(int(probB * 150), int(probB * 170)) #根据能力值随机出进攻次数

for i inrange(int(a)):if random() >probA:if random() > 0.62:

scoreA+= 3

else:

scoreA+= 2

for i inrange(int(b)):if random() >probB:if random() > 0.62:

scoreB+= 3

else:

scoreB+= 2

while scoreA == scoreB: #加时赛

a, b = randint(int(probA * 10), int(probA * 15)), randint(int(probB * 10), int(probB * 15)) #根据能力值随机出进攻次数

for i inrange(int(a)):if random() >probA:if random() > 0.62:

scoreA+= 3

else:

scoreA+= 2

for i inrange(int(b)):if random() >probB:if random() > 0.62:

scoreB+= 3

else:

scoreB+= 2

returnscoreA, scoreBdefmain():

printIntro()

probA, probB, n=getInputs()

winsA, winsB=simNGames(n, probA, probB)

printSummary(winsA, winsB)

k=0

main()

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值