[2] Начало тестов классов Cell и Maze
This commit is contained in:
parent
98c17e18fa
commit
5525af0b26
|
|
@ -26,9 +26,9 @@ class Cell:
|
||||||
"""
|
"""
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.is_wall = is_wall
|
self._is_wall = is_wall
|
||||||
self.is_start = is_start
|
self._is_start = is_start
|
||||||
self.is_exit = is_exit
|
self._is_exit = is_exit
|
||||||
|
|
||||||
def isPossible(self) -> bool:
|
def isPossible(self) -> bool:
|
||||||
"""Проверка возможности перемещения в это поле
|
"""Проверка возможности перемещения в это поле
|
||||||
|
|
@ -37,55 +37,40 @@ class Cell:
|
||||||
:rtype: bool
|
:rtype: bool
|
||||||
"""
|
"""
|
||||||
return not self.is_wall
|
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:
|
class Maze:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
size: tuple[int, int] = (10, 10),
|
size: tuple[int, int] = (10, 10)
|
||||||
start_point: tuple[int, int] = (0, 0),
|
|
||||||
exit_point: tuple[int, int] = (-1, -1),
|
|
||||||
):
|
):
|
||||||
# Установка размеров лабиринта
|
# Установка размеров лабиринта
|
||||||
self._width = size[0]
|
self._width = size[0]
|
||||||
self._height = size[1]
|
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 = [
|
self._map = [
|
||||||
[Cell(x, y) for x in range(self._width)] for y in range(self._height)
|
[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:
|
def _check_point_in_map(self, x: int, y: int) -> bool:
|
||||||
return (0 <= x < self._width) and (0 <= y < self._height)
|
return (0 <= x < self._width) and (0 <= y < self._height)
|
||||||
|
|
||||||
def get_cell(self, x: int, y: int) -> Optional[Cell]:
|
def get_cell(self, x: int, y: int) -> Optional[Cell]:
|
||||||
if not self._check_point_in_map(x, y):
|
if not self._check_point_in_map(x, y):
|
||||||
raise ValueError(
|
return None
|
||||||
f"Указанные координаты выходят за границы лабиринта: ({x}, {y}) not in ({self._width}, {self._height})"
|
|
||||||
)
|
|
||||||
return self._map[y][x]
|
return self._map[y][x]
|
||||||
|
|
||||||
def get_neighbors(self, x: int, y: int) -> Optional[list[Cell]]:
|
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):
|
for vec_x, vec_y in zip(vector_x, vector_y):
|
||||||
temp_x, temp_y = x + vec_x, y + vec_y
|
temp_x, temp_y = x + vec_x, y + vec_y
|
||||||
if self._check_point_in_map(temp_x, temp_y):
|
value = self.get_cell(temp_x, temp_y)
|
||||||
neighbors.append(self._map[temp_y][temp_x])
|
if value is not None:
|
||||||
|
neighbors.append(value)
|
||||||
|
|
||||||
return neighbors
|
return neighbors
|
||||||
|
|
@ -5,3 +5,4 @@ nbsphinx
|
||||||
myst-nb
|
myst-nb
|
||||||
tabulate
|
tabulate
|
||||||
bibtexparser
|
bibtexparser
|
||||||
|
pytest
|
||||||
0
skorohodovsa/task_2/test/__init__.py
Normal file
0
skorohodovsa/task_2/test/__init__.py
Normal file
0
skorohodovsa/task_2/test/map/__init__.py
Normal file
0
skorohodovsa/task_2/test/map/__init__.py
Normal file
8
skorohodovsa/task_2/test/map/test_cell.py
Normal file
8
skorohodovsa/task_2/test/map/test_cell.py
Normal 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
|
||||||
0
skorohodovsa/task_2/test/map/test_maze.py
Normal file
0
skorohodovsa/task_2/test/map/test_maze.py
Normal file
Loading…
Reference in New Issue
Block a user