一.简介

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

二.源代码

函数介绍:

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. Arch Linux安装配置-双系统(1)

    Arch Linux启动盘准备: 在Windows下安装Win32 Disk Imager,打开页面,点击Download即可! 安装配置 1.选择我同意 2.选择安装位置路径 3.打勾,在桌面显示图 ...

  2. 目标检测 | RetinaNet:Focal Loss for Dense Object Detection

    论文分析了one-stage网络训练存在的类别不平衡问题,提出能根据loss大小自动调节权重的focal loss,使得模型的训练更专注于困难样本.同时,基于FPN设计了RetinaNet,在精度和速 ...

  3. git 使用和一些错误

    一.简单使用 Git是目前世界上最先进的分布式版本控制系统,用于自动记录每次文件的改动,但是和所有版本控制系统一样,只能跟踪文本文件的改动,比如TXT文件,网页,所有的程序代码等,而图片.视频这些二进 ...

  4. Angular2入门(一)

    原先用vue.js写的项目,最近领导要求改用Angular,于是开始自学之路.网上搜索了众多资料,包括谷歌原版书籍,但是Angular自从17年开始分为AngularJs和Angular两个版本,相差 ...

  5. 多线程之旅(Thread)

    在上篇文章中我们已经知道了多线程是什么了,那么它到底可以干嘛呢?这里特别声明一个前面的委托没看的同学可以到上上上篇博文查看,因为多线程要经常使用到委托.源码 一.异步.同步 1.同步(在计算的理解总是 ...

  6. 李宏毅老师机器学习课程笔记_ML Lecture 0-1: Introduction of Machine Learning

    引言: 最近开始学习"机器学习",早就听说祖国宝岛的李宏毅老师的大名,一直没有时间看他的系列课程.今天听了一课,感觉非常棒,通俗易懂,而又能够抓住重点,中间还能加上一些很有趣的例子 ...

  7. effective-java学习笔记---优先使用泛型方法30

    泛型类型比需要在客户端代码中强制转换的类型更安全,更易于使用. 当你设计新的类型时,确保它们可以在没有这种强制转换的情况下使用. 这通常意味着使类型泛型化. 如果你有任何现有的类型,应该是泛型的但实际 ...

  8. 详解Redis持久化(RDB和AOF)

    详解Redis持久化(RDB和AOF) 什么是Redis持久化? Redis读写速度快.性能优越是因为它将所有数据存在了内存中,然而,当Redis进程退出或重启后,所有数据就会丢失.所以我们希望Red ...

  9. 本地缓存Ehcache

    1,什么是Ehcache    Ehcache是纯java的开源缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.它主要面向通用缓存.Java EE和轻量级容器, ...

  10. 阿里云域名+ 腾讯云服务器 配置nginx

    1,实现目标,通过外网访问域名,能够通过nginx 实现反向代理,以及负载均衡 2,准备工具 阿里云注册的域名: aiyuesheng.com 腾讯云领取的云服务器:centos 7 xshell 6 ...