Sports Analysis
problem analysis
Sports Analysis
Requirements: How much is a millimetre?
How to scientifically analyze sports competitions?
Input: player's level
Output: Predictable game score
Sports competition analysis: simulate N games
Computational Thinking: Abstraction + Automation
Simulation: abstract game process + automatic execution of N games
When N is larger, the game result analysis will be more scientific
competition rules
Doubles: A & B, turn-based, best of 5
At the beginning, one party will serve first until a point is awarded, and then the winner will serve
Players can only score on serve, 15 points wins a game
This involves two concepts, top-down design and bottom-up execution
Top-Down Efficient Approach to Solving Complex Problems
Express a general problem as a form composed of several small problems
Use the same method to further break down small problems
Until, small problems can be solved simply and clearly by computer
bottom-up (execution)
Efficient testing methods for building complex systems step by step
Sub-unit testing, step-by-step assembly
Follow the reverse path from top to bottom
Until, all parts of the system have been tested and verified with the idea of assembly
This is a Python program for simulating the results of some kind of competitive competition between two players A and B, and gives a statistical analysis of the results of the competition.
First, the program defines several functions:
- printIntro(): Used to print a brief introduction to the program.
- getInputs(): Used to obtain the ability values of players A and B entered by the user, as well as the number of simulated games.
- simNGames(n, probA, probB): Used to simulate n games and return the number of games won by players A and B.
- gameOver(a,b): It is used to judge whether the game is over. When the score of player A or B reaches 15 points, the game ends.
- simOneGame(probA, probB): Used to simulate the result of a game and return the scores of players A and B.
- printSummary(winsA, winsB): It is used to print the competitive analysis results, including the number of simulated games, the winning games and percentages of players A and B.
Then, the program calls the main() function, which performs the following operations in sequence:
- A brief introduction to the printing program.
- Obtain the ability values of players A and B entered by the user, as well as the number of simulated games.
- Simulate n games and return the number of wins of players A and B.
- Print the competitive analysis results, including the number of simulated games, the number of games won by players A and B and their proportions.
Generally speaking, this program is a simple competitive analysis simulation program, which simulates the results of the game through random numbers, and conducts statistical analysis on the results of the game to obtain the winning percentage of players A and B.
#!/usr/bin/env python
# -- coding: utf-8 --
# @Time : 2023/4/11 16:50
# @File : 体育竞技分析.py
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):
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()
Python programming thinking
Computational Thinking and Programming
A 3rd trait of the human mind
Logical thinking: reasoning and deduction, represented by mathematics, A->B B->C A->C -
Empirical thinking: experiments and verification, represented by physics, gravitational waves<-experiments-
Computational Thinking: Design and Construction, Computer Representation, Towers of Hanoi Recursion
abstraction and automation
Computational Thinking: Computational Thinking
The calculation process of abstract problems, using computer automation to solve
Computational thinking is the way of thinking based on computers
It used to be empirical thinking, by querying local historical weather data, and based on such experience, to guess the temperature of the next day. Empirical thinking constructs future values from physical, empirical things
Computational thinking is to use computers to calculate the weather, so that the data is more accurate
Computing ecology and python language
High-quality computing ecology Python123 - programming is easier
User experience and software products
The four steps of application development
Note: The content comes from the MOOC -- Teacher Songtian