体育竞技分析

一、体育竞技分析

1. 简介: 模拟不同的两个队伍进行排球的模拟比赛。

2. 模拟原理: 通过输入各自的能力值(Ⅰ),模拟比赛的进行( P ),最后输出模拟的结果( O )。

P 简介:通过产生随机数得到每局比赛的难度,若小于能力值则表示赢得本局比赛,反之输掉本局比赛。

3. 规则简介:

① 每场比赛采用 5局3胜制。

② 前四局采用25分制,每个队只有在赢得至少25分,且同时超过对方2分时才胜一局。

③ 决胜局(第五局)采用15分制,先获得15分,且同时超过对方2分为胜。

4. 准备就绪,就差代码来实现了

插入代码之前,先对代码做个简单的介绍:

printInfo() 打印程序的介绍信息
getInputs() 获得用户输入的参数
simNGames(n, probA, probB) 模拟n场比赛
simOneGame(probA, probB) 模拟一场比赛,包括五局,采取五局三胜制
simAGame(N, probA, probB) 模拟一局比赛
GameOver(N, scoreA, scoreB) 定义一局比赛的结束条件
printResult(n, winsA, winsB) 输出模拟比赛的结果

行了。直接上代码

复制代码
# -*- coding: utf-8 -*-
"""
Created on Sun May 12 23:15:03 2019

@author: guo'yu'yi
"""

import random
import os
def printInfo():
    print("gyy的这个程序模拟两个选手A和B的某种竞技比赛 : ")
    print("需要A和B的能力值(以0到1之间的小数表示):")
def getInputs():
    proA=eval(input("请输入选手A的能力值(0-1):"))
    proB=eval(input("请输入选手B的能力值(0-1):"))
    n=eval(input("模拟比赛的场次:"))
    return proA,proB,n
def simNGames(proA, proB, n):
    countA,countB=0,0
    for i in range(n):          
        if SimOneGame(proA, proB)==1:     
            countA+=1
        else:       
            countB+=1
    return countA, countB
def SimOneGame(proA, proB):
    temp=0.5
    while temp==0.5:
        temp=random.random()
    if temp<0.5:
        serve="A"  
    else:
        serve="B"
    setA,setB=0,0
    while not gameOver(setA,setB):
        scoreA,scoreB=0, 0       
        while not setOver(scoreA, scoreB,setA+setB+1):      
            if serve=="A":
                if random.random()<proA:      
                    scoreA+=1
                else:
                    scoreB+=1
                    serve="B"
            else:
                if random.random()<proB:      
                    scoreB+=1
                else:
                    scoreA+=1
                    serve="A"
 
        if scoreA>scoreB:
            setA+=1
        else:
            setB+=1
    return (1 if (setA > setB) else 0)
def setOver(scoreA, scoreB,sets):
    if sets==5:         
        if scoreA>=15 or scoreB>=15:
            if(abs(scoreA-scoreB)>=2):      
                return True
            else:       
                return False
        else:
            return False
    else:           
        if scoreA>=25 or scoreB>=25:
            if(abs(scoreA-scoreB)>=2):       
                return True
            else:
                return False
        else:       
            return False
def gameOver(setA,setB):
    if setA==3 or setB==3:      
        return True
    else:
        return False
def printSummary(countA, countB):
    print("选手A获胜{0}场比赛,占比{1:.2f}%".format(countA, countA/(countA + countB)*100))
    print("选手B获胜{0}场比赛,占比{1:.2f}%".format(countB, countB/(countA + countB)*100))
if __name__ == "__main__":
    printInfo()
    proA, proB, n = getInputs()
    print("竞技分析开始,共模拟{}场比赛".format(n))
    countA, countB = simNGames(proA, proB, n)
    printSummary(countA, countB)
复制代码

结果如下:

 

二、排球比赛

1,比赛规则

  比赛采用每球得分制,前4局以先得25分,并同时超出对方2分的队获胜。

决胜局以先得15分,并同时超出对方2分的队获胜。正式比赛采用五局三胜制。

2、实现排球比赛规则的简单程序

复制代码
from random import random
def printIntro():
    print("36号程序员的程序模拟两个选手A和B的排球比赛")
    print("程序需要A和B的能力值(以0到1之间的小数表示)")
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 gameOver1(a,b):
    return (a>=25 and a-b>2) or (b>=25 and b-a>2)
def gameOver2(a,b):
    return (a>=15 and a-b>2) or (b>=15 and b-a>2)
def simOneGame(probA,probB):
    
    for i in range (5):
        winsA,winsB=0,0
        scoreA,scoreB=0,0
        serving="A"
        while not gameOver1(scoreA,scoreB):
            if serving=="A":
                if random()<probA:
                    scoreA+=1
                else:
                    scoreB+=1
                    serving="B"
            else:
                if random()<probB:
                    scoreB+=1
                else:
                    serving="A"
                    scoreA+=1
            if scoreA>scoreB:
                winsA+=1
            else:
                winsB+=1
            if winsA==3 or winsB==3:
                break
            if winsA==2 and winsB==2:
                simtowGame(probA,probB)
    return winsA,winsB
def simtowGame(probA,probB):
    
    for i in range (1):
        scoreA,scoreB=0,0
        serving="A"
        while not gameOver2(scoreA,scoreB):
            if serving=="A":
                if random()<probA:
                    scoreA+=1
                else:
                    scoreB+=1
                    serving="B"
            else:
                if random()<probB:
                    scoreB+=1
                else:
                    serving="A"
                    scoreA+=1
    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()
复制代码

•效果图

• 打包后的exe文件

 

三,下面是篮球比赛的模拟,这段代码和书上的类似

复制代码
# -*- coding: utf-8 -*-
"""
Created on Tue May 14 19:45:17 2019

@author: guo'yu'yi
"""

from random import *
def printIntro():
    print("这个程序模拟两个队伍A和B的篮球比赛")
    print("这个程序需要A和B的能力值(0-1)")
def getInputs():
    a=eval(input('A的能力值为:'))
    b=eval(input("B的能力值为:"))
    n=eval(input("比赛的场次为:"))
    return a,b,n
def gameover(i,k):
    if i>k:
        return True
    return False
def one(a,b):
    scorea,scoreb=0,0
    x='A'
    i=1
    k=randint(10,40)
    while not gameover(i,k):
        if x=='A':
            if random()<a:
                scorea+=2
            else:
                x='B'
        else:
            if random()<b:
                scoreb+=2
            else:
                x='A'
        i+=1
    return scorea,scoreb
def ones(probA,probB):
    wa,wb=0,0
    for j in range(4):
        xa,xb=one(probA,probB)
        wa+=xa
        wb+=xb
    while wa==wb:
        xa,xb=jiashisai(probA,probB)
        wa+=xa
        wb+=xb
    return wa,wb
def jiashisai(a,b):
    scorea,scoreb=0,0
    i=1
    x='A'
    while not gameover(i,randint(1,5)):
        if x=='A':
            if random()<a:
                scorea+=2
            else:
                x='B'
        else:
            if random()<b:
                scoreb+=2
            else:
                x='A'
        i+=1
    return scorea,scoreb
def simNGames(n,probA,probB):
    a,b=0,0
    for i in range(n):
        x,y=ones(probA,probB)
        if x>y:
            a+=1
        else:
            b+=1
    return a,b
def printSummary(a,b,n):
    print("A赢了{}场          B赢了{}场          共{}".format(a,b,n))
    print("A赢的概率为{:0.1%} B赢的概率为{:0.1%}".format(a/n,b/n))
def main():
    printIntro()
    probA,probB,n=getInputs()
    winsA,winsB=simNGames(n,probA,probB)
    printSummary(winsA,winsB,n)
main()
复制代码

结果如下:

posted @ 2019-05-20 11:20  Emxin  阅读(357)  评论(0编辑  收藏  举报