预测球队比赛成绩

一. 模拟体育竞技分析

1.体育竞技分析程序

(1)模拟体育竞技程序,我们采用自顶向下的设计方法。自顶向下设计中最重要的是顶层设计。以体育体育竞技分析为例,可以从问题的IPO描述开始。大多数程序都可以讲IPO描述直接用到程序设计结构中。体育竞技分析从用户处得到模拟参数,最后呢输出结果。下面是一个基础设计的步骤。

步骤一:打印程序的介绍信息。

步骤二:获得程序运行的参数, 即probA,probB,n。

步骤三:利用球员的能力值。模拟n场比赛。

步骤四:输出球员的获胜比赛场次及概率。 

(2)自顶向下设计的基本思想,如下图:

2. 体育竞技分析的IPO模式:

输入I(input):两个球员的能力值,模拟比赛的次数(其中,运动员的能力值,可以通过发球方赢得本回合的概率来表示,

            一个能力值为0.8的球员,在他发球时,有80%的可能性赢得1分)

处理P(process):模拟比赛过程

输出O(output):两个球员获胜的概率

二.模拟乒乓球竞赛

1.乒乓球比赛规则:

(1)一局比赛:
在一局比赛中,先得11分的一方为胜方;10平后,先多得2分的一方为胜方。
(2)一场比赛:
单打的淘汰赛采用七局四胜制,双打淘汰赛和团体赛采用五局三胜制。

(3)模拟乒乓球比赛的完整代码

# -*- coding: utf-8 -*-
"""
Created on Sun May 12 12:26:37 2019

@author: czd33
"""

from random import random
def printIntro(): #声明函数,对整个程序功能进行解释
print("这个程序模拟两个选手A和B的某种竞技比赛")
print("程序运行需要A和B的能力值(以0到1之间的小数表示)——Author's number:33")
def getInputs(): #输入函数,输入选手队伍的能力值以及所模拟进行的比赛场次数量
a = eval(input("请输入选手A的能力值(0-1): "))
b = eval(input("请输入选手B的能力值(0-1): "))
n = eval(input("模拟比赛的场次: "))
return a, b, n
def simNGames(n, probA, probB): #统计对每一场模拟比赛的结果
winsA, winsB = 0, 0
for i in range(n):
scoreA, scoreB = simOneGame(probA, probB)
if scoreA > scoreB:
winsA += 1
else:
winsB += 1
return winsA, winsB
def gameOver(a,b): #判断比赛结束条件
return a==15 or b==15
def simOneGame(probA, probB): #对每一球的得失进行模拟
scoreA, scoreB = 0, 0
serving = "A"
while not gameOver(scoreA, scoreB):
if serving == "A":
if random() < probA:
scoreA += 1
else:
serving="B"
else:
if random() < probB:
scoreB += 1
else:
serving="A"
return scoreA, scoreB
def printSummary(winsA, winsB): #输出模拟的比赛结果
n = winsA + winsB
print("竞技分析开始,共模拟{}场比赛".format(n))
print("选手A获胜{}场比赛,占比{:0.1%}".format(winsA, winsA/n))
print("选手B获胜{}场比赛,占比{:0.1%}".format(winsB, winsB/n))
def main(): #调用上方功能函数的主函数
printIntro()
probA, probB, n = getInputs()
winsA, winsB = simNGames(n, probA, probB)
printSummary(winsA, winsB)
main()

运行结果:

三。模拟足球竞赛

 1.比赛规则:采用回合制。一名球员在一个回合中未能合法击打时,回合结束。本回合胜方得一分、得球权。先得15分赢得一场比赛。

模拟的代码如下

# -*- coding: utf-8 -*-
"""
Created on Tue May 14 18:28:58 2019

@author:33czd
"""

from random import random

def printInfo():    # 打印程序介绍信息
    print('这个程序模拟两个选手A和B的某种竞技比赛')
    print('程序运行需要A和B的能力值(以0到1之间的小数表示')
    print('33czd')

def getInputs():    # 获得程序运行参数
    a = eval(input('请输入选手A的能力值(0-1):'))
    b = eval(input('请输入选手B的能力值(0-1):'))
    n = eval(input('模拟比赛场次:'))
    return a, b, n

def simOneGame(probA, probB):    # 进行一场比赛
    scoreA, scoreB = 0, 0   # 初始化AB的得分
    serving = 'A'         # 首先由A发球
    while not gameOver(scoreA, scoreB):  #用while循环来执行比赛
        if serving == 'A':
            if random() < probA:   # random() 方法返回随机生成的一个实数,它在[0,1)范围内。
                scoreA += 1     # 用随机数来和能力值比较从而分出胜负
            else:
                serving = 'B'
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving = 'A'
    return scoreA, scoreB

def simNGames(n, probA, probB):    #进行N场比赛
    winsA, winsB = 0, 0    # 初始化AB的胜场数
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA += 1
        else:
            winsB += 1
    return winsA, winsB

def gameOver(c, d):    #比赛结束
    return c==15 or d==15

def printSummary(n ,winA, winB):    #打印比赛结果
    print('竞技分析开始,共模拟{}场比赛'.format(n))
    print('选手A获胜{}场比赛,占比{:.2f}%'.format(winA, winA/n*100))
    print('选手B获胜{}场比赛,占比{:.2f}%'.format(winB, winB / n * 100))
def main():
    printInfo()
    probA, probB, n =getInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(n, winsA, winsB)

main()

执行结果

原文地址:https://www.cnblogs.com/czd1/p/10869878.html