一.简介

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

二.源代码

函数介绍:

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. 大数据安装之Kafka(用于实时处理的消息队列)

    一.安装部署kafka 1.集群规划 hadoop102                                 hadoop103                          hado ...

  2. VScode 快捷键大全

    按 Press 功能 Function Ctrl + Shift + P,F1 显示命令面板 Show Command Palette Ctrl + P 快速打开 Quick Open Ctrl + ...

  3. hdu1026Ignatius and the Princess ,又要逃离迷宫了

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1026/ 题意就是一个迷宫,然后有些位置上有守卫,守卫有一个血量,要多花费血量的时间击败他,要求从(0,0)到(n ...

  4. C 2013笔试题

    1.把整数分解成素数 如90=2*3*3*5 [见2015年] 方法一: int main() { int n, i=2; printf("\nInput:"); scanf(&q ...

  5. Redis 按正则获取keys

    首先,我被坑了很久由于不知道这个redis支持的正则只有3种 1. * 任意长度的任意字符 2. ? 任意单一字符 3. [xxx] 匹配方括号中的一个字符 2.从上面开来,keys的模糊匹配功能很方 ...

  6. springboot整合dubbo+zookeeper最新详细

    引入 最近和小伙伴做一个比赛,处于开发阶段,因为涉及的服务比较多,且服务需要分开部署在不同的服务器上,讨论之后,打算采用分布式来做,之前学习springboot的时候,部分章节涉及到了springbo ...

  7. effective-java学习笔记---使用接口模拟可扩展的枚举38

    枚举类型( BasicOperation )不可扩展,但接口类型( Operation )是可以扩展的,并且它是用于表示 API 中的操作的接口类型. // Emulated extensible e ...

  8. tensorflow 控制流操作,条件判断和循环操作

    Control flow operations: conditionals and loops When building complex models such as recurrent neura ...

  9. 使用TensorFlow v2.0构建卷积神经网络

    使用TensorFlow v2.0构建卷积神经网络. 这个例子使用低级方法来更好地理解构建卷积神经网络和训练过程背后的所有机制. CNN 概述 MNIST 数据集概述 此示例使用手写数字的MNIST数 ...

  10. 纯css实现图片或者页面变灰色

    前言 今天是个沉痛的日子,全国哀悼英雄,各大平台平日鲜丽的界面置纷纷换成了灰色,以表对逝者的安息与尊敬!!对付疫病,我没多大的本事,只能记录一点点知识来提升自己擅长的技术,待到将来能为国家尽一份绵薄之 ...