2026-rff_mp/SmirnovVS/docs/data/2-nd-exercize/generate_mazes.py
2026-09-04 17:37:01 +00:00

53 lines
1.9 KiB
Python

"""Генерация воспроизводимых тестовых лабиринтов."""
import random
from pathlib import Path
def obstacle_maze(width, height, wall_probability, seed):
rng = random.Random(seed)
grid = [["#" if x in (0, width - 1) or y in (0, height - 1) else " " for x in range(width)] for y in range(height)]
for y in range(1, height - 1):
for x in range(1, width - 1):
if rng.random() < wall_probability:
grid[y][x] = "#"
# Оставляем гарантированный путь по верхней и правой внутренним границам.
for x in range(1, width - 1):
grid[1][x] = " "
for y in range(1, height - 1):
grid[y][width - 2] = " "
grid[1][1], grid[height - 2][width - 2] = "S", "E"
return "\n".join("".join(row) for row in grid) + "\n"
def empty_maze(width=50, height=50):
return obstacle_maze(width, height, 0, 1)
def blocked_maze(width=30, height=30):
lines = empty_maze(width, height).splitlines()
grid = [list(line) for line in lines]
exit_y, exit_x = height - 2, width - 2
grid[exit_y - 1][exit_x] = "#"
grid[exit_y][exit_x - 1] = "#"
return "\n".join("".join(row) for row in grid) + "\n"
def generate_all(output_dir="mazes"):
output = Path(output_dir)
output.mkdir(parents=True, exist_ok=True)
maps = {
"small_10x10.txt": obstacle_maze(10, 10, 0.12, 10),
"medium_50x50.txt": obstacle_maze(50, 50, 0.28, 50),
"large_100x100.txt": obstacle_maze(100, 100, 0.32, 100),
"empty_50x50.txt": empty_maze(),
"no_path_30x30.txt": blocked_maze(),
}
for filename, content in maps.items():
(output / filename).write_text(content, encoding="utf-8")
return list(maps)
if __name__ == "__main__":
print("Созданы файлы:", ", ".join(generate_all()))