Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions api/snake.py
Original file line number Diff line number Diff line change
@@ -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
Empty file added engine/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions api/entity/apple.py → engine/entity/apple.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
File renamed without changes.
14 changes: 9 additions & 5 deletions api/entity/snake.py → engine/entity/snake.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion api/map_location.py → engine/map_location.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from api.entity.entity import Entity
from engine.entity.entity import Entity


class MapLocation:
Expand Down
4 changes: 2 additions & 2 deletions api/world.py → engine/world.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 8 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/entity/test_apple.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from api.entity.apple import Apple, AppleType
from engine.entity.apple import Apple, AppleType


class TestApple(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/entity/test_snake.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down