class FieldTests(unittest.TestCase): def setUp(self): self.__x = random.randint(2, 128) self.__y = random.randint(2, 128) self.__field = Field(self.__y, self.__x) def test_zero(self): self.assertFalse(Field(0, 0).is_correct(0, 0)) def test_size(self): self.assertEqual(self.__field.x, self.__x) self.assertEqual(self.__field.y, self.__y) def test_basic(self): n = random.random() x = random.randint(0, self.__x - 1) y = random.randint(0, self.__y - 1) self.__field[y][x] = n self.assertEqual(self.__field[y][x], n) def test_border(self): self.__field[0][0] = 0 self.__field[self.__y - 1][self.__x - 1] = 0 def test_method_is_correct(self): x = random.randint(0, self.__x - 1) y = random.randint(0, self.__y - 1) self.assertTrue(self.__field.is_correct(0, 0)) self.assertTrue(self.__field.is_correct(y, x)) self.assertTrue(not self.__field.is_correct(-1, 0)) self.assertTrue(not self.__field.is_correct(0, -1)) self.assertTrue(not self.__field.is_correct(self.__y, 0)) self.assertTrue(not self.__field.is_correct(0, self.__x))
def __init__(self, field: Field, x: int, y: int): self._neighbor = {'grass': 0, 'herbivore': 0, 'carnivore': 0} if not field.is_correct(y, x): raise IndexError for dx, dy in [(0, 0), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]: cx = x + dx cy = y + dy if not field.is_correct(cy, cx): continue self._neighbor['grass'] += field[cy][cx].get_grass() self._neighbor['herbivore'] += field[cy][cx].get_herbivore() self._neighbor['carnivore'] += field[cy][cx].get_carnivore()
def __init__(self, field: Field, x: int, y: int): self.__neighbor = {} if not field.is_correct(y, x): raise IndexError for dx, dy in [(0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1)]: cx = x + dx cy = y + dy if not field.is_correct(cy, cx): continue class_name = type(field[cy][cx]).__name__ if class_name in self.__neighbor: self.__neighbor[class_name] += 1 else: self.__neighbor[class_name] = 1