def test_stepToHeadDirection_whenDirectionShouldNotChange(self, direction): self.snake.step(direction) expected_snake_parts = [ SnakePart(Vector(4, 0), Direction.RIGHT), SnakePart(Vector(3, 0), Direction.RIGHT) ] self.assertTrue(self.equals_snake(self.snake, expected_snake_parts))
class TranslateDirection: dir_offset = { Direction.UP: Vector(0, -1), Direction.DOWN: Vector(0, 1), Direction.RIGHT: Vector(1, 0), Direction.LEFT: Vector(-1, 0) } opposite_dir = { Direction.RIGHT: Direction.LEFT, Direction.LEFT: Direction.RIGHT, Direction.DOWN: Direction.UP, Direction.UP: Direction.DOWN } word_dir = { 'right': Direction.RIGHT, 'left': Direction.LEFT, 'up': Direction.UP, 'down': Direction.DOWN } direction = { pygame.K_RIGHT: Direction.RIGHT, pygame.K_LEFT: Direction.LEFT, pygame.K_UP: Direction.UP, pygame.K_DOWN: Direction.DOWN }
def test_eq(self): v1 = Vector(1, 2) v2 = Vector(1, 2) v3 = Vector(2, 3) self.assertTrue(v1 == v2) self.assertFalse(v1 == v3) self.assertFalse(v1 == 0)
def test_step_whenDirectionChange(self): self.snake.step(direction=Direction.UP) expected_snake_parts = [ SnakePart(Vector(3, -1), Direction.UP), SnakePart(Vector(3, 0), Direction.RIGHT) ] self.assertTrue(self.equals_snake(self.snake, expected_snake_parts))
def test_step_whenGoingOutOfTheField(self): self.snake.step(size_field=(4, 4)) expect_snake_parts = [ SnakePart(Vector(0, 0), Direction.RIGHT), SnakePart(Vector(3, 0), Direction.RIGHT) ] self.assertTrue(self.equals_snake(self.snake, expect_snake_parts))
def setUp(self): snake_parts = [ SnakePart(Vector(3, 0), Direction.RIGHT), SnakePart(Vector(2, 0), Direction.RIGHT) ] snake = Snake(snake_parts) walls = {Vector(3, 1), Vector(2, 1), Vector(1, 1)} size = (4, 4) self.field = Field(snake, walls, size)
def setUp(self): walls = {Vector(i, 2) for i in range(3)} snake = Snake([ SnakePart(Vector(2, 1), Direction.RIGHT), SnakePart(Vector(1, 1), Direction.RIGHT), SnakePart(Vector(0, 1), Direction.RIGHT) ]) self.field = Field(snake, walls, (3, 3)) with unittest.mock.patch.object(Level, 'parse_map') as mock_get_value: mock_get_value.return_value = deepcopy(self.field), 5 self.level = Level('level', 3)
def test_check_intersection(self): self.assertFalse(self.snake.check_intersection()) self.assertTrue( self.snake.check_intersection({self.snake_parts[0].location})) snake_parts = [ SnakePart(Vector(0, 0), Direction.RIGHT), SnakePart(Vector(0, 0), Direction.RIGHT) ] snake = Snake(snake_parts) self.assertTrue(snake.check_intersection()) self.assertTrue(snake.check_intersection({Vector(1, 1)}))
def test_step_whenSnakeShouldIncrease(self): self.snake.length_change = 1 expect_snake_parts = ([SnakePart(Vector(4, 0), Direction.RIGHT)] + self.snake_parts) self.snake.step() self.equals_snake(self.snake, expect_snake_parts)
def test_eat_food(self, mock): location = Vector(3, 0) food = Food() self.field.foods_location[location] = food self.assertEqual(food.score, self.field.eat_food(location)) self.field.foods_location[location] = food self.assertEqual(food.score, self.field.eat_food()) self.assertEqual(2, mock.call_count)
def __init__(self, snake: Snake, walls: set, size_field: tuple): self.foods_location = {} self.snake = snake self.walls = walls self.width, self.height = size_field self.not_walls_cells = set() for x in range(self.width): for y in range(self.height): point = Vector(x, y) if point not in walls: self.not_walls_cells.add(point)
def parse_map(map_path: str) -> (Field, int): walls = set() with open(map_path, 'r', encoding='utf-8') as mfd: lines = mfd.read().split('\n') width_field = 0 height_field = len(lines) - 5 for i in range(height_field): for j in range(len(lines[i])): if len(lines[i]) > width_field: width_field = len(lines[i]) if lines[i][j] == settings.wall_symbol: walls.add(Vector(j, i)) snake_dir = TranslateDirection.word_dir[lines[-5]] snake = [] for i in range(-4, -1): location = tuple(map(int, lines[i].split(' '))) snake_part = SnakePart(Vector(*location), snake_dir) snake.append(snake_part) max_score = int(lines[-1]) field = Field(Snake(snake), walls, (width_field, height_field)) return field, max_score
def test_mul(self): v = Vector(1, 2) result = v * 3 self.assertEqual([3, 6], [result.x, result.y])
def test_hash(self): v1 = Vector(1, 2) v2 = Vector(0, 0) self.assertEqual(hash(v1), hash(v1)) self.assertNotEqual(hash(v1), hash(v2))
def test_init(self): v = Vector(1, 2) self.assertEqual([1, 2], [v.x, v.y])
def setUp(self): self.snake_parts = [ SnakePart(Vector(3, 0), Direction.RIGHT), SnakePart(Vector(2, 0), Direction.RIGHT) ] self.snake = Snake(self.snake_parts)
def test_step_when_snake_eat_not_basic_food(self, mock): self.level.field.foods_location[Vector(0, 1)] = Food(2, 2, 2) self.level.step_snake() self.assertFalse(mock.called)
def test_step_snake_when_snake_break(self, mock): self.level.field.walls.add(Vector(0, 1)) self.level.step_snake() self.assertTrue(mock.called)
def test_add(self): v1 = Vector(1, 2) v2 = Vector(3, 4) result = v1 + v2 self.assertEqual([4, 6], [result.x, result.y])
def test_str(self): v = Vector(0, 1) self.assertEqual('(0, 1)', str(v))