2026-rff_mp/novikovsd/maze.py
novikovsd 20982d4172 maze model
реализованы классы симулирующую модель лабиринта
2026-05-25 13:20:57 +03:00

75 lines
1.8 KiB
Python

import sys
import time
import csv
from collections import deque
from heapq import heappush, heappop
from abc import ABC, abstractmethod
from typing import List, Optional, Tuple, Dict, Any
import os
class Cell:
def __init__(self, x: int, y: int, is_wall: bool = False):
self.x = x
self.y = y
self.is_wall = is_wall
self.is_start = False
self.is_exit = False
def is_passable(self) -> bool:
return not self.is_wall
def __repr__(self):
return f"Cell({self.x},{self.y})"
class Maze:
def __init__(self, width: int, height: int):
self.width = width
self.height = height
self.cells: List[List[Cell]] = []
self.start: Optional[Cell] = None
self.exit: Optional[Cell] = None
def set_cell(self, x: int, y: int, cell: Cell):
if not self.cells:
self.cells = [[None] * self.width for _ in range(self.height)]
self.cells[y][x] = cell
def get_cell(self, x: int, y: int) -> Optional[Cell]:
if 0 <= x < self.width and 0 <= y < self.height:
return self.cells[y][x]
return None
def get_neighbors(self, cell: Cell) -> List[Cell]:
neighbors = []
for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
nx, ny = cell.x + dx, cell.y + dy
neighbor = self.get_cell(nx, ny)
if neighbor and neighbor.is_passable():
neighbors.append(neighbor)
return neighbors
class MazeBuilder(ABC):
class TextFileMazeBuilder(MazeBuilder):
class PathFindingStrategy(ABC):
class BFSStrategy(PathFindingStrategy):
class DFSStrategy(PathFindingStrategy):
class AStarStrategy(PathFindingStrategy):
class SearchStats:
class MazeSolver:
class Observer(ABC):
class ConsoleView(Observer):
class MoveCommand(ABC):
class Player:
class MoveCommandImpl(MoveCommand):