1、體育競技分析:模擬N場比賽
2、計算思維:抽象 + 自動化
3、模擬:抽象比賽過程,自動模擬N場比賽,當N越大時,比賽結果分析越科學。
4、本次比賽規則:1、回合制,先由一方發球,如勝利,則得1分並繼續發球,如失敗,則雙方分數不改變並交換球權。球員AB的能力為0-1之間的一個數值先得11分的一方為勝方;10平后,先多得2分的一方為勝方。
2、單打的淘汰賽采用七局四勝制,雙打淘汰賽和團體賽采用五局三勝制。
重點:
思維方式:自頂向下即將一個復雜問題分解成幾個問題,再細分成一個個具體的小問題,從而來解決復雜問題。自底向上為自頂向下的逆過程,即解決復雜問題的方法,逐步解決一個個小問題,來達成目的。
2.將體育競技分析分解為以下幾個小步驟
1、打印程序的介紹性信息式
2、獲得程序運行參數:probA(A的能力值),probB(B的能力值),n(比賽場次)
3、利用球員AB的能力值,模擬n場比賽
4、輸出球員AB獲勝的場次及概率
3.將各個步驟定義成函數來實現:
分解代碼
from random import random #引用隨機庫 def printIntro(): #打印程序介紹信息 print("這個程序模擬兩個選手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): #進行N場比賽 每場比賽是7局4勝制 def gameOver(a,b): #正常比賽結束 def gameOver2(a,b): #搶12比賽結束 def simOneGame(probA, probB): #進行一場比賽 def simtwoGame2(probA,probB): #進行搶12比賽 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():
3完整的代碼如下:
from random import random def printIntro(): #打印程序介紹信息 print("信計一班10號石貴雨進行比賽分析結果:") print("這個程序模擬兩個選手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): # 進行N場比賽 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 gameOver(a,b): #正常比賽結束 return a==11 or b==11 def gameOver2(a,b): #進行搶12比賽結束 if abs((a-b))>=2: return a,b def simOneGame(probA, probB): #進行一場比賽 scoreA, scoreB = 0, 0 #初始化AB的得分 serving = "A" while not gameOver(scoreA, scoreB): #用while循環來執行比賽 if scoreA==10 and scoreB==10: return(simtwoGame2(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 simtwoGame2(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 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) n=input("輸入任何數") main()
4、模擬1000場單打7局4勝的比賽情況結果:
5.模擬團隊打5局3勝的情況:
代碼如下:
from random import random def printIntro(): #打印程序介紹信息 print("信計一班10號石貴雨進行比賽分析結果:") print("這個程序模擬兩個隊伍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): # 進行N場比賽 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 gameOver(a,b): #正常比賽結束 return a==11 or b==11 def gameOver2(a,b): #進行搶12比賽結束 return a==12 or b==12 def simOneGame(probA, probB): #進行一場比賽 scoreA, scoreB = 0, 0 #初始化AB的得分 serving = "A" while not gameOver(scoreA, scoreB): #用while循環來執行比賽 if scoreA==10 and scoreB==10: return(simtwoGame2(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 simtwoGame2(probA,probB): scoreA,scoreB=10,10 serving = "A" #假如先讓隊伍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 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()
模擬1000場打5局3勝的比賽情況:
6模擬排球比賽:規則7局4勝,先得24分的一方為勝,23平的話就要進行先拉2分的比賽
from random import random def printIntro(): #打印程序介紹信息 print("信計一班10號石貴雨進行比賽分析結果:") print("這個程序模擬兩個選手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): # 進行N場比賽 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 gameOver(a,b): #正常比賽結束 return a==24 or b==24 def gameOver2(a,b): #進行搶12比賽結束 if abs((a-b))>=2: return a,b def simOneGame(probA, probB): #進行一場比賽 scoreA, scoreB = 0, 0 #初始化AB的得分 serving = "A" while not gameOver(scoreA, scoreB): #用while循環來執行比賽 if scoreA==10 and scoreB==10: return(simtwoGame2(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 simtwoGame2(probA,probB): scoreA,scoreB=23,23 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 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) n=input("輸入任何數") main()
比賽結果如圖:
6、最后將該程序進行封裝
1、 首先要安裝pyinstall庫,在cmd中用輸入pip install PyInstaller
2.接着會自動下載,安裝成功后 通過Pyinstall自帶命令進行打包
安裝過程就不截圖了。
3.控制台輸入 Pyinstaller -F xxx(pyw文件路徑,例如c://user/desktop/demo.py)
4.打包后exe文件路徑在項目下dict文件夾中 具體可以看日志
具體情況:
執行情況:
在dict文件下可查看該exe程序