Merge pull request #6554 from PySimpleGUI/Dev-latest
Fixed numpy deprecated problems. Checking in improvement made long …
This commit is contained in:
commit
79e9b6198e
|
@ -2,7 +2,6 @@ import PySimpleGUI as sg, random
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from typing import List, Any, Union, Tuple, Dict
|
from typing import List, Any, Union, Tuple, Dict
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Sudoku Puzzle Demo
|
Sudoku Puzzle Demo
|
||||||
|
|
||||||
|
@ -27,7 +26,7 @@ def generate_sudoku(mask_rate):
|
||||||
"""
|
"""
|
||||||
while True:
|
while True:
|
||||||
n = 9
|
n = 9
|
||||||
solution = np.zeros((n, n), np.int)
|
solution = np.zeros((n, n), np.int_)
|
||||||
rg = np.arange(1, n + 1)
|
rg = np.arange(1, n + 1)
|
||||||
solution[0, :] = np.random.choice(rg, n, replace=False)
|
solution[0, :] = np.random.choice(rg, n, replace=False)
|
||||||
try:
|
try:
|
||||||
|
@ -39,7 +38,8 @@ def generate_sudoku(mask_rate):
|
||||||
sub_r, sub_c = r // 3, c // 3
|
sub_r, sub_c = r // 3, c // 3
|
||||||
avb2 = np.setdiff1d(np.arange(0, n + 1), solution[sub_r * 3:(sub_r + 1) * 3, sub_c * 3:(sub_c + 1) * 3].ravel())
|
avb2 = np.setdiff1d(np.arange(0, n + 1), solution[sub_r * 3:(sub_r + 1) * 3, sub_c * 3:(sub_c + 1) * 3].ravel())
|
||||||
avb = np.intersect1d(avb1, avb2)
|
avb = np.intersect1d(avb1, avb2)
|
||||||
solution[r, c] = np.random.choice(avb, size=1)
|
# solution[r, c] = np.random.choice(avb, size=1)
|
||||||
|
solution[r, c] = np.random.choice(avb, size=1)[0]
|
||||||
break
|
break
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
@ -48,7 +48,6 @@ def generate_sudoku(mask_rate):
|
||||||
return puzzle, solution
|
return puzzle, solution
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def check_progress(window, solution):
|
def check_progress(window, solution):
|
||||||
"""
|
"""
|
||||||
Gives you a visual hint on your progress.
|
Gives you a visual hint on your progress.
|
||||||
|
@ -82,9 +81,17 @@ def check_progress(window, solution):
|
||||||
return solved
|
return solved
|
||||||
|
|
||||||
|
|
||||||
def create_and_show_puzzle(window):
|
def main(mask_rate=0.7):
|
||||||
|
""""
|
||||||
|
The Main GUI - It does it all.
|
||||||
|
|
||||||
|
The "Board" is a grid that's 9 x 9. Even though the layout is a grid of 9 Frames, the
|
||||||
|
addressing of the individual squares is via a key that's a tuple (0,0) to (8,8)
|
||||||
|
"""
|
||||||
|
|
||||||
|
def create_and_show_puzzle():
|
||||||
# create and display a puzzle by updating the Input elements
|
# create and display a puzzle by updating the Input elements
|
||||||
rate = DEFAULT_MASK_RATE
|
rate = mask_rate
|
||||||
if window['-RATE-'].get():
|
if window['-RATE-'].get():
|
||||||
try:
|
try:
|
||||||
rate = float(window['-RATE-'].get())
|
rate = float(window['-RATE-'].get())
|
||||||
|
@ -96,34 +103,24 @@ def create_and_show_puzzle(window):
|
||||||
window[r, c].update(puzzle[r][c] if puzzle[r][c] else '', background_color=sg.theme_input_background_color())
|
window[r, c].update(puzzle[r][c] if puzzle[r][c] else '', background_color=sg.theme_input_background_color())
|
||||||
return puzzle, solution
|
return puzzle, solution
|
||||||
|
|
||||||
|
|
||||||
def main(mask_rate=0.7):
|
|
||||||
""""
|
|
||||||
The Main GUI - It does it all.
|
|
||||||
|
|
||||||
The "Board" is a grid that's 9 x 9. Even though the layout is a grid of 9 Frames, the
|
|
||||||
addressing of the individual squares is via a key that's a tuple (0,0) to (8,8)
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# It's 1 line of code to make a Sudoku board. If you don't like it, then replace it.
|
# It's 1 line of code to make a Sudoku board. If you don't like it, then replace it.
|
||||||
# Dude (Dudette), it's 1-line of code. If you don't like the board, write a line of code.
|
# Dude (Dudette), it's 1-line of code. If you don't like the board, write a line of code.
|
||||||
# The keys for the inputs are tuples (0-8, 0-8) that reference each Input Element.
|
# The keys for the inputs are tuples (0-8, 0-8) that reference each Input Element.
|
||||||
# Get an input element for a position using: window[row, col]
|
# Get an input element for a position using: window[row, col]
|
||||||
# To get a better understanding, take it apart. Spread it out. You'll learn in the process.
|
# To get a better understanding, take it apart. Spread it out. You'll learn in the process.
|
||||||
window = sg.Window('Sudoku',
|
window = sg.Window('Sudoku',
|
||||||
[[sg.Frame('', [[sg.I(random.randint(1,9), justification='r', size=(3,1),enable_events=True, key=(fr*3+r,fc*3+c)) for c in range(3)] for r in range(3)]) for fc in range(3)] for fr in range(3)] +
|
[[sg.Frame('', [[sg.I(random.randint(1, 9), justification='r', size=(3, 1), key=(fr * 3 + r, fc * 3 + c)) for c in range(3)] for r in range(3)]) for fc in
|
||||||
[[sg.B('Solve'), sg.B('Check'), sg.B('Hint'), sg.B('New Game'), sg.T('Mask rate (0-1)'), sg.In(str(mask_rate), size=(3,1),key='-RATE-')],], finalize=True)
|
range(3)] for fr in range(3)] +
|
||||||
|
[[sg.B('Solve'), sg.B('Check'), sg.B('Hint'), sg.B('New Game')], [sg.T('Mask rate (0-1)'), sg.In(str(mask_rate), size=(3, 1), key='-RATE-')], ],
|
||||||
|
finalize=True)
|
||||||
|
|
||||||
# create and display a puzzle by updating the Input elements
|
# create and display a puzzle by updating the Input elements
|
||||||
|
|
||||||
puzzle, solution = create_and_show_puzzle(window)
|
puzzle, solution = create_and_show_puzzle()
|
||||||
check_showing = False
|
|
||||||
while True: # The Event Loop
|
while True: # The Event Loop
|
||||||
event, values = window.read()
|
event, values = window.read()
|
||||||
if event == sg.WIN_CLOSED:
|
if event is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
if event == 'Solve':
|
if event == 'Solve':
|
||||||
|
@ -131,7 +128,6 @@ def main(mask_rate=0.7):
|
||||||
for c, col in enumerate(row):
|
for c, col in enumerate(row):
|
||||||
window[r, c].update(solution[r][c], background_color=sg.theme_input_background_color())
|
window[r, c].update(solution[r][c], background_color=sg.theme_input_background_color())
|
||||||
elif event == 'Check':
|
elif event == 'Check':
|
||||||
check_showing = True
|
|
||||||
solved = check_progress(window, solution)
|
solved = check_progress(window, solution)
|
||||||
if solved:
|
if solved:
|
||||||
sg.popup('Solved! You have solved the puzzle correctly.')
|
sg.popup('Solved! You have solved the puzzle correctly.')
|
||||||
|
@ -142,15 +138,12 @@ def main(mask_rate=0.7):
|
||||||
except:
|
except:
|
||||||
pass # Likely because an input element didn't have focus
|
pass # Likely because an input element didn't have focus
|
||||||
elif event == 'New Game':
|
elif event == 'New Game':
|
||||||
puzzle, solution = create_and_show_puzzle(window)
|
puzzle, solution = create_and_show_puzzle()
|
||||||
elif check_showing: # an input was changed, so clear any background colors from prior hints
|
|
||||||
check_showing = False
|
|
||||||
for r, row in enumerate(solution):
|
|
||||||
for c, col in enumerate(row):
|
|
||||||
window[r, c].update(background_color=sg.theme_input_background_color())
|
|
||||||
window.close()
|
window.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
DEFAULT_MASK_RATE = 0.7 # % Of cells to hide
|
if __name__ == "__main__":
|
||||||
main(DEFAULT_MASK_RATE)
|
mask_rate = 0.7 # % Of cells to hide
|
||||||
|
main(mask_rate)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue