From b091b98a93a42b4b820392cedd5efc863bb2117b Mon Sep 17 00:00:00 2001 From: novikovsd Date: Mon, 25 May 2026 09:23:17 +0000 Subject: [PATCH] experiments and new folder creation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit реализовано создание папки для сохранения в ней результатов эксперимента, реализовано создание тестогово лабиринта --- novikovsd/maze.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/novikovsd/maze.py b/novikovsd/maze.py index e14e424..af0d9bb 100644 --- a/novikovsd/maze.py +++ b/novikovsd/maze.py @@ -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) \ No newline at end of file + 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))