I made Othello to teach Python3 to children (2)

Programming should be expressed in words

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.

Main program of Othello game

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)

Start of main program

ʻ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.

Description of the main program

When you first run the program, you will see a screen like this, waiting for keyboard input.

スクリーンショット 2020-05-23 22.54.49.png

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

Studying Python in the main program

There are two types of repetitive sentences

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.

スクリーンショット 2020-05-23 23.37.13.png

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()

Judgment of conditions

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

I made Othello to teach Python3 to children (4)
I made Othello to teach Python3 to children (2)
I made Othello to teach Python3 to children (5)
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I made Othello to teach Python3 to children (6) Final episode
I made a password generator to teach Python3 to children (bonus) * Completely remade
I made blackjack with python!
I made a Python module to translate comment outs
I made a python text
I made a python library to do rolling rank
I made blackjack with Python.
I made an action to automatically format python code
Othello made with python (GUI-like)
I made wordcloud with Python.
I made a package to filter time series with python
I made an animation to return Othello stones with POV-Ray
I made a Line-bot using Python!
I tried to touch Python (installation)
I made my own Python library
I made a fortune with Python.
Othello game development made with Python
I want to debug with Python
I made a daemon with Python
I made a library to easily read config files with Python
I tried to teach Python to those who have no programming experience
I made a library that adds docstring to a Python stub file.
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I made a payroll program in Python!
I made a character counter with Python
I installed Python 3.5.1 to study machine learning
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
I want to analyze logs with Python
I want to play with aws with python
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
I made a script to display emoji
I made a Hex map with Python
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
After studying Python3, I made a Slackbot
I made a roguelike game with Python
What I was addicted to Python autorun
I wanted to solve ABC172 with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
What I did to save Python memory
Othello app (iOS app) made with Python (Kivy)
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
[Python] I made a decorator that doesn't seem to have any use.
I made a tool to automatically browse multiple sites with Selenium (Python)
I made a web application in Python that converts Markdown to HTML
I tried to discriminate a 6-digit number with a number discrimination application made with python
Environment maintenance made with Docker (I want to post-process GrADS in Python