If you want to make an Othello game, you first need an 8x8 board. There are 64 squares in all.
In order to represent the position of the squares, I decided to use the numbers 1 to 8 as X in the horizontal direction and the numbers 1 to 8 as Y in the vertical direction. Generally, it seems that the horizontal axis of Othello is often represented by a to h, but when studying programming, numbers are easier to understand, so make it so that both horizontal and vertical are represented by 1 to 8. There is.
This is the only main program of Othello games I made to teach children. As I wrote in the previous article, the basics of the program are only control statements, conditional statements, and repetitive statements. This Othello is also made by making full use of these three. Let's explain the following program.
if __name__ == '__main__':
#Create an instance of the Othello class
myot = OthelloCls()
#Board display
myot.ot_display()
#Repeat forever
while True:
#Determine if the game is over or if there is a place to put your hand
sts = myot.ot_checkendofgame()
if sts == 1: #Game over
break
elif sts < 0: #I can't put my hand
continue
#Call a function that shows who's turn
myot.ot_yourturn()
#Enter the X and Y coordinates
myx = myot.ot_inputXY("X = ")
myy = myot.ot_inputXY("Y = ")
#Put your hand on the specified coordinates
myot.ot_place(myx,myy)
ʻIf name == The declaration of'main':` indicates that the following indentation is the main program from here. Since the block of the main program needs to be indented, the indentation is done in 4 spaces and the program is written.
When you first run the program, you will see a screen like this, waiting for keyboard input.
The first line of the program materializes an instance of the Othello class. There is no doubt that the Othello class is packed with various functions required for Othello, but please be assured that the details will be described later.
The board of Othello in the very first state is displayed on the screen. When you call the function ʻot_display () `, it will display the board in the state when it was called.
The main program starts by repeating forever at the whilt True:
part.
In the Othello game, white and black take turns, but eventually it comes when either hand can't be placed or both hands can't be placed.
The only condition to exit this eternal repetition is if you decide that the game is over. A function called ʻot_checkendofgame () `is responsible for checking the next behavior in a permanent iteration. The value returned by this function determines what to do next.
Return value from function | meaning | Next action |
---|---|---|
0 | Continue because you can put your own hands | Go to X and Y coordinate input where to put from the keyboard |
1 | Game over | Escape from eternal repetition |
-1 | I can't put my own hand, so I changed to the other party's hand | Return to the beginning of eternal repetition |
As mentioned above, if the return value of the function is 0, the function ʻot_yourturn () `indicates who's turn, such as" ●'s hand! "Or" ○'s hand! ".
Next, enter one of 1 to 8 with X and one of 1 to 8 with Y as to which square to place from the keyboard. The function ʻot_inputXY ("X =") `is a function that performs keyboard input that does not accept values other than 1 to 8, and implements keyboard input as one function by specifying X = or Y = as a parameter. There is. The parts of X = 6 and Y = 5 in the above figure are exactly done by this function.
Finally, instruct the function ʻot_place (myx, myy)` to place the frame by passing the X-coordinate number entered from the keyboard and the Y-coordinate number.
An Othello program can be played as an Othello game by calling only five functions. The types of programs are divided into screen display system, input system, and program logic system. In order to evolve into a graphical Othello game in the future, the screen display part and input system and the logic part are intentionally separated in the class.
Functions for making Othello games | Program type | What you are doing |
---|---|---|
ot_display() | Screen display | Display the board of Othello |
ot_checkendofgame() | logic | Tells you what to do next in eternal repetition |
ot_yourturn() | Screen display | Show who's turn |
ot_inputXY() | Keyboard input | Enter the coordinates to specify the location to place the frame |
ot_place() | logic | Place the frame at the entered coordinates |
As I mentioned briefly in the previous article, there are two types of repetitive sentences. One is a repeating sentence that turns forever. The other is a repeating sentence in which the number of turns is known. In the figure below, in the type that repeats forever, when an event that breaks out of the repetition occurs, it escapes from the repetition. The other is a repeating pattern where you decide to do sit-ups 10 times from the beginning and then finish 10 times. In Python, permanent iterations are written as while True:
. The indent block under this while True:
will be repeated forever unless some event occurs. In the eternal repetition, write continue
to return to the beginning, and write break
to escape from the eternal repetition. In the figure below, if "Snack?" Is Yes, write break
, otherwise write continue
. In the main program, I write continue
when I want to return to the beginning of the repetition in the middle of a block that repeats forever, but after calling ʻot_place (), I do not bother to write
continue`. Since it is the last line of the repeating block, it automatically returns to the beginning of the repeating when returning from the function.
If you write these two in Python, it will look like this. I will write more about how to use for next time.
while True:
study()
if snack=="Yes":
break
for index in range(10):
Abs()
Use the ʻif condition: to determine the condition. The logic that meets the conditions is described in the indented block, and the logic that does not meet the conditions is described by indenting from under ʻelse:
or ʻelif:`. Let's take a look at the main program earlier. The return value from the function ot_checkendofgame () is stored in a variable called sts. The following if statement determines if sts is 1, and if so, uses brek to break out of the endless loop. If elif does not meet the first condition, the next conditional statement determines whether sts is less than 0, and if the condition is met, you can see that continue returns to the beginning of the permanent iteration.
sts = myot.ot_checkendofgame()
if sts == 1: #Game over
break
elif sts < 0: #I can't put my hand
continue
Symbol used for condition judgment | How to use | Description |
---|---|---|
equal sign== | if sts == 1: | sts equals 1 |
Inequality sign!= | if sts != 1: | sts is not equal to 1 |
Greater> | if sts > 1: | sts is greater than 1 (2 or greater) |
Small< | if sts < 1: | sts is less than 1 (less than or equal to 0) |
Greater equal sign>= | if sts >= 1: | sts is 1 or more |
Small equal sign<= | if sts <= 1: | sts is 1 or less |
So, next time, I would like to briefly explain the actual execution of the program and the variables used in Python.
<< I made Othello to teach Python3 to children (1) I made Othello to teach Python3 to children (3) >>
Recommended Posts