-
Notifications
You must be signed in to change notification settings - Fork 8
/
lightsoutgui.py
108 lines (75 loc) · 2.84 KB
/
lightsoutgui.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# coding: utf-8
"""
GUI for the Light Out solver.
"""
import sys
import tkinter as tk
from itertools import product
import numpy as np
import lightsout
BUTTON_SIZE = 23
class ButtonGrid(tk.Frame, object):
def __init__(self, master, size, callback=None):
tk.Frame.__init__(self, master)
buttons = [[None]*size for i in range(size)]
for r,c in product(range(size), range(size)):
buttons[r][c] = tk.Label(self, bitmap="gray12", bg="red",
width=BUTTON_SIZE, height=BUTTON_SIZE)
buttons[r][c].grid(row=r, column=c, padx=2, pady=2)
for btn in sum(buttons, []):
btn.bind("<Button-1>", self.button_pressed)
self.buttons = buttons
self.callback = callback
self.size = size
def button_pressed(self, event):
if event.widget["bg"] == "red":
event.widget["bg"] = "green"
else:
event.widget["bg"] = "red"
if self.callback is not None:
self.callback(self.get_board())
def get_board(self):
# board = map(lambda x:x["bg"]=="green", sum(self.buttons, []))
board = [i["bg"] == "green" for i in sum(self.buttons, [])]
return np.reshape(board, (self.size, self.size))
def set_solution(self, solution):
if solution is None:
for btn in sum(self.buttons, []):
btn["bitmap"] = "gray12"
return
assert solution.shape[0]==solution.shape[1]==self.size
for r,c in product(range(self.size), range(self.size)):
if solution[r,c]:
self.buttons[r][c]["bitmap"] = "gray75"
else:
self.buttons[r][c]["bitmap"] = "gray12"
class App(tk.Frame):
def __init__(self, master, size=5):
tk.Frame.__init__(self, master, width=1000, height=1000)
self.pack(fill="both")
self.solver = lightsout.LightsOut(size)
self.buttongrid = ButtonGrid(self, size, callback=self.solve)
self.buttongrid.pack(padx=10, pady=10)
self.info = tk.Label(self, text="")
self.info.pack(side="bottom")
def solve(self, board):
if not self.solver.issolvable(board):
self.info["text"] = "Not solvable"
self.buttongrid.set_solution(None)
return
self.info["text"] = ""
self.buttongrid.set_solution(self.solver.solve(board))
def main():
size = 5
if len(sys.argv) > 1:
size = int(sys.argv[1])
else:
print("""Use
{} <board_size>
to indicate the board size (default=5)""".format(sys.argv[0]))
root = tk.Tk()
app = App(root, size)
app.master.title("Lights Out solver")
root.mainloop()
if __name__ == '__main__':
main()