Skip to content

Instantly share code, notes, and snippets.

@MikeTheWatchGuy
Last active November 26, 2018 15:36
Show Gist options
  • Select an option

  • Save MikeTheWatchGuy/1976a9696e01c7d4d736145b6a2ed774 to your computer and use it in GitHub Desktop.

Select an option

Save MikeTheWatchGuy/1976a9696e01c7d4d736145b6a2ed774 to your computer and use it in GitHub Desktop.
Mastermind Game
import PySimpleGUI as sg
import random
frames = []
for i in range(6): # make 6 rows of frames
row = []
for j in range(4): # make the 4 colored buttons
row.append(sg.Button('', size=(1,1), key=(i,j), button_color=('black', 'lightblue')))
# make the hint area and decode button
row.append(sg.Text('', size=(5,1), background_color='yellow', key='_HINT_'+str(i)))
row.append(sg.Button('Decode', key='_DECODE_'+str(i)))
# create frame with everything inside
frames.append([sg.Frame('Attempt %s'%i, [row], title_color='Blue'),])
# create solution frame with buttons inside
solution_gui = [sg.Frame('Solution',[[sg.Button(size=(1,1), key='_SOL_'+str(i)) for i in range(4)]]), sg.Button('I Give Up')]
# the window layout is the frames and the solution frame
layout = [*frames,
solution_gui]
window = sg.Window('Window Title', use_default_focus=False).Layout(layout)
button_colors = ('white', 'blue', 'red', 'yellow', 'green', 'purple')
button_values = [[0,0,0,0]]*6
solution = [random.randint(0,5) for _ in range(4)]
print(solution)
while True: # Event Loop
event, values = window.Read()
print(event, values)
if event is None or event == 'Exit':
break
# If clicked on one of the colored buttons
if type(event) is tuple:
cur_color = button_values[event[0]][event[1]]
cur_color = cur_color+1 if cur_color+1 < len(button_colors) else 0
button_values[event[0]][event[1]] = cur_color
window.Element(event).Update(button_color=('white', button_colors[cur_color]))
# if clicked on the decode button
elif event.startswith('_DECODE_'):
row_num = int(event[-1:])
# TODO add code here to compare button choices to solution
# display the hint
window.Element('_HINT_'+str(row_num)).Update('*!&-')
elif event == 'I Give Up':
for i in range(4):
window.Element('_SOL_'+str(i)).Update(button_color=('white', button_colors[solution[i]]))
window.Close()
@MikeTheWatchGuy
Copy link
Copy Markdown
Author

matermind

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment