A Lights Out Puzzle with Solver (JavaScript)

Blackout

How to play

BlueClick on one character and the adjacent ones will change too. Goal is to change them all to the same character in the fewest moves possible. I recommend playing on a larger screen.


Answers

How does it work?

In case of 6 × 5 grid and two colors, a bit vector x of length 30 can contain an answer. Each element of the vector corresponds to a field cell and its bit value expresses whether the cell should be pressed or not. A board state can also be stated as a bit vector b of length n = 30 where each element expresses whether the cell is lit or not.

Pressing a cell inverts the state of some cells, which is expressed as a 30-vector [a1j ... anj] where each bit aij contains whether the cell i is inverted or not by pressing the cell j. The whole effect of pressing the cells as specified by the answer vector x is determined by, for each cell i, whether the times of inversions at i is odd or even. This can be expressed as (ai1*x1 + ... + ain*xn) mod 2.

The puzzle is now stated as a problem to find an x such that

  (ai1*x1 + ... + ain*xn + bi) mod 2 = c

for all i with some final lightness c, or equivalently:

  (ai1*x1 + ... + ain*xn) mod 2 = (c - bi) mod 2

This can be solved with Gaussian elimination since mod 2 effectively distributes over anything.

---Using finite field linear algebra, this method is stated as follows:

  A x = c - b
	x = A^{-1}(c - b)

Although A^{-1} can be computed in advance for better performance, the script in this page computes A x = c - b every time.

Unknown Author

This is one of the projects I picked up to learn javascript. I do not know who the original author is, but if someone knows, please send me a message. The code was well documented and the game was fun. One day I will learn the inner workings of this game and write my own. I hope you have fun as well.

Some changes I made include changing the image name to id because the name attribute on the img element was obsolete. The old game also made use of tables, which I changed into div sections instead to make it more responsive. I broke up the game tiles and the answer tiles to two different functions createFieldTiles and createFieldAns so they could be called seperately. The old javascript also needed some security updates which I don't really understand, but I tried my best to rewrite those parts without breaking the code. I replaced the images with my own graphics and changed some of the text around to match the new theme.