Lights-Out looks deceptively simple: tap a tile, and it flips itself along with its orthogonal neighbors. Turn all the tiles on (or off) to clear the board.

But if you build a 500-stage Lights-Out game with custom board shapes, locked tiles, and diagonal toggle tiles — like Glass Lights on 2Day Games — two hard questions immediately show up:

  1. How do you guarantee a board is actually solvable?
  2. How do you show the player the exact mathematical minimum number of clicks (Par)?

The answer is one of the cleanest applications of linear algebra in game development: Gaussian elimination over GF(2).

Modeling the Board as a System of Linear Equations

Every tile press is its own inverse — pressing a tile twice is identical to not pressing it at all (1 + 1 = 0 mod 2), and the order of presses does not matter.

That means an N×N board with M active (unlocked) tiles is simply a linear system over the binary field GF(2):

$$A \cdot x = b \pmod 2$$

  • A is an M×M binary toggle matrix where A[i][j] = 1 if pressing tile j flips tile i (easily supporting orthogonal +, diagonal ×, or custom patterns).
  • b is the target delta vector (1 if tile i needs to change state, 0 otherwise).
  • x is the binary solution vector (1 = press this tile once, 0 = leave it alone).

Finding the True Minimum Clicks (Nullspace Search)

Here is the subtle catch many implementations miss: for certain board sizes (famously 5×5 Lights-Out!), the matrix A is not invertible — its nullspace has dimension 2 (4 distinct valid solutions for the same board!).

If you stop at the first solution Gaussian elimination hands you, your Par number might be 11 clicks when a 7-click solution exists!

To compute the exact minimum Par for every stage in Glass Lights:

  1. Run RREF (Reduced Row Echelon Form) over GF(2) using fast bitmask XOR operations.
  2. Extract one particular solution x_0 and all nullspace basis vectors v_1, v_2, ..., v_k.
  3. Iterate over all 2^k linear combinations of the nullspace basis (k is tiny, typically 0 to 4) and pick the solution vector with the minimum Hamming weight (fewest 1 bits).

That Hamming weight is the mathematically provable minimum Par!

Putting It Into Live Gameplay

In Glass Lights, every stage from 3×3 warmups up to 6×6 boards with locked cells and diagonal tiles carries its pre-verified Par value, displayed live as Clicks N / Par M right above the board.

Give it a try in your browser and see if you can match (or beat your own record toward) mathematical Par: