Python Lights Out
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:
- Install directly from this repository.
- 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 anUnsolvablePuzzleexception. - It is recommended to call
is_solvable()first or usetry/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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_lightsout-1.0.0.tar.gz.
File metadata
- Download URL: python_lightsout-1.0.0.tar.gz
- Upload date:
- Size: 11.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe7a8a414644216ca80bbee4d03e0fff7cbacace9476abce9fc09fbaee33c241
|
|
| MD5 |
b44c3bbec0a2da051d3d33eed06f66c5
|
|
| BLAKE2b-256 |
886286dc8ef299da1dadbf08f21fd568e00693facae14b9a4b4c56b4a138c883
|
File details
Details for the file python_lightsout-1.0.0-py3-none-any.whl.
File metadata
- Download URL: python_lightsout-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89c1932c15f559bf65d842d220446f849b56d035307d215a74087f19b16b7a48
|
|
| MD5 |
b1b46a71c7cc1d9a6391f2e7e9cc5624
|
|
| BLAKE2b-256 |
f5af12eef2c80f85123b066661f8709ba3f256cc4e69885ebb5714c89a859573
|