experiments and new folder creation

реализовано создание папки для сохранения в ней результатов эксперимента, реализовано создание тестогово лабиринта
This commit is contained in:
novikovsd 2026-05-25 09:23:17 +00:00
parent 9ae55f1244
commit b091b98a93

View File

@ -7,6 +7,8 @@ from abc import ABC, abstractmethod
from typing import List, Optional, Tuple, Dict, Any
import os
RESULTS_DIR = "lab2_results"
class Cell:
def __init__(self, x: int, y: int, is_wall: bool = False):
self.x = x
@ -335,4 +337,87 @@ class MoveCommandImpl(MoveCommand):
return False
def undo(self):
self.player.move_to(self.previous_cell)
self.player.move_to(self.previous_cell)
def ensure_results_dir():
if not os.path.exists(RESULTS_DIR):
os.makedirs(RESULTS_DIR)
print(f"Создана папка: {RESULTS_DIR}")
def generate_test_maze_file(filename: str, maze_type: str):
full_path = os.path.join(RESULTS_DIR, filename)
if maze_type == "small":
lines = [
"##########",
"#S #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# E#",
"##########"
]
elif maze_type == "medium":
height, width = 50, 50
lines = []
for y in range(height):
row = []
for x in range(width):
if y == 0 or y == height-1 or x == 0 or x == width-1:
row.append('#')
elif (y % 5 == 0 and x % 7 == 0) or (y % 8 == 0 and x % 3 == 0):
row.append('#')
else:
row.append(' ')
row_str = ''.join(row)
lines.append(row_str)
lines[1] = 'S' + lines[1][1:]
lines[height-2] = lines[height-2][:width-2] + 'E' + lines[height-2][width-1:]
elif maze_type == "large":
import random
height, width = 100, 100
random.seed(42)
lines = []
for y in range(height):
row = []
for x in range(width):
if y == 0 or y == height-1 or x == 0 or x == width-1:
row.append('#')
else:
if random.random() < 0.2:
row.append('#')
else:
row.append(' ')
lines.append(''.join(row))
lines[1] = 'S' + lines[1][1:]
lines[height-2] = lines[height-2][:width-2] + 'E' + lines[height-2][width-1:]
elif maze_type == "empty":
height, width = 50, 50
lines = []
for y in range(height):
if y == 0 or y == height-1:
lines.append('#' * width)
else:
lines.append('#' + ' ' * (width-2) + '#')
lines[1] = 'S' + lines[1][1:]
lines[height-2] = lines[height-2][:width-2] + 'E' + lines[height-2][width-1:]
elif maze_type == "no_exit":
lines = [
"##########",
"#S #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"# #",
"##########"
]
else:
raise ValueError("Unknown maze type")
with open(full_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(lines))