Python数字華容道の実現(ステージ選択可能)を作成します。
試合が必要なので、こちらでデジタル華容道を作りました。中には3、4、5段階の数字華容道が含まれています。最初はシェルウィンドウで選択して、乱数モジュールとグラフィカルユーザーインターフェースを使いました。
以下はプログラムの完全コードです。
三階の華容道に勝ちを加えます。
四階の華容道に勝利を加えます。
5階はパスしません。全部同じです。
次にコードの原理を説明します。
まずこの段落を見てください。
続いて、時間が限られているので、シェルウィンドウの中から何階級の数字華容道のバージョンを選んだだけです。数字を入力して判定します。他は全部簡単なゲームです。例えば、ボタンの作成、関数の呼び出しとループの入れ子、コードの注釈に大体の意味が書いてあります。
ここでPython数字華容道の実現に関する文章を紹介します。Python数字華容道の内容については以前の文章を検索してください。または下記の関連記事を引き続きご覧ください。これからもよろしくお願いします。
以下はプログラムの完全コードです。
# coding:utf-8 #
"""
#============================================================
:@Qss
2021 3 20
2021 3 21
2021 3 23
2021 3 31 ( )
: ,
2021 4 1 , bug
#============================================================
"""
from random import * #
from tkinter import * #
step_number = 0 # , 0
difficulty = int(input(' (3/4/5):'))
def Button_Click_1(x,y): #
""" """
global row_of_space
global col_of_space
global step_number
""" , """
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #
label_step_number['text'] = ' :' + str(step_number) # label
""" """
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(3):
for col in range(3):
""" , """
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
""" """
label_welcomes['text'] = ' '
def Button_Click_2(x,y): #
""" """
global row_of_space
global col_of_space
global step_number
""" , """
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #
label_step_number['text'] = ' :' + str(step_number) # label
""" """
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(4):
for col in range(4):
""" , """
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
""" """
label_welcomes['text'] = ' '
def Button_Click_3(x,y): #
""" """
global row_of_space
global col_of_space
global step_number
""" , """
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #
label_step_number['text'] = ' :' + str(step_number) # label
""" """
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(5):
for col in range(5):
""" , """
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
""" """
label_welcomes['text'] = ' '
""" """
root = Tk() #
root.title(' ') #
root.geometry("400x400") # 400 400
root.configure(bg = 'black') #
root.resizable(width = False,height = False) #
""" label """
label_welcomes = Label(root,text = ' ',bg = 'black',fg = 'red',font = ("Arial",13)) # label
label_welcomes.place(x = 20,y = 10,width = 250,height = 40) # label
""" label """
label_operation = Label(root,text = ' ',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = ' ',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
""" label """
label_step_number = Label(root,text = ' :' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)
if difficulty == 3:
root.attributes("-topmost", True)
row_of_space = 0 #
col_of_space = 0 #
buttons = {} #
numbers = ['1','2','3','4','5','6','7','8',' '] #
shuffle(numbers) #
""" """
for row in range(3):
for col in range(3):
""" , """
button = Button(root,command = lambda x = row,y = col:Button_Click_1(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #
button['text'] = numbers.pop() #
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #
if button['text'] == ' ': # , ,
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8',' '] #
root.mainloop() #
elif difficulty == 4:
root.attributes("-topmost", True)
row_of_space = 0 #
col_of_space = 0 #
buttons = {} #
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15',' '] #
shuffle(numbers) #
""" """
for row in range(4):
for col in range(4):
""" , """
button = Button(root,command = lambda x = row,y = col:Button_Click_2(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #
button['text'] = numbers.pop() #
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #
if button['text'] == ' ': # , ,
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15',' '] #
root.mainloop() #
elif difficulty == 5:
root.attributes("-topmost", True)
row_of_space = 0 #
col_of_space = 0 #
buttons = {} #
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',' '] #
shuffle(numbers) #
for row in range(5):
for col in range(5):
""" , """
button = Button(root,command = lambda x = row,y = col:Button_Click_3(x,y),bg = 'black',fg = 'green',font = ("Arial",35))
buttons[row,col] = button #
button['text'] = numbers.pop() #
button.place(x = 60 + col * 60,y = 60 + row * 60,width = 50,height = 50) #
if button['text'] == ' ': # , ,
row_of_space = row
col_of_space = col
numbers = ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24',' '] #
root.mainloop() #
else:
print(' ')
プログラム運転の結果は以下の通りです。三階の華容道に勝ちを加えます。
四階の華容道に勝利を加えます。
5階はパスしません。全部同じです。
次にコードの原理を説明します。
まずこの段落を見てください。
def Button_Click_1(x,y): #
""" """
global row_of_space
global col_of_space
global step_number
""" , """
if abs(x-row_of_space) + abs(y-col_of_space) == 1:
step_number += 1 #
label_step_number['text'] = ' :' + str(step_number) # label
""" """
buttons[row_of_space,col_of_space]['text'] = buttons[x,y]['text']
buttons[x,y]['text'] = ' '
row_of_space = x
col_of_space = y
n = 0
for row in range(3):
for col in range(3):
""" , """
if buttons[row,col]['text'] != numbers[n]:
return
n += 1
""" """
label_welcomes['text'] = ' '
この段落は三つのボタンをカスタマイズしたクリックイベント関数ですが、三つとも同じぐらいで、ルージュの後ろの数字が変わりました。この関数はコメントに書いてありますが、ボタンの横に空白のボタンがあるかどうかを判断するものです。一連の数字とマトリックスの一つ一つが対応しているかどうかをループネストで照らし合わせ、対応しないとループが飛び出し、対応するとゲームの勝ちと判定されます。
""" """
root = Tk() #
root.title(' ') #
root.geometry("400x400") # 300 300
root.configure(bg = 'black') #
root.resizable(width = False,height = False) #
""" label """
label_welcomes = Label(root,text = ' ',bg = 'black',fg = 'red',font = ("Arial",13)) # label
label_welcomes.place(x = 20,y = 10,width = 250,height = 40) # label
""" label """
label_operation = Label(root,text = ' ',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation.place(x = 3,y = 40,width = 50,height = 30)
label_operation_2 = Label(root,text = ' ',bg = 'black',fg = 'white',font = ("Arial",10))
label_operation_2.place(x = 3,y = 60,width = 50,height = 30)
""" label """
label_step_number = Label(root,text = ' :' + str(step_number),bg = 'black',fg = 'yellow',font = ("Arial",10))
label_step_number.place(x = 3,y = 20,width = 50,height = 30)
このセクションはグラフィカルユーザーインターフェースを作成しました。具体的に各ラインのコードは何をするかのコメントをはっきり表示しました。続いて、時間が限られているので、シェルウィンドウの中から何階級の数字華容道のバージョンを選んだだけです。数字を入力して判定します。他は全部簡単なゲームです。例えば、ボタンの作成、関数の呼び出しとループの入れ子、コードの注釈に大体の意味が書いてあります。
ここでPython数字華容道の実現に関する文章を紹介します。Python数字華容道の内容については以前の文章を検索してください。または下記の関連記事を引き続きご覧ください。これからもよろしくお願いします。