From 638fbcdece197a5f133d5f98d18995a4fdf8bcdc Mon Sep 17 00:00:00 2001 From: ltuffery <123221865+ltuffery@users.noreply.github.com> Date: Sat, 22 Mar 2025 12:54:31 +0000 Subject: [PATCH] refactor: move api to engine package and create snake interface --- api/snake.py | 33 +++++++++++++++++++++++++++ engine/__init__.py | 0 {api => engine}/entity/apple.py | 4 ++-- {api => engine}/entity/entity.py | 0 {api => engine}/entity/snake.py | 14 ++++++++---- {api => engine}/exception/gameover.py | 0 {api => engine}/map_location.py | 2 +- {api => engine}/world.py | 4 ++-- main.py | 12 ++++++---- tests/entity/test_apple.py | 2 +- tests/entity/test_snake.py | 4 ++-- 11 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 api/snake.py create mode 100644 engine/__init__.py rename {api => engine}/entity/apple.py (96%) rename {api => engine}/entity/entity.py (100%) rename {api => engine}/entity/snake.py (94%) rename {api => engine}/exception/gameover.py (100%) rename {api => engine}/map_location.py (97%) rename {api => engine}/world.py (98%) diff --git a/api/snake.py b/api/snake.py new file mode 100644 index 0000000..432cfe5 --- /dev/null +++ b/api/snake.py @@ -0,0 +1,33 @@ +from abc import abstractmethod +from api.direction import Direction + + +class SnakeInterface: + """ + Defines the interface for a Snake entity in the game. + + This interface enforces the implementation of movement and vision + methods for a Snake. + """ + + @abstractmethod + def move(self, direction: Direction) -> None: + """ + Moves the snake in the specified direction. + + Args: + direction (Direction): The direction in which the snake should + move. + """ + pass + + @abstractmethod + def see(self) -> list[list[str]]: + """ + Provides the snake's current field of view as a 2D grid. + + Returns: + list[list[str]]: A 2D list representing what the snake can see + around it. + """ + pass diff --git a/engine/__init__.py b/engine/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/entity/apple.py b/engine/entity/apple.py similarity index 96% rename from api/entity/apple.py rename to engine/entity/apple.py index 1a15748..c77d667 100644 --- a/api/entity/apple.py +++ b/engine/entity/apple.py @@ -1,6 +1,6 @@ from enum import Enum -from api.entity.entity import Entity -from api.world import World +from engine.entity.entity import Entity +from engine.world import World class AppleType(Enum): diff --git a/api/entity/entity.py b/engine/entity/entity.py similarity index 100% rename from api/entity/entity.py rename to engine/entity/entity.py diff --git a/api/entity/snake.py b/engine/entity/snake.py similarity index 94% rename from api/entity/snake.py rename to engine/entity/snake.py index d9fa9b9..9d3f7b9 100644 --- a/api/entity/snake.py +++ b/engine/entity/snake.py @@ -1,13 +1,14 @@ from api.direction import Direction -from api.exception.gameover import GameOver -from api.entity.apple import Apple -from api.world import World -from api.entity.entity import Entity +from api.snake import SnakeInterface +from engine.exception.gameover import GameOver +from engine.entity.apple import Apple +from engine.world import World +from engine.entity.entity import Entity import random from collections import deque -class Snake(Entity): +class Snake(Entity, SnakeInterface): """ Represents a snake in the game, which moves, grows, and interacts with the world. @@ -138,6 +139,9 @@ def size(self) -> int: """ return len(self.__body) + 1 + def see(self): + pass + def get_body(self) -> deque[tuple[int, int]]: """ Returns the list of body segments of the snake. diff --git a/api/exception/gameover.py b/engine/exception/gameover.py similarity index 100% rename from api/exception/gameover.py rename to engine/exception/gameover.py diff --git a/api/map_location.py b/engine/map_location.py similarity index 97% rename from api/map_location.py rename to engine/map_location.py index 0e05436..c3b3e0e 100644 --- a/api/map_location.py +++ b/engine/map_location.py @@ -1,4 +1,4 @@ -from api.entity.entity import Entity +from engine.entity.entity import Entity class MapLocation: diff --git a/api/world.py b/engine/world.py similarity index 98% rename from api/world.py rename to engine/world.py index d86f9a2..e8dcfac 100644 --- a/api/world.py +++ b/engine/world.py @@ -1,5 +1,5 @@ -from api.map_location import MapLocation -from api.entity.entity import Entity +from engine.map_location import MapLocation +from engine.entity.entity import Entity import sys import copy import random diff --git a/main.py b/main.py index 8e1624d..ca576ea 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,12 @@ -from api.world import World +from engine.world import World from api.direction import Direction -from api.exception.gameover import GameOver -from api.entity.snake import Snake -from api.entity.apple import Apple, AppleType +from engine.exception.gameover import GameOver +from engine.entity.snake import Snake +from engine.entity.apple import Apple, AppleType +import sys +import os + +sys.path.append(os.path.abspath(os.path.dirname(__file__))) world = World() snake = Snake(world, 5, 5, Direction.SOUTH) diff --git a/tests/entity/test_apple.py b/tests/entity/test_apple.py index bdc9988..cb9b12b 100644 --- a/tests/entity/test_apple.py +++ b/tests/entity/test_apple.py @@ -1,5 +1,5 @@ import unittest -from api.entity.apple import Apple, AppleType +from engine.entity.apple import Apple, AppleType class TestApple(unittest.TestCase): diff --git a/tests/entity/test_snake.py b/tests/entity/test_snake.py index 3a27313..c338969 100644 --- a/tests/entity/test_snake.py +++ b/tests/entity/test_snake.py @@ -1,7 +1,7 @@ import unittest -from api.entity.snake import Snake +from engine.entity.snake import Snake from api.direction import Direction -from api.world import World +from engine.world import World class TestSnake(unittest.TestCase):