一.简介

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

二.源代码

函数介绍:

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. android studio 添加GSON

  2. 初识socket之TCP协议

    TCP服务端.客户端(基础版本) # 这是服务端import socket ​server = socket.socket()  # 买手机server.bind(('127.0.0.1', 8080 ...

  3. Java井字棋游戏

    试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议. 一.棋盘类 package 井字棋游戏; public class ChessBoard { private int number; Perso ...

  4. 我们是怎么实现Grpc CodeFirst

    前言: Grpc默认是ProtoFirst的,即先写 proto文件,再生成代码,需要人工维护proto,生成的代码也不友好,所以出现了Grpc CodeFirst,下面来说说我们是怎么实现Grpc ...

  5. 《自拍教程51》Python_adb批量生成App版本表格

    案例一:版本在软件研发阶段是很重要的, 不同的版本, 已修复的Bug也不一样, 所实现的功能不一样, Android终端产品正式版本发布前,项目经理除了确保系统版本确定无误外, 还会逐个验证所搭载的所 ...

  6. 使用node.js中遇到的一些小bug

    1.BUG Cannot set headers after they are sent to the client 解决:即发出一次请求得到两次或以上的回应时会出现此警告,此时注意查看再在一些条件下 ...

  7. Windows系统向Ubuntu传输文件

    PuTTY传输: 安装PuTTY,然后将PuTTY安装目录下的pscp.exe文件拷贝到/Windows/System32/目录下,在cmd控制台执行命令: # pscp 要传输的文件路径 ubunt ...

  8. docker 本地镜像导入导出 compose安装

    docker 本地镜像导入导出 1.Docker导入本地gz镜像 [root@rocketmq-nameserver4 dev]# cat alibaba-rocketmq-3.2.6.tar.gz ...

  9. 《java编程思想》对象导论

    1.抽象过程 所有编程语言都提供抽象机制.可以认为,人们所能够解决的问题的复杂性直接取决于抽象的类型和质量,所谓的'类型'是指“所抽象的是什么?”汇编语言是对底层机器的轻微抽象. java的基本 特性 ...

  10. 尴尬,通篇使用 if

    以给博客园头部导航条链接添加图标为例, 接下来看看如何分别使用对象.数组.Map 优化它的. 前置 1.接下来给头部导航条添的图标包含: 博客园首页 新随笔 博客首页 联系 订阅 管理 2.这里封装了 ...