質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

662閲覧

マインスイーパー、周辺マスの開け方

suu_TCG

総合スコア4

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2019/12/16 19:29

前提・実現したいこと

PythonのTkinterにてマインスイーパ-を作っています。
その中で”開けたマスの周辺に爆弾がない場合、周辺8マスを空ける”
といったコードを記述したいのですが、上手くできません。
Def left_click上で横マスの<1>を起動できれば行けると思うのですが、、、
皆さん、教えてください。。。

該当のソースコード

Python

1from tkinter import * 2import random 3import sys 4from tkinter import messagebox 5root = Tk() 6root.title("mine sweeper") 7root.resizable(0,0) 8 9 10menu_ROOT = Menu(root) 11root.configure(menu = menu_ROOT) 12 13menu_Game = Menu(menu_ROOT, tearoff= False) 14 15menu_ROOT.add_cascade(label = "Game(G)",under=4,menu=menu_Game) 16 17menu_Game.add_command(label="Beginner(B)",under=3) 18menu_Game.add_command(label="Regular(R)",under=3) 19menu_Game.add_command(label="Hard(H)",under=3) 20 21menu_ROOT.add_command(label="End(X)",under=3) 22 23root_frame = Frame(root, relief = "groove", borderwidth = 5, bg = 'Lightgray') 24status_frame = Frame(root_frame, width = 300, height = 50, relief = 'sunken', borderwidth = 3, bg = 'LightGray') 25game_frame = Frame(root_frame, width = 300, height = 300, relief = 'sunken', borderwidth = 3, bg = 'Lightgray') 26 27root_frame.pack() 28root_frame.pack() 29status_frame.pack(pady = 5, padx = 5, fill="x") 30 31game_frame.pack(pady = 5, padx = 5) 32 33bomb_list = [] 34 35 36openct = 0 37ct = Label(status_frame, text = openct) 38ct.pack(fill = 'x', padx = 30, side = 'left') 39 40EndB=Button(status_frame, text="Quit", command= quit) 41EndB.pack(fill = 'x', padx = 30, side = 'left') 42 43bmct = 10 44bt= Label(status_frame, text = bmct) 45bt.pack(fill = 'x', padx = 30, side = 'left') 46 47 48 49def left_click(event): 50 global openct 51 52 53 event.widget.configure(relief = 'ridge', bd = '1') 54 except_num = event.widget.num 55 print(event.widget.num) 56 57 if len(bomb_list) == 0: 58 while len(bomb_list) != 10: 59 bomb_num = random.randint(0,len(frame_list)) 60 if bomb_num != except_num and (bomb_num in bomb_list) == False: 61 bomb_list.append(bomb_num) 62 63 bomb_count = search_bomb(bomb_list,event.widget.num) 64 #ボムに当たった場合 65 if bomb_count == 9: 66 print('Game Over') 67 for i in bomb_list: 68 frame_list[i].configure(bg = 'red') 69 for i in frame_list: 70 i.bind("<1>", stop) 71 messagebox.showerror("Hit!", "GAME OVER") 72 #周辺マスにボムなし 73 elif bomb_count == 0: 74 bomb_count_label = Label(event.widget, text = bomb_count, bg = 'LightGray') 75 bomb_count_label.place(width = 30, height = 30) 76 event.widget.bind("<1>", stop) 77 openct += 1 78 #ここに周辺8マスを開けるコマンドを入力する 79 #それ以外 80 else: 81 bomb_count_label = Label(event.widget, text = bomb_count, bg = 'LightGray') 82 bomb_count_label.place(width = 30, height = 30) 83 event.widget.bind("<1>", stop) 84 openct += 1 85 86 ct["text"] = str(openct) 87 88 if openct == (len(frame_list) - len(bomb_list)): 89 messagebox.showinfo("Congragelation!","Game clear!") 90 91 for i in frame_list: 92 i.bind("<1>", stop) 93 94 95 96def right_click(event): 97 global bmct 98 99 except_num = event.widget.num 100 101 frame_list[event.widget.num].configure(bg = 'blue') 102 bmct -=1 103 bt["text"] = str(bmct) 104 105def quit(): 106 global root 107 root.quit() 108 109def count(): 110 open_count += 1 111 112 return open_count 113 114 115def search_bomb(list, num): 116 around_list = [] 117 bomb_count = 0 118 if num in list: 119 return 9 120 if num %9 == 0: 121 around_list.append(num-9) 122 around_list.append(num-8) 123 around_list.append(num+1) 124 around_list.append(num+9) 125 around_list.append(num+10) 126 elif num %9 == 8: 127 around_list.append(num-10) 128 around_list.append(num-9) 129 around_list.append(num-1) 130 around_list.append(num+8) 131 around_list.append(num+9) 132 elif num < 9: 133 around_list.append(num-1) 134 around_list.append(num+1) 135 around_list.append(num+8) 136 around_list.append(num+9) 137 around_list.append(num+10) 138 elif num > 72: 139 around_list.append(num-10) 140 around_list.append(num-9) 141 around_list.append(num-8) 142 around_list.append(num-1) 143 around_list.append(num+1) 144 else: 145 around_list.append(num-10) 146 around_list.append(num-9) 147 around_list.append(num-8) 148 around_list.append(num-1) 149 around_list.append(num+1) 150 around_list.append(num+8) 151 around_list.append(num+9) 152 around_list.append(num+10) 153 for i in around_list: 154 if i in list: 155 bomb_count += 1 156 return bomb_count 157 158 159i=0 160frame_list = [] 161 162for x in range(9): 163 for y in range(9): 164 frame = Frame(game_frame, width = 30, height = 30, bd = 3, relief = 'raised', bg = 'LightGray') 165 frame.bind("<1>", left_click) 166 frame.bind("<3>", right_click) 167 frame.num = i 168 frame_list.append(frame) 169 frame.grid(row=x, column=y) 170 i += 1 171 172def stop(event): 173 pass 174 175while True: 176 root.mainloop() 177

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/12/16 23:09

うまく出来ないとはどうなるんでしょうか
guest

回答1

0

横マスの<1>を起動できれば行けると思うのですが

python

1#ここに周辺8マスを開けるコマンドを入力する 2for idx in around_list: 3 frame_list[idx].event_generate('<1>')

でいかがでしょう。idx は対象のマスの添字です。

データ構造がリスト(1次元)なので、壁際のマスへの配慮が必要ですが、
search_bomb 関数内の around_list を生成する部分を別関数に括り出し、
再利用する等して工夫してみてください。

投稿2019/12/18 21:40

teamikl

総合スコア8664

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問