1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| import PySimpleGUI as sg from random import randint import numpy as np from scipy import signal MAX_ROWS = MAX_COL = 10 board = [[randint(0,1) for j in range(MAX_COL)] for i in range(MAX_ROWS)] board = np.zeros(MAX_ROWS*MAX_COL).astype(np.int) idx = np.random.choice(len(board), 3, replace=False) board[idx] = 1 board = board.reshape(MAX_ROWS, MAX_COL).tolist() filter = np.array([ [1,1,1], [1,0,1], [1,1,1] ]) counter = signal.convolve2d(board, filter, 'full') board = counter[1:-1,1:-1] - board print (board) board = board.tolist() layout = [[sg.Button('?', size=(4, 2), key=(i,j), pad=(0,0)) for j in range(MAX_COL)] for i in range(MAX_ROWS)] window = sg.Window('Minesweeper', layout) while True: event, values = window.read() print (event,values) if event in (None, 'Exit'): break select = sg.popup('マスに対する操作を選択してください',custom_text=('open', 'flag'),title='') print('select', select) if select is 'open': window[event].update(board[event[0]][event[1]], button_color=('white','black')) if board[event[0]][event[1]]==-1: sg.popup("You Lose") break elif select is 'flag': state = window[event].GetText() if state == '?': window[event].update('F', button_color=('red',None)) elif state == 'F': window[event].update('?', button_color=('white',None)) window.close()
|