You have a blood type, right? Whether it is true or not, A-type, B-type, O-type, AB-type, etc., the type has its own characteristics, like "A-type that is meticulous" and "B-type that is selfish". In programming, types are used to make important divisions like blood types.
I've talked a lot about ** variables ** so far, but I haven't dared to mention types. I explained that arrays are a class called list (), and dictionary variables are also a class called dict (). The data type in the program, like the blood type, represents the nature of the data.
Data type name | Data type | Description |
---|---|---|
String | str | "letter"Or'letter' |
integer | int | +-Numerical value |
Floating point | float | +-Floating point |
Imaginary number | complex | +-Real part+-Imaginary number部j |
Authenticity | bool | True or False |
Date Time | datetime | Date and time |
List (array) | list | Array regardless of data type |
Tuple (reference sequence) | tuple | Array regardless of data type |
Set (unique array) | set | Block array with unique and out-of-order data |
Dictionary (keymap array) | dict | Key vs. data array |
In Python, when using variables, the types are assigned without any special awareness. This is called dynamic typing
. For example, if you write ʻage = 12`, Python will make the type int (). To find out the assigned type, you can do type (age) to find out the type assigned to the variable age. As I wrote last time, all types are classes that include the functions required to handle the data type. If it is assigned to an unintended type, you can explicitly specify it with float () etc. or convert the type.
First is the function that performs keyboard input. Displays the string passed as an argument and waits for keyboard input. If you hit the enter key blank or enter a value other than 1 to 8, you will return to keyboard input again. Once the correct number is entered, int () converts it back to an ** integer **. Important values for this Othello class are numbers 1-8 of X and Y. Even if it becomes a graphical Othello and the location is clicked with the mouse, it can be internally converted to the X and Y coordinates of 1 to 8 so that the functions of the current class can be used as they are.
#input
def ot_inputXY(self,otstr):
while True:
myXY = input(otstr)
if myXY == "":
continue
if '1' <= myXY <= '8' :
return int(myXY)
The function ʻot_yourturn () is a function that only displays" It's the hand of ●! "Or which player's turn. What is displayed is a character string enclosed in "~", but ** "character string" ** is the character string class
str ()` as mentioned above, so even a character string just written as "aiueo" is str. The functions contained in the () class can be used as they are.
The {} and format ()
described here when using the string type are very common expressions, so remember them here. {}
Is called ** replacement field ** and so on. str ("Aka {} Tana ". format (" sa ")
is a convenient description that replaces {} with the data specified by the format () function. In Python 3.6 and later, the f character. You can now put variables directly into the replacement field as a column operation, improving readability.
name = input("Please enter your name=")
print("A brave man who came often{}Yo! !! I've been waiting for a hero to destroy the Demon King!".format(name))
# Python3.After 6 it is convenient to write as follows!
print(f"A brave man who came often{name}Yo! !! I've been waiting for a hero to destroy the Demon King!")
In ʻot_yourturn () `in the actual Othello class, ● or ○ is displayed because the variable ot_offdef that stores the current hand is specified in the replacement field.
#It's your turn
def ot_yourturn(self):
print("{}It's your hand!".format(self.ot_offdef))
The ʻot_changeturn () `function is a function that switches the attacker's turn.
#It's your turn
def ot_changeturn(self):
#Store the attacker's piece
self.ot_offdef = '●' if self.ot_offdef == '○' else '○'
#Stores the opposite piece to the attacker
self.ot_search = '●' if self.ot_offdef == '○' else '○'
Programs have a ternary operator. It's almost like writing an IF statement in an Excel macro, but the logic above and the logic below do exactly the same thing. By writing value when the condition is met if condition else value when the condition is not met
, the if conditional statement can be concisely stored in one line. The ternary operator has the effect of refreshing the program depending on how it is written, but if you use it forcibly, it will be a very unreadable source, so it is recommended to use it with caution. If you find it difficult to understand, you can replace it with the following source.
#It's your turn
def ot_changeturn(self):
#Store the attacker's piece
if self.ot_offdef == '○':
self.ot_offdef = '●'
else:
self.ot_offdef = '○'
#Stores the opposite piece to the attacker
if self.ot_offdef == '○':
self.ot_search = '●'
else:
self.ot_search = '○'
The ʻot_checkendofgame () `function is a program that considers whether the Othello game will end. Please refer to (2) of this series of articles for the operation.
What we are doing in this is as follows.
In this function, count (), which is called myot.ot_bit.count ('●')
, is a function that returns the number of data contained in the array of the list () class.
#Determine if it's over (change the turn if you just can't put your hand)
def ot_checkendofgame(self):
#There is no place to put your own hand, and you can't put your opponent's hand
if 0 > self.ot_canplacemypeace():
self.ot_changeturn()
if 0 > self.ot_canplacemypeace():
bc = myot.ot_bit.count('●')
wc = myot.ot_bit.count('○')
if ( bc - wc ) > 0:
bws = "● Victory"
elif ( bc - wc ) < 0:
bws = "○ Victory"
else:
bws = "draw"
print("{}is. Thank you for your hard work!".format(bws))
return 1
else:
print("{}There is no place to put it. It changes to the opponent's turn! !!".format(self.ot_search))
return -1
else:
return 0
The previous ʻot_checkendofgame () determined if the game was over, if there was a place to put your hand, or if not, if there was a place to put your opponent's hand. The ʻot_canplacemypeace ()
function thinks "Is there a place to put my hand?"
#Find out if there is a place to put your hands
def ot_canplacemypeace(self):
for n in range(64):
if self.ot_bit[n] != '・':
continue
for d in self.ot_direction:
if 1 == self.ot_next_onepeace(int(n%8)+1,int(n/8)+1, d):
return 0
#If there is no place to put one, I will come here
return -1
Here's what we're doing:
for d in self.ot_direction:
, the key string indicating the direction stored in the dictionary is stored in the variable d, which is repeated for the number of times of the dictionary variable.As I said last time, if you put a piece in the specified place, you have to judge whether it is a place where you can put it, and if you can put it, you have to turn over the opponent's piece. And if the place where you put it is a place where you should not put it in the first place, you have to display that fact.
#Put your hand on the board of Othello
def ot_place(self, ot_x, ot_y):
ot_change = False
for d in self.ot_direction:
if 1 == self.ot_next_onepeace(ot_x,ot_y,d):
self.ot_peace(ot_x,ot_y,self.ot_offdef)
self.ot_changepeace(ot_x,ot_y,d)
ot_change = True
#When there is no valid direction
if not ot_change:
print("{}Could not be placed".format(self.ot_offdef))
return -1
#Display the screen and change your hand to the opponent's turn
self.ot_display()
self.ot_changeturn()
return 0
This function is called when you try to put a frame in a predetermined place. At the specified location, X and Y are the coordinates specified by the values 1 to 8, respectively. Now let's explain the program of this function.
. The variable d contains the key value of ʻot_direction
.function with your hand in the third argument to place your frame at the specified coordinates. The ʻot_peace ()
function is also a function that returns one frame at the specified coordinates if the third argument is omitted. If the third argument is specified, the frame at the specified coordinates will be replaced with the specified character. flag is True when exiting the iteration, the flipping process has completed the rewriting of the board data ʻot_bit
, so call the ʻot_display ()function to redisplay the board. Then call the ʻot_changeturn ()
function to change the attacker's turn.Next time, I will explain the ʻot_next_onepeace () function and the ʻot_changepeace ()
function, which may be the most complicated. These two functions are called recursive functions, which are the functions that you call yourself, so I would like to explain about them.
See you again! !!
c u
<< I made Othello to teach Python3 to children (4) I made Othello to teach Python3 to children (6) >>
Recommended Posts