[2] Изменение положения функций _find_start() и _find_exit() из PathFindingStrategy в функционал класса Maze
This commit is contained in:
parent
f393ad5203
commit
86b31473f3
|
|
@ -183,6 +183,22 @@ class Maze:
|
||||||
neighbors.append(cell)
|
neighbors.append(cell)
|
||||||
|
|
||||||
return neighbors
|
return neighbors
|
||||||
|
|
||||||
|
@property
|
||||||
|
def start(self) -> Optional[Cell]:
|
||||||
|
for y in range(self._height):
|
||||||
|
for x in range(self._width):
|
||||||
|
if self[y, x].is_start:
|
||||||
|
return self[y, x]
|
||||||
|
return None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def exit(self) -> Optional[Cell]:
|
||||||
|
for y in range(self._height):
|
||||||
|
for x in range(self._width):
|
||||||
|
if self[y, x].is_exit:
|
||||||
|
return self[y, x]
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def shape(self) -> tuple[int, int]:
|
def shape(self) -> tuple[int, int]:
|
||||||
|
|
|
||||||
|
|
@ -23,24 +23,6 @@ class PathFindingStrategy(ABC):
|
||||||
Пустой список, если путь не найден.
|
Пустой список, если путь не найден.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _find_start(self, maze: Maze) -> Optional[Cell]:
|
|
||||||
row, col = maze.shape
|
|
||||||
|
|
||||||
for y in range(row):
|
|
||||||
for x in range(col):
|
|
||||||
if maze[y, x].is_start:
|
|
||||||
return maze[y, x]
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _find_exit(self, maze: Maze) -> Optional[Cell]:
|
|
||||||
row, col = maze.shape
|
|
||||||
|
|
||||||
for y in range(row):
|
|
||||||
for x in range(col):
|
|
||||||
if maze[y, x].is_exit:
|
|
||||||
return maze[y, x]
|
|
||||||
return None
|
|
||||||
|
|
||||||
def _reconstruct_path(
|
def _reconstruct_path(
|
||||||
self, came_from: dict[Cell, Optional[Cell]], end: Cell
|
self, came_from: dict[Cell, Optional[Cell]], end: Cell
|
||||||
) -> list[Cell]:
|
) -> list[Cell]:
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ class AStarStrategy(PathFindingStrategy):
|
||||||
|
|
||||||
def find_path(self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None) -> list[Cell]:
|
def find_path(self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None) -> list[Cell]:
|
||||||
if start is None:
|
if start is None:
|
||||||
start = self._find_start(maze)
|
start = maze.start
|
||||||
if exit is None:
|
if exit is None:
|
||||||
exit = self._find_exit(maze)
|
exit = self.exit
|
||||||
|
|
||||||
g_score: dict[Cell, int] = {start: 0}
|
g_score: dict[Cell, int] = {start: 0}
|
||||||
came_from: dict[Cell, Optional[Cell]] = {start: None}
|
came_from: dict[Cell, Optional[Cell]] = {start: None}
|
||||||
|
|
|
||||||
|
|
@ -16,9 +16,9 @@ class BFSStrategy(PathFindingStrategy):
|
||||||
self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None
|
self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None
|
||||||
) -> list[Cell]:
|
) -> list[Cell]:
|
||||||
if start is None:
|
if start is None:
|
||||||
start = self._find_start(maze)
|
start = maze.start
|
||||||
if exit is None:
|
if exit is None:
|
||||||
exit = self._find_exit(maze)
|
exit = maze.exit
|
||||||
|
|
||||||
came_from: dict[Cell, Optional[Cell]] = {start: None}
|
came_from: dict[Cell, Optional[Cell]] = {start: None}
|
||||||
queue: deque[Cell] = deque([start])
|
queue: deque[Cell] = deque([start])
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,9 @@ class DFSStrategy(PathFindingStrategy):
|
||||||
self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None
|
self, maze: Maze, start: Optional[Cell] = None, exit: Optional[Cell] = None
|
||||||
) -> list[Cell]:
|
) -> list[Cell]:
|
||||||
if start is None:
|
if start is None:
|
||||||
start = self._find_start(maze)
|
start = maze.start
|
||||||
if exit is None:
|
if exit is None:
|
||||||
exit = self._find_exit(maze)
|
exit = maze.exit
|
||||||
|
|
||||||
came_from: dict[Cell, Optional[Cell]] = {start: None}
|
came_from: dict[Cell, Optional[Cell]] = {start: None}
|
||||||
stack: list[Cell] = [start]
|
stack: list[Cell] = [start]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user