1. 程式人生 > 實用技巧 >乒乓球比賽

乒乓球比賽

單人賽預測

from random import random
 
#列印程式介紹資訊
def printIntro():
    print("這個程式模擬兩個選手A和B的乒乓球比賽")
    print("程式執行需要A和B的能力值(以0到1之間的小數表示)")
 
#獲得程式執行引數
def printInputs():
    a = eval(input("請輸入選手A的能力值(0-1): "))
    b = eval(input("請輸入選手B的能力值(0-1): "))
    n = eval(input("模擬比賽的場次: "))
    return a, b, n
 
# 進行N場比賽
def simNGames(n, probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        for j in range(7):           #進行7局4勝的比賽
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
    return winsA,winsB
 
 
 #進行一場比賽
def simOneGame(probA, probB):
    scoreA, scoreB = 0, 0           #初始化AB的得分
    serving = "A"                
    while not gameOver(scoreA, scoreB):     #用while迴圈來執行比賽
        if scoreA==10 and scoreB==10:
            return(simOneGame2(probA,probB))
        if serving == "A":
            if random() < probA:            ##用隨機數生成勝負
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
 
def simOneGame2(probA,probB):
    scoreA,scoreB=10,10
    serving = "A"
    while not gameOver2(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 gameOver(a,b):               #正常比賽結束
    return a==11 or b==11
def gameOver2(a,b):              #進行搶12比賽結束
   if abs((a-b))>=2:
       return a,b
 
 
#輸出資料
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 = printInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
 
main()

  

雙人賽或者團體賽

from random import random
 


#列印程式介紹資訊
def printIntro():
    print("這個程式模擬兩個選手A和B的乒乓球比賽")
    print("程式執行需要A和B的能力值(以0到1之間的小數表示)")
 
#獲得程式執行引數
def printInputs():
    a = eval(input("請輸入選手A的能力值(0-1): "))
    b = eval(input("請輸入選手B的能力值(0-1): "))
    n = eval(input("模擬比賽的場次: "))
    return a, b, n
 
# 進行N場比賽
def simNGames(n, probA, probB):
    winsA, winsB = 0, 0
    for i in range(n):
        for j in range(5):           #進行5局3勝的比賽
            scoreA, scoreB = simOneGame(probA, probB)
            if scoreA > scoreB:
                winsA += 1
            else:
                winsB += 1
    return winsA,winsB
    return winsA,winsB
 
 
 #進行一場比賽
def simOneGame(probA, probB):
    scoreA, scoreB = 0, 0           #初始化AB的得分
    serving = "A"                
    while not gameOver(scoreA, scoreB):     #用while迴圈來執行比賽
        if scoreA==10 and scoreB==10:
            return(simOneGame2(probA,probB))
        if serving == "A":
            if random() < probA:            ##用隨機數生成勝負
                scoreA += 1
            else:
                serving="B"
        else:
            if random() < probB:
                scoreB += 1
            else:
                serving="A"
    return scoreA, scoreB
 
def simOneGame2(probA,probB):
    scoreA,scoreB=10,10
    serving = "A"
    while not gameOver2(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 gameOver(a,b):               #正常比賽結束
    return a==11 or b==11
def gameOver2(a,b):              #進行搶12比賽結束
   if abs((a-b))>=2:
       return a,b
 
 
#輸出資料
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 = printInputs()
    winsA, winsB = simNGames(n, probA, probB)
    printSummary(winsA, winsB)
 
main()