I'm trying to solve the Lights Out puzzle. I use a two-dimensional list of Boolean values, such as [[True, True, True], [True, True, True], [True, True, True]]
, to represent the condition of a light.
I want to reverse the boolean value at a given location such as (1,1), and I code as:
class LightsOutPuzzle(object):
def __init__(self, board):
self.board = board
def get_board(self):
return self.board
def perform_move(self, row, col):
self.board[row][col] = not self.board[row][col]
print(self.board)
return LightsOutPuzzle(self.board)
I call perform_move(1,1) on
[[False, False, False], [False, False, False], [False, False, False]]
but the result is:
[[False, True, False], [False, True, False], [False, True, False]]
which makes me confused. Could you please tell me what's wrong with it? What is the right way to do this task? Thanks a lot for helping!
I'm using this function to generate new boards:
def create_puzzle(rows, cols):
c = []
r = []
for x in range(cols):
c.append(False)
for y in range(rows):
r.append(c)
return LightsOutPuzzle(r)
Does it cause the problem?
Thank you sooooo much for your replies!!! QWQ