2026-rff_mp/krasnovia/lab2/docs/data/generate_mazes.py
2026-05-20 23:04:18 +03:00

126 lines
3.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Генерирует тестовые лабиринты в папку mazes/.
Запуск: python generate_mazes.py
"""
import os
import random
os.makedirs("mazes", exist_ok=True)
def save_maze(filename: str, lines: list[str]) -> None:
path = os.path.join("mazes", filename)
with open(path, "w", encoding="utf-8") as f:
f.write("\n".join(lines))
print(f"Создан: {path}")
# ── 1. Маленький 10×10 с простым путём ───────────────────────────────────────
small = [
"##########",
"#S #",
"# ###### #",
"# # # #",
"# # ## # #",
"# # ## # #",
"# # # #",
"# ###### #",
"# E#",
"##########",
]
save_maze("small_10x10.txt", small)
# ── 2. Средний 20×20 с тупиками ──────────────────────────────────────────────
def gen_medium():
W, H = 20, 20
grid = [["#"] * W for _ in range(H)]
def carve(x, y):
dirs = [(2, 0), (-2, 0), (0, 2), (0, -2)]
random.shuffle(dirs)
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 1 <= nx < W - 1 and 1 <= ny < H - 1 and grid[ny][nx] == "#":
grid[y + dy // 2][x + dx // 2] = " "
grid[ny][nx] = " "
carve(nx, ny)
grid[1][1] = " "
carve(1, 1)
grid[1][1] = "S"
# Убедимся что выход соединён с лабиринтом
grid[H - 2][W - 2] = " "
# Прорубаем проход к выходу если нужно
if grid[H - 3][W - 2] == "#" and grid[H - 2][W - 3] == "#":
grid[H - 3][W - 2] = " "
grid[H - 2][W - 2] = "E"
return ["".join(row) for row in grid]
random.seed(42)
save_maze("medium_20x20.txt", gen_medium())
# ── 3. Большой 50×50 с запутанной структурой ─────────────────────────────────
def gen_large(w=50, h=50, seed=7):
random.seed(seed)
grid = [["#"] * w for _ in range(h)]
def carve(x, y):
dirs = [(2, 0), (-2, 0), (0, 2), (0, -2)]
random.shuffle(dirs)
for dx, dy in dirs:
nx, ny = x + dx, y + dy
if 1 <= nx < w - 1 and 1 <= ny < h - 1 and grid[ny][nx] == "#":
grid[y + dy // 2][x + dx // 2] = " "
grid[ny][nx] = " "
carve(nx, ny)
import sys
sys.setrecursionlimit(100000)
grid[1][1] = " "
carve(1, 1)
grid[1][1] = "S"
grid[h - 2][w - 2] = " "
if grid[h - 3][w - 2] == "#" and grid[h - 2][w - 3] == "#":
grid[h - 3][w - 2] = " "
grid[h - 2][w - 2] = "E"
return ["".join(row) for row in grid]
save_maze("large_50x50.txt", gen_large())
# ── 4. «Пустой» лабиринт (без стен внутри) ───────────────────────────────────
def gen_open(w=20, h=20):
lines = []
for y in range(h):
row = ""
for x in range(w):
if y == 0 or y == h - 1 or x == 0 or x == w - 1:
row += "#"
elif x == 1 and y == 1:
row += "S"
elif x == w - 2 and y == h - 2:
row += "E"
else:
row += " "
lines.append(row)
return lines
save_maze("open_20x20.txt", gen_open())
# ── 5. Лабиринт без выхода ───────────────────────────────────────────────────
no_exit = [
"##########",
"#S #",
"# ########",
"# #",
"##########",
]
save_maze("no_exit.txt", no_exit)
print("\nВсе лабиринты созданы в папке mazes/")