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)
print(board, "\n")

# You can also provide a 1D grid (size must be length * length)
board = Board(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 5x5 generator.
# random_seed can be omitted if not needed.
generator = Generator(length=5, 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 3 4
0  . . . . O
1  O . . O .
2  . . . O O
3  . O . O O
4  O O . O . 

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

Find a solution

from lightsout import Board, Generator, Solver

puzzle = Generator(length=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 .
1  O . O
2  O . O 

Press the following positions in order:

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

Press (2, 1)
   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.

References & Tools

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_lightsout-1.0.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_lightsout-1.0.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file python_lightsout-1.0.1.tar.gz.

File metadata

  • Download URL: python_lightsout-1.0.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.15

File hashes

Hashes for python_lightsout-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4a206b213995244558f213e108e74042062a682b6cbb0681e6987553ec4b7c3a
MD5 57e6f66ba8f8e866a806f42ccb35d8e1
BLAKE2b-256 c65ea8cec9093bfea82c3f06d427e72b2971fbfac8e8f2cd0d85b79476b9a3ad

See more details on using hashes here.

File details

Details for the file python_lightsout-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_lightsout-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b39842d71dc2f15577da7787179ab029f6d9b7c2ed255319fc49e097661a7abe
MD5 5f6547c13a1cbe9dfa5d3ece92ab7ee5
BLAKE2b-256 18f0e8d47f11dda5bb6384982170f6a03941fa2d46474048664d925a3bd4a237

See more details on using hashes here.

Release history Release notifications | RSS feed

2.0.1

2 files

This release

1.0.1 This release

2 files

1.0.0

2 files

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