[2] Начало тестов классов Cell и Maze

This commit is contained in:
SerKin0 2026-05-20 13:51:55 +03:00
parent 98c17e18fa
commit 5525af0b26
6 changed files with 29 additions and 34 deletions

View File

@ -26,9 +26,9 @@ class Cell:
"""
self.x = x
self.y = y
self.is_wall = is_wall
self.is_start = is_start
self.is_exit = is_exit
self._is_wall = is_wall
self._is_start = is_start
self._is_exit = is_exit
def isPossible(self) -> bool:
"""Проверка возможности перемещения в это поле
@ -38,54 +38,39 @@ class Cell:
"""
return not self.is_wall
def __str__(self) -> str:
if self._is_wall:
type_cell = "Стена"
elif self._is_start:
type_cell = "Начало"
elif self._is_exit:
type_cell = "Выход"
else:
type_cell = "Пусто"
return f"Cell: (x={self.x}, y={self.y}), '{type_cell}'"
class Maze:
def __init__(
self,
size: tuple[int, int] = (10, 10),
start_point: tuple[int, int] = (0, 0),
exit_point: tuple[int, int] = (-1, -1),
size: tuple[int, int] = (10, 10)
):
# Установка размеров лабиринта
self._width = size[0]
self._height = size[1]
# Установка значения стартового поля в лабиринте
self._start_x = start_point[0]
self._start_y = start_point[1]
# Установка значения выходного поля в лабиринте
self._exit_x = self._width - 1 if exit_point[0] == -1 else exit_point[0]
self._exit_y = self._height - 1 if exit_point[1] == -1 else exit_point[1]
# Создание двумерного списка лабиринта
self._map = [
[Cell(x, y) for x in range(self._width)] for y in range(self._height)
]
# Проверяем, что координаты старта находятся в области лабиринта
if self._check_point_in_map(self._start_x, self._start_y):
self._map[self._start_y][self._start_x].is_start = True
else:
raise ValueError(
f"Координата точки старта задана вне области лабиринта: ({self._start_x}, {self._start_y}) not in ({self._width}, {self._height})"
)
# Проверяем, что координаты выхода находятся в области лабиринта
if not self._check_point_in_map(self._exit_x, self._exit_y):
raise ValueError(
f"Координата точки выхода задана вне области лабиринта: ({self._start_x}, {self._start_y}) not in ({self._width}, {self._height})"
)
self._map[self._exit_y][self._exit_x].is_exit = True
def _check_point_in_map(self, x: int, y: int) -> bool:
return (0 <= x < self._width) and (0 <= y < self._height)
def get_cell(self, x: int, y: int) -> Optional[Cell]:
if not self._check_point_in_map(x, y):
raise ValueError(
f"Указанные координаты выходят за границы лабиринта: ({x}, {y}) not in ({self._width}, {self._height})"
)
return None
return self._map[y][x]
def get_neighbors(self, x: int, y: int) -> Optional[list[Cell]]:
@ -96,7 +81,8 @@ class Maze:
for vec_x, vec_y in zip(vector_x, vector_y):
temp_x, temp_y = x + vec_x, y + vec_y
if self._check_point_in_map(temp_x, temp_y):
neighbors.append(self._map[temp_y][temp_x])
value = self.get_cell(temp_x, temp_y)
if value is not None:
neighbors.append(value)
return neighbors

View File

@ -5,3 +5,4 @@ nbsphinx
myst-nb
tabulate
bibtexparser
pytest

View File

View File

View File

@ -0,0 +1,8 @@
import pytest
import sys
import os
import copy
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "./../../models")))
from base import Cell