2026-rff_mp/kolesovve/task2/docs/data/generate_mazes.py
2026-09-05 01:23:16 +03:00

88 lines
2.4 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.

import random
def save_maze(filename, width, height, wall_probability):
maze = []
for i in range(height):
row = ""
for j in range(width):
if i == 0 or i == height - 1:
row += "#"
elif j == 0 or j == width - 1:
row += "#"
else:
if random.random() < wall_probability:
row += "#"
else:
row += " "
maze.append(list(row))
maze[1][1] = "S"
maze[height - 2][width - 2] = "E"
for i in range(1, height - 1):
maze[i][1] = " "
for j in range(1, width - 1):
maze[height - 2][j] = " "
maze[1][1] = "S"
maze[height - 2][width - 2] = "E"
with open(filename, "w", encoding="utf-8") as f:
for row in maze:
f.write("".join(row) + "\n")
def save_maze_no_exit(filename, width, height, wall_probability):
maze = []
for i in range(height):
row = ""
for j in range(width):
if i == 0 or i == height - 1:
row += "#"
elif j == 0 or j == width - 1:
row += "#"
else:
if random.random() < wall_probability:
row += "#"
else:
row += " "
maze.append(list(row))
maze[1][1] = "S"
for i in range(1, height - 1):
maze[i][1] = " "
maze[1][1] = "S"
with open(filename, "w", encoding="utf-8") as f:
for row in maze:
f.write("".join(row) + "\n")
def save_maze_no_wall(filename, width, height):
maze = []
for i in range(height):
row = ""
for j in range(width):
if i == 0 or i == height - 1:
row += "#"
elif j == 0 or j == width - 1:
row += "#"
else:
row += " "
maze.append(list(row))
maze[1][1] = "S"
maze[height - 2][width - 2] = "E"
with open(filename, "w", encoding="utf-8") as f:
for row in maze:
f.write("".join(row) + "\n")
# Генерируем все лабиринты
save_maze("small_maze.txt", 10, 10, 0.20)
save_maze("medium_maze.txt", 50, 50, 0.30)
save_maze("big_maze.txt", 100, 100, 0.40)
save_maze_no_wall("no_wall_maze.txt", 10, 10)
save_maze_no_exit("no_exit_maze.txt", 10, 10, 0.30)
print("Все лабиринты созданы!")