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
2 changes: 1 addition & 1 deletion ai/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import engine.settings as settings
from engine.exception.gameover import GameOver
import ai.replay as replay
from ai.utils import get_Q, action
from ai.utils import get_Q, action

# Exploration rate
EPSILON = settings.EPSILON
Expand Down
2 changes: 1 addition & 1 deletion ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def action(Q: dict, state: list[bool], explo: float) -> int:
if np.random.uniform() < explo:
return np.random.randint(0, len(Direction)) # Exploration
else:
return max(range(4), key=lambda a: get_Q(Q, state, a)) # Exploitation
return max(range(4), key=lambda a: get_Q(Q, state, a)) # Exploitation
4 changes: 2 additions & 2 deletions engine/entity/apple.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Apple(Entity):
__apple_type (AppleType): The type of the apple (RED or GREEN).
"""

def __init__(self, world: World, x: int, y: int, apple_type: AppleType):
def __init__(self, world: World, apple_type: AppleType):
"""
Initializes the apple with a given type and position.

Expand All @@ -36,7 +36,7 @@ def __init__(self, world: World, x: int, y: int, apple_type: AppleType):
y (int): The Y-coordinate of the apple's initial position.
apple_type (AppleType): The type of apple (red or green).
"""
super().__init__(x, y)
super().__init__()
self.__world = world
self.__apple_type = apple_type

Expand Down
6 changes: 3 additions & 3 deletions engine/entity/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ class Entity(ABC):
__y (int): The Y-coordinate of the entity.
"""

def __init__(self, x: int, y: int):
def __init__(self):
"""
Initializes an entity with specified coordinates.

Args:
x (int): The X-coordinate of the entity.
y (int): The Y-coordinate of the entity.
"""
self.__x = x
self.__y = y
self.__x = 0
self.__y = 0

def get_x(self) -> int:
"""
Expand Down
9 changes: 5 additions & 4 deletions engine/entity/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Snake(Entity):
The last movement direction of the snake.
"""

def __init__(self, world: World, x: int, y: int, direction: Direction):
def __init__(self, world: World):
"""
Initializes a snake in the given world at a specific position and
direction.
Expand All @@ -33,13 +33,14 @@ def __init__(self, world: World, x: int, y: int, direction: Direction):
y (int): The initial Y position of the snake's head.
direction (Direction): The initial movement direction of the snake.
"""
super().__init__(x, y)
super().__init__()

self.__body: deque[tuple[int, int]] = deque()
self.__world: World = world
self.__last_direction: Direction = direction
self.__last_direction: Direction = random.choice(list(Direction))

dir_x, dir_y = direction.value
dir_x, dir_y = self.__last_direction.value
x, y = self.get_position()

# Create the initial body of the snake (3 segments)
for _ in range(3):
Expand Down
22 changes: 13 additions & 9 deletions engine/game.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from engine.world import World
from engine.entity.snake import Snake
from engine.entity.apple import Apple, AppleType
from engine.direction import Direction


class Game:
Expand Down Expand Up @@ -33,12 +32,12 @@ def start(self) -> None:
(one green and one red) at the starting position (0, 0).
"""
self.__world = World()
self.__snake = Snake(self.__world, 0, 0, Direction.EAST)
self.__snake = Snake(self.__world)

self.__world.spawn_entity(self.__snake)
self.__world.spawn_entity(Apple(self.__world, 0, 0, AppleType.GREEN))
self.__world.spawn_entity(Apple(self.__world, 0, 0, AppleType.GREEN))
self.__world.spawn_entity(Apple(self.__world, 0, 0, AppleType.RED))
self.__world.spawn_entity(Apple(self.__world, AppleType.GREEN))
self.__world.spawn_entity(Apple(self.__world, AppleType.GREEN))
self.__world.spawn_entity(Apple(self.__world, AppleType.RED))

def get_snake(self) -> Snake:
"""
Expand Down Expand Up @@ -69,8 +68,11 @@ def set_snake(self, head: tuple[int, int], body: list[tuple]) -> None:
body (list[tuple[int, int]]): A list of (x, y) tuples representing
the snake's body segments.
"""
self.__snake = Snake(self.__world, head[0], head[1], Direction.EAST)
self.__snake = Snake(self.__world)
self.__snake.set_x(head[0])
self.__snake.set_y(head[1])
self.__snake.set_body(body)

self.__world.add_entity(self.__snake)

def set_apples(self, apples: list[tuple[int, int, bool]]) -> None:
Expand All @@ -86,7 +88,9 @@ def set_apples(self, apples: list[tuple[int, int, bool]]) -> None:
"""
for apple in apples:
apple_type = AppleType.GREEN if apple[2] else AppleType.RED
apple_obj = Apple(self.__world, apple_type)

apple_obj.set_x(apple[0])
apple_obj.set_y(apple[1])

self.__world.add_entity(
Apple(self.__world, apple[0], apple[1], apple_type)
)
self.__world.add_entity(apple_obj)
9 changes: 4 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

world = World()
snake = Snake(world, 5, 5, Direction.SOUTH)
green_apple = Apple(world, 2, 2, AppleType.GREEN)
red_apple = Apple(world, 2, 2, AppleType.RED)
snake = Snake(world)

world.spawn_entity(snake)
world.spawn_entity(green_apple)
world.spawn_entity(red_apple)
world.spawn_entity(Apple(world, AppleType.GREEN))
world.spawn_entity(Apple(world, AppleType.GREEN))
world.spawn_entity(Apple(world, AppleType.RED))

while True:
world.render()
Expand Down
Loading