一.简介

可以选择任意规则,模拟不同的两个队伍进行球赛的模拟比赛

二.源代码

函数介绍:

from random import *

#输出介绍信息
def printIntro():
print("这个程序模拟两个选手A和B的某种竞技比赛")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)") #输入,获取能力值和比赛场数
def getInputs():
#获取数据 a = 0.45
b = 0.5
n = 5
'''
a = eval(input("请输入a选手的能力值:"))
b = eval(input("请输入b选手的能力值:"))
n = eval(input("模拟比赛场次:"))
'''
return a,b,n #比赛全过程
def simNGames(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
return winsA, winsB #单场比赛过程,返回比分
def simOneGame(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) return scoreA, scoreB #输出获胜场数和比例
def printSummary(winsA, winsB):
n = winsA + winsB
print("选手A获胜{}场比赛,占比例{:0.2%}".format(winsA, winsA/n))
print("选手B获胜{}场比赛,占比例{:0.2%}".format(winsB, winsB/n)) #排球比赛单局
def Volleyball_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()<probA:
scoreA += 1
else:
serving='B'
else:
if random()<probB:
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()<probA:
scoreA += 1
else:
serving='B'
else:
if random()<probB:
scoreB += 1
else:
serving='A'
return scoreA, scoreB #足球比赛一场
def Football_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 in range(int(a)):
if random() - 0.1 > probA:
scoreA += 1
for i in range(int(b)):
if random() - 0.1 > probB:
scoreB += 1 while scoreA == scoreB : #点球大战
if random() > probA:
scoreA += 1
if random() > scoreB :
scoreB += 1
return scoreA, scoreB #乒乓球比赛单局
def Table_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() < probA:
scoreA += 1
else:
serving = 'B'
else:
if random() < probB:
scoreB += 1
else:
serving = 'A'
return scoreA, scoreB #篮球比赛一场
def Basketball_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 in range(int(a)):
if random() > probA:
if random() > 0.62:
scoreA += 3
else:
scoreA += 2
for i in range(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 in range(int(a)):
if random() > probA:
if random() > 0.62:
scoreA += 3
else:
scoreA += 2
for i in range(int(b)):
if random() > probB:
if random() > 0.62:
scoreB += 3
else:
scoreB += 2
return scoreA, scoreB def main():
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB) k = 0
main()

三.效果

2019-05-12 Python之模拟体育竞赛的更多相关文章

  1. python初体验 ——>>> 模拟体育竞技

    python初体验 ——>>> 模拟体育竞技 一.排球训练营 1. 简介: 模拟不同的两个队伍进行排球的模拟比赛. 2. 模拟原理: 通过输入各自的能力值(Ⅰ),模拟比赛的进行( P ...

  2. 2019.8.3 [HZOI]NOIP模拟测试12 C. 分组

    2019.8.3 [HZOI]NOIP模拟测试12 C. 分组 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 刚看这题觉得很难,于是数据点分治 k只有1和2两种,分别 ...

  3. 2019.8.3 [HZOI]NOIP模拟测试12 B. 数颜色

    2019.8.3 [HZOI]NOIP模拟测试12 B. 数颜色 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 数据结构学傻的做法: 对每种颜色开动态开点线段树直接维 ...

  4. 2019.8.3 [HZOI]NOIP模拟测试12 A. 斐波那契(fibonacci)

    2019.8.3 [HZOI]NOIP模拟测试12 A. 斐波那契(fibonacci) 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 找规律 找两个节点的lca,需 ...

  5. 7.搭建hyperledger fabric环境及启动——2019年12月12日

    2019年12月12日13:05:16 声明:从网络中学习整理实践而来. 1.介绍fabric Fabric 是一个面向企业应用的区块链框架,基于 Fabric 的开发可以粗略分为几个层面: 1. 参 ...

  6. AHKManager.ahk AHK管理器 2019年12月15日

    AHKManager.ahk  AHK管理器  2019年12月15日 快捷键   {Alt} + {F1} ///////////////////////////////////////////// ...

  7. 用python实现模拟登录人人网

    用python实现模拟登录人人网 字数4068 阅读1762 评论19 喜欢46 我决定从头说起.懂的人可以快速略过前面理论看最后几张图. web基础知识 从OSI参考模型(从低到高:物理层,数据链路 ...

  8. python selenium5 模拟点击+拖动+按照指定相对坐标拖动 58同城验证码

    #!/usr/bin/python # -*- coding: UTF-8 -*- # @Time : 2019年12月9日11:41:08 # @Author : shenghao/10347899 ...

  9. 36.React基础介绍——2019年12月24日

    2019年12月24日16:47:12 2019年10月25日11:24:29 主要介绍react入门知识. 1.jsx语法介绍 1.1 介绍 jsx语法是一种类似于html标签的语法,它的作用相当于 ...

随机推荐

  1. 【转】Java包管理器Maven学习

    Maven 引入Jar包的几种情况 1.通过pom.xml中导入的jar包 (1)链接打开http://mvnrepository.com/ (2)搜索罐包,比如我想要的是servlet的api.ja ...

  2. Android课程设计——博学谷1.0

    本文讲述了如何应用大三下学期智能移动终端开发技术课程所学知识,完成包含服务器端.客户端程序的应用——博学谷登录模块的开发,结合java语言基本知识,例如:字符串.列表.类.数据库读写等,设计.实现一个 ...

  3. 201771010103 陈亚茹 《面向对象程序设计(java)》第一周学习总结

    本人学号<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.com https://w ...

  4. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测——线段树上bfs求可撤销并查集

    题面 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 12030 Solved: 6024 Desc ...

  5. linux golden-dict个性化添加词典

    国内有道,百度等参考https://www.jianshu.com/p/9bf577335945如果和我一样,想要添加大名鼎鼎的韦氏词典英文词典,则地址如下https://www.merriam-we ...

  6. Python第五章-内置数据结构03-元组

    Python 内置的数据结构 三.元组(tuple) python 作为一个发展中的语言,也提供了其他的一些数据类型. tuple也是 python 中一个标准的序列类型. 他的一些操作和str和li ...

  7. 渗透测试之Web安全

    写在前面: 渗透测试包含但不限于Web安全 渗透测试并不相当于Web渗透 Web安全学习是入门渗透测试最容易的途径,门槛最低 Web安全入门: 基础入门 整体框架 SQL注入 XSS攻击 业务逻辑漏洞 ...

  8. Unable to locate JAR/zip in file system as specified by the driver definitio

    把之前的驱动包删掉,然后把你的驱动包导入就行了 现在OK键就算正常了

  9. flask中的表单文件上传

    在表单中上传文件时,一定要加入 enctype="multipart/form-data" 属性 为什么要加入这个属性呢: enctype就是encodetype就是编码类型的意思 ...

  10. MATLAB 大数相乘溢出显示

    解一道面试题——华为社招现场面试1:请使用代码计算1234567891011121314151617181920*2019181716151413121110987654321 . 乘积是逐位相乘,也 ...