## catalogue

# 1, Top down design:

## 1. Top level design:

The most important thing in Item - down design is item - level design. Taking sports competitive analysis as an example, we can start from the IPC of the problem Description begins. Most programs can directly use IPO description in program structure design and sports competition analysis Get the simulation parameters from the user, and finally output the results. Here are four steps of a basic design.

Step 1: print introductory information about the program.

Step 2: obtain the parameters required for program operation, namely probA, probB and ne

Step 3: simulate n games by using the ability values probA and probB of players A and B Step 4: output the number of games and probability of players A and B winning the game.

This basic design is obtained from the IPO description and can be used as a top-down item level design.

Step 1: output some introduction information, which is very useful for improving the user experience. Here is the Python for this step Code, the top-level design generally does not write specific code, but only gives the function definition, in which the printIntro() function is marked Print some necessary instructions.

def main(): printIntro()

Step 2: get user input. The input statement, input format and other details are encapsulated or hidden through the function. Just assume that if the program calls the getInputs() function, the values of variables probA, probB and n can be obtained. This function must return these values to the main program. As of step 2, the whole code is as follows,

def main(): printIntro() probA, probB, n = getInputs()

Step 3: you need to use probA and probB to simulate n games. At this time, A similar procedure in step 2 can be used Method, design A simNGames() function to simulate n games and return the results. According to sports competition The function needs to simulate the game and give the results of player A and player B winning the game. Cut off step Step 3, the Python code of the program is as follows:

def main(): printIntro() probA, probB, n = getInputs() winsA. winsB = simNGamesl(n， ProbA, probB)

Step 4: output the results. The design idea is similar. Only functions and functions are planned. The code is as follows:

def main() ; PrintIntro() probA, probB, n = getInputs() winsA, winsB = simNGames (n,probA, probB) PrintSummary (winsA, winsB)

So far, the procedural framework of sports competition analysis has been clear, but this is only the framework, main0) The new does nothing. The original problem is divided into four independent lie numbers: pintntro0, getlnputsO, simnNCG number and printsummary) The names, input parameters, and expected return values of these numbers have been determined. This decomposition process is very useful because it allows programmers to avoid having to Care about the specific details and concentrate on the program design.

## 2. Design of the nth floor:

After the top-level design, the main() function becomes the top-level structure of sports competition analysis. The above design can be represented as the following figure, in which each layer is executed from left to right, each function is represented by a rectangle, and the line connecting the two rectangles represents the call relationship between the above function and the following function. In terms of information flow, arrows and annotations represent inputs and outputs

In the design of each layer, how to design parameters and return values is the focus, and other details can be ignored. The process of determining the important characteristics of an event while ignoring other details is called abstraction. Abstract is a basic design method. The top-down design process can be regarded as the process of discovering and abstracting functions. The second stage of top-down design is to implement or further Abstract Layer 2 functions.

The printlntro() function should output a program introduction. The Python code of this function is as follows. This function is composed of Python basic expressions to add or change the program structure.

def printIntro(): print("This program models two players A and B Mozambique China competitive competition") print("Program running line needs input and output B Ability value of some kind of competitive competition (Expressed as a decimal between 0 and 1) ")

getInputs() function gets three values that need to be returned to the main program according to the prompt. The code is as follows:

def getInputs(): a = eval(input("Please enter a contestant A Capability value (0)-1): ")) b = eval(input("Please enter a contestant B Capability value (0)-1): ")) n = eval(input("Number of simulated competitions:")) return a, b, n

The simNGameson function is the core of the whole program. Its basic idea is to simulate n games and track and record how many games each player has won. Simulating n games "feels like a counting cycle, while tracking and recording the winning games is more like a counting process. This is a fairly straightforward and rent granularity design, similar to the top-level design. Its Python code is as follows:

def simNGmes(n, probA, probB) : winsA, winsB= 0,0 for i in range(n): scoreA,coreB = simOneGame (probA, probB) if scoreA > scoreB: winsA += 1 else: winsB += 1 return winsA, winsB

The simneGame0 function is designed in the code to simulate a game, This function needs to know the probability of each player and return the final score of two players. Figure 8 shows the update of the overall structure of this design.

Next, you need to implement the simOneGame0 function. In order to simulate A game, The code needs to be written according to the game rules. Two players A and B continue to attack each other until the end of the game. The infinite loop structure can be adopted until the conditions for the end of the game are established. At the same time, the game scores need to be tracked and recorded, and the service marks need to be retained, Anyway, Simulate the game process as detailed as possible. In the cycle of simulated game, we need to consider the problem of single service right and score. Through random number and probability, we can determine whether the server has won the score (random o)< prob). If player A serves, it needs to use the probability of A, and then update whether player A scores or gives the ball right to player B according to the service result. The code of this function is as follows:

def simOneGame (probA ,probB): scoreA, scoreB = 0，0 serving = "A" while not gameover(acoreA, scoreB) : if serving "A" if random() < probA: scoreA += 1 else: serving= "B" else : if random () < probB : scoreB += 1 else : serving= "A" return scoreA, scoreB

Here, the gameOver() function is further designed to represent the condition for the end of a game, For different sports competitions, the ending conditions may be different. Encapsulating this function helps to simplify the cost of modifying the function according to different rules and improve the maintainability of the code. The gameOver() function tracks the score change and returns True at the end of the competition, and returns False if it is not finished. Then continue with the rest of the loop. The following figure is a new structure diagram of the program.

According to the game rules, when any player reaches 15 points, the game ends. The implementation code of gameOver() function is as follows:

def gameOver(a,b): return a==15 or b==15

Finally, the printSummary() function, whose python code is as follows:

def printSummary(winsA,winsB): n = winsA + winsB print("Competitive analysis begins,Co simulation{}Game".format(n)) print("player A win victory{}Game,Proportion{:0.1%}".format(winsA,winsA/n)) print("player B win victory{}Game,Proportion{:0.1%}".format(winsB,winsB/n))

All implementation codes are as follows:

from random import random def printIntro(): print("This program models two players A and B Mozambique China competitive competition") print("Program running line needs input and output B Ability value of some kind of competitive competition (Expressed as a decimal between 0 and 1) ") def getInputs(): a = eval(input("Please enter a contestant A Capability value (0)-1): ")) b = eval(input("Please enter a contestant B Capability value (0)-1): ")) n = eval(input("Number of simulated competitions:")) return a, b, n def simNGmes(n, probA, probB) : winsA, winsB= 0,0 for i in range(n): scoreA,scoreB = simOneGame (probA, probB) if scoreA <= scoreB: winsB += 1 else: winsA += 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("Competitive analysis begins,Co simulation{}Game".format(n)) print("player A win victory{}Game,Proportion{:0.1%}".format(winsA,winsA/n)) print("player B win victory{}Game,Proportion{:0.1%}".format(winsB,winsB/n)) def main() PrintIntro() probA,probB,n=getInputs() winsA,winsB=simNGames(n, probA, probB) PrintSummary(winsA,winsB) main()

Finally, return to the sports competition analysis problem, through the simulation method to analyze the different game results caused by the small difference in ability between players. Will there be A phenomenon that the small difference in ability leads to one-sided game results? Suppose A wins 45% of the game on the serve, while his opponent B wins 55% more on the serve. The results show that although there is only A small gap (5%) in ability, But A needs about three games to win one. He won three There is little chance of A game in one or five innings. Further, this program can be extended to badminton, table tennis, tennis and other modes, and the law of sports competition can be found. Of course, the premise of in-depth discussion on the competitive law is that there is little difference in the level of contestants, and the found law will help to make up for the short board. There is no need to explore the competitive law similar to the Chinese men's football team and the Brazilian men's football team.