Although the python tic-tac-toe game looks very simple, it is very worth learning.
Let's see how to play:
Display the rule description, here is the description of the gameplay, and how to judge the outcome and so on.
Decide who will go first.
The above is the process of playing, how to write the code?
To write a program with many steps, it is very important to plan the program before writing;
Fake code:
Print display gameplay
Decide who goes first
Create an empty chessboard
Print shows the current board
When no one wins and it is not a tie:
If it's the player's turn
Get the player's chess position
Change the board according to the position
otherwise
Calculate the computer's chess position
Change the board according to the position
Print shows the current board
Switch player
Congratulate the winner or explain the tie
The pseudo code actually shows all the steps very clearly, and it is easier to convert to the corresponding code;
Define a function for each step and adopt a top-down modular design idea;
The most important thing in modular design is to understand what is input and what is output;
Print display
No input or output is required, just print the text content directly.
Decide who goes first
There is interactive input, parameters can be passed in through variables, or input during operation. Here, the second type is used to output the corresponding sequential results.
The same principle applies to other methods. In this process, careful consideration of this design idea is very helpful for the future design of large-scale programs.
Create a chessboard
Show chess board
Player and computer play chess
Switch sides and judge whether you win or lose
Congratulations to players
If you want to break through your own bottleneck in programming, you must first have a very solid foundation, and then practice internal skills, which requires your own deliberate training;
In many cases, we wrote a lot of case code, but there was no essential breakthrough, resulting in no progress.
Recommended Posts