-
Notifications
You must be signed in to change notification settings - Fork 0
/
lights_out.py
256 lines (209 loc) · 9.02 KB
/
lights_out.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import pygame as pg
import sys
import random
import numpy as np
pg.init()
screen_width = 500
screen_height = 600
bg_rect_width = int(screen_width * (5/6))
bg_rect_height = int(screen_width * (5/6))
screen = pg.display.set_mode((screen_width, screen_height))
title_font = pg.font.SysFont("Verdana", 60)
game_font = pg.font.SysFont("Verdana", 15)
pg.display.set_caption("Lights Out")
class Game:
def __init__(self, n):
self.size = n
self.board = []
for _ in range(n):
to_add = []
for _ in range(n):
to_add.append(False)
self.board.append(to_add)
self.solution_vector = [0] * (n * n)
@staticmethod
def _draw_title():
title = title_font.render("Lights Out", True, (0, 0, 0))
title_rect = title.get_rect(center=(screen_width // 2, screen_height // 12))
screen.blit(title, title_rect)
@staticmethod
def draw_buttons():
bg_rect = pg.Rect(0, 0, bg_rect_width, bg_rect_height)
bg_rect.center = (screen_width // 2, screen_height // 2)
restart_rect = pg.Rect(bg_rect.topleft[0] - 5, bg_rect.bottomleft[1] + 20, 100, 50)
restart_word = game_font.render("Reset Game", True, (0, 0, 0))
restart_word_rect = restart_word.get_rect(center=restart_rect.center)
pg.draw.rect(screen, (255, 255, 255), restart_rect)
screen.blit(restart_word, restart_word_rect)
reference_x = restart_rect.topleft[0] + 115
reference_y = bg_rect.bottomleft[1] + 20
board_size_rects = []
for i in range(4, 9):
board_size_rect = pg.Rect(reference_x, reference_y, 50, 50)
board_size_word = game_font.render(f"{i} x {i}", True, (0, 0, 0))
board_size_word_rect = board_size_word.get_rect(center=board_size_rect.center)
pg.draw.rect(screen, (255, 255, 255), board_size_rect)
screen.blit(board_size_word, board_size_word_rect)
reference_x += 65
board_size_rects.append(board_size_rect)
return restart_rect, board_size_rects
def _draw_board(self):
# draw background
bg_rect = pg.Rect(0, 0, bg_rect_width, bg_rect_height)
bg_rect.center = (screen_width // 2, screen_height // 2)
pg.draw.rect(screen, (0, 0, 0), bg_rect)
# draw board
for i, row in enumerate(self.board):
y_offset = (bg_rect_height // self.size) * i
x_offset = 0
square_rect_width = bg_rect_width // self.size
square_rect_height = bg_rect_height // self.size
for j, square in enumerate(row):
grid_pos = (self.size * i) + j
square_rect = pg.Rect(0, 0, square_rect_width, square_rect_height)
square_rect.topleft = bg_rect.topleft
if square:
square_rect.x += x_offset
square_rect.y += y_offset
pg.draw.rect(screen, (0, 0, 150), square_rect, 1)
pg.draw.circle(screen, (150, 0, 0), square_rect.center, square_rect_width // 2.5)
if self.solution_vector[grid_pos]:
hint_rect = square_rect.center
pg.draw.circle(screen, (0, 223, 0), hint_rect, square_rect_width // 10)
else:
square_rect.x += x_offset
square_rect.y += y_offset
pg.draw.rect(screen, (0, 0, 150), square_rect, 1)
if self.solution_vector[grid_pos]:
hint_rect = square_rect.center
pg.draw.circle(screen, (0, 223, 0), hint_rect, square_rect_width // 10)
x_offset += square_rect_width
def draw_game(self):
self._draw_title()
self._draw_board()
self.draw_buttons()
def change_lights(self, row, col):
self.board[row][col] = not self.board[row][col]
grid_pos = (self.size * row) + col
if row < self.size - 1:
self.board[row + 1][col] = not self.board[row + 1][col]
if row > 0:
self.board[row - 1][col] = not self.board[row - 1][col]
if col < self.size - 1:
self.board[row][col + 1] = not self.board[row][col + 1]
if col > 0:
self.board[row][col - 1] = not self.board[row][col - 1]
self.solution_vector[grid_pos] = 0
def update_board(self, x, y):
bg_rect = pg.Rect(0, 0, bg_rect_width, bg_rect_height)
bg_rect.center = (screen_width // 2, screen_height // 2)
board_x = (x - bg_rect.topleft[0]) // (bg_rect_width // self.size)
board_y = (y - bg_rect.topleft[1]) // (bg_rect_height // self.size)
if (board_x >= 0) and (board_x < self.size) and (board_y >= 0) and (board_y < self.size):
self.change_lights(board_y, board_x)
def start(self):
self.solution_vector = [0] * (self.size * self.size)
for row in range(self.size):
for col in range(self.size):
switch = random.randint(0, 100) < 35
if switch:
self.change_lights(row, col)
def check_win(self):
for row in self.board:
if sum(row) > 0:
return False
return True
def _get_move_matrix(self):
move_matrix = []
n = self.size
for _ in range(n * n):
to_add = [0] * n * n
move_matrix.append(to_add)
for col in range(n * n):
for row in range(n * n):
if row == col:
move_matrix[row][col] = 1
if row + 1 < n * n and col % n != n - 1:
move_matrix[row + 1][col] = 1
if row - 1 >= 0 and col % n != 0:
move_matrix[row - 1][col] = 1
if row + n < n * n:
move_matrix[row + n][col] = 1
if row - n >= 0:
move_matrix[row - n][col] = 1
return np.array(move_matrix)
def solve(self):
def gauss_elim(A):
rows_left = list(range(len(A)))
new_rowlist = []
for col_idx in range(len(A)):
# among rows left, list of row-labels whose rows have a nonzero in position col_idx
rows_with_nonzero = [row_idx for row_idx in rows_left if A[row_idx][col_idx]]
if rows_with_nonzero:
pivot_idx = rows_with_nonzero[0]
rows_left.remove(pivot_idx)
new_rowlist.append(A[pivot_idx])
for row_idx in rows_with_nonzero[1:]:
A[row_idx] -= (A[row_idx][col_idx] // A[pivot_idx][col_idx]) * A[pivot_idx]
A[row_idx] = A[row_idx] % 2
if rows_left:
[new_rowlist.append(A[row]) for row in rows_left]
return np.array(new_rowlist)
def triangular_solve_n(row_list, b):
x = np.zeros(len(row_list))
for i in reversed(range(len(row_list))):
dot_prod = np.dot(row_list[i], x)
x[i] = (b[i] - dot_prod) % 2
return x
curr_board = [[num] for row in self.board for num in row]
move_matrix = self._get_move_matrix()
move_matrix = np.append(move_matrix, curr_board, axis=1)
echelon_matrix = gauss_elim(move_matrix)
ans_col = echelon_matrix[:, -1]
echelon_matrix = np.delete(echelon_matrix, -1, 1)
moves = triangular_solve_n(echelon_matrix, ans_col)
return moves
def use_solution_vector(self):
for i in range(self.size):
for j in range(self.size):
grid_pos = (self.size * i) + j
if self.solution_vector[grid_pos]:
self.change_lights(i, j)
game = Game(4)
game.start()
win = False
now = 0
while True:
screen.fill((112, 128, 144))
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
elif event.type == pg.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pg.mouse.get_pos()
game.update_board(mouse_x, mouse_y)
restart_rect, size_rects = game.draw_buttons()
if restart_rect.collidepoint(mouse_x, mouse_y):
game.start()
board_size = 4
for rect in size_rects:
if rect.collidepoint(mouse_x, mouse_y) and game.size != board_size:
game = Game(board_size)
game.start()
board_size += 1
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
game.solution_vector = game.solve()
if event.key == pg.K_s:
game.use_solution_vector()
if game.check_win() and not win:
game.draw_game()
pg.display.update()
win = True
now = pg.time.get_ticks()
# I'm sure there's a better way to wait a few seconds after winning, but idk
if win and (pg.time.get_ticks() - now > 3000):
win = False
game.start()
game.draw_game()
pg.display.update()