Skip to main content

Python Lights Out

Static Badge License

A Python package for generating and solving Lights Out puzzles.

Lights Out (Game)

The game consists of a 5 by 5 grid of lights.

  • When the game starts, a random number of these lights is switched on.
  • Pressing any of the lights will toggle it and the adjacent lights.

The goal of the puzzle is to switch all the lights off, preferably with as few button presses as possible.

Source: Wikipedia – Lights Out (game)

Example

For simplicity, use a 3×3 grid as an example.

   0 1 2                   0 1 2                   0 1 2                   0 1 2
0  . O .                0  O . O                0  . . O                0  . . .
1  O O O  —— (0, 1) —→  1  O . O  —— (1, 0) —→  1  . O O  —— (1, 2) —→  1  . . .  (complete)
2  O . O                2  O . O                2  . . O                2  . . .

Installation

This package can be installed in two ways:

  1. Install directly from this repository.
  2. Install via PyPI.

Install from the repository

$ git clone https://github.com/MingMinNa/Python-LightsOut.git
$ cd Python-LightsOut
$ pip install .

Install via PyPI

$ pip install python-lightsout

Then, run the following code to verify the installation:

import lightsout
print(lightsout.__version__)

Usage

Here are some simple usage examples.

Create a Board

from lightsout.board import Board, ON, OFF

# Method 1: Create an empty 5x5 board with all lights off
board = Board(5, 5)
print(board, "\n")

# You can also provide a 1D grid (size must be height * width)
board = Board(3, 3, [
    OFF, ON , OFF,
    ON , OFF, ON ,
    OFF, ON , OFF,
])
print(board, "\n")

# Method 2: Create a board from a 2D grid.
# It will be automatically converted to an internal 1D grid.
board = Board.from_2d_grid([
    [OFF, ON , OFF],
    [ON , OFF, ON ],
    [OFF, ON , OFF],
])
print(board, "\n")

# Press the light at (row, col).
# The selected light and its adjacent lights are toggled.
board.press(1, 1)
print(board, "\n")
Output
   0 1 2 3 4
0  . . . . .
1  . . . . .
2  . . . . .
3  . . . . .
4  . . . . . 

   0 1 2
0  . O .
1  O . O
2  . O . 

   0 1 2
0  . O .
1  O . O
2  . O . 

   0 1 2
0  . . .
1  . O .
2  . . . 

Generate a puzzle

from lightsout.generator import Generator

# Create a 5x3 generator.
# random_seed can be omitted if not needed.
generator = Generator(5, 3, random_seed=42)

# Get the current puzzle.
# A copy is returned, so the internal state will not be affected.
puzzle = generator.get_puzzle()
print(puzzle, "\n")

# Generate a new puzzle
generator.regenerate()
puzzle = generator.get_puzzle()
print(puzzle)
Output
   0 1 2
0  . . .
1  O . .
2  . O O
3  . O .
4  . . O 

   0 1 2
0  . O O
1  O O O
2  O O O
3  O . O
4  . . O

Find a solution

from lightsout import Board, Generator, Solver

puzzle = Generator(3, 3).get_puzzle()
solver = Solver(puzzle)
print(puzzle, "\n")

# Check if the puzzle is solvable
if solver.is_solvable():
    solution = solver.solve()  # Returns tuple[Position]
    print("Press the following positions in order:\n")

    for pos in solution:
        print(f"Press ({pos.row}, {pos.col})")
        puzzle.press(pos.row, pos.col)
        print(puzzle, "\n")
else:
    print("This puzzle has no solution.")
Output
   0 1 2
0  . O O
1  O . .
2  O . . 

Press the following positions in order:

Press (0, 1)
   0 1 2
0  O . .
1  O O .
2  O . . 

Press (1, 0)
   0 1 2
0  . . .
1  . . .
2  . . . 

Notes

  • If the puzzle is unsolvable, calling solve() directly will raise an UnsolvablePuzzle exception.
  • It is recommended to call is_solvable() first or use try/except.
  • If the board size exceeds about 14280, a ValueError may occur due to Python's integer string conversion limit.

References & Tools

Metadata

Release files for python-lightsout 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-lightsout 2.0.1
File Size Uploaded Provenance Actions
python_lightsout-2.0.1.tar.gz 12.7 kB Without provenance Details for python_lightsout-2.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-lightsout 2.0.1
File Interpreter ABI Platform Actions
python_lightsout-2.0.1-py3-none-any.whl Python 3 none any Details for python_lightsout-2.0.1-py3-none-any.whl

Total release size: 21.1 kB

Release files / python_lightsout-2.0.1.tar.gz

Download URL python_lightsout-2.0.1.tar.gz
Size 12.7 kB
Tags Source
SHA-256 checksum
How to use checksums
d450a49b36c4269921ac2860a0f276a7ea59972b0f348377880ce2ca6eafb40f
BLAKE2b-256 checksum
How to use checksums
999901b546a395078c95b033ab87f0fd2955e972c13efbb4884fbf443e131efd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release files / python_lightsout-2.0.1-py3-none-any.whl

Download URL python_lightsout-2.0.1-py3-none-any.whl
Size 8.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e5627850d572d57394fb2801fce2049261810326ae4c9fdb7a5da6a4ddb177c
BLAKE2b-256 checksum
How to use checksums
f63447cb38a1148a4582b27c347c05441d6972ae961ce61d6f9713fee0093ea8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Security for python-lightsout 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page