Skip to content

Commit

Permalink
Abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Weichart committed May 4, 2024
1 parent 11d2bf6 commit 691871a
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 149 deletions.
16 changes: 8 additions & 8 deletions examples/play_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import cv2
import gymnasium as gym

from tetris_gymnasium.envs.tetris import ACTIONS
from tetris_gymnasium.envs import Tetris

if __name__ == "__main__":
# Create an instance of Tetris
Expand All @@ -30,19 +30,19 @@
key = cv2.waitKey(1)

if key == ord("a"):
action = ACTIONS["move_left"]
action = tetris_game.unwrapped.actions.move_left
elif key == ord("d"):
action = ACTIONS["move_right"]
action = tetris_game.unwrapped.actions.move_right
elif key == ord("s"):
action = ACTIONS["move_down"]
action = tetris_game.unwrapped.actions.move_down
elif key == ord("w"):
action = ACTIONS["rotate_counterclockwise"]
action = tetris_game.unwrapped.actions.rotate_counterclockwise
elif key == ord("e"):
action = ACTIONS["rotate_clockwise"]
action = tetris_game.unwrapped.actions.rotate_clockwise
elif key == ord(" "):
action = ACTIONS["hard_drop"]
action = tetris_game.unwrapped.actions.hard_drop
elif key == ord("q"):
action = ACTIONS["swap"]
action = tetris_game.unwrapped.actions.swap
elif key == ord("r"):
tetris_game.reset(seed=42)
break
Expand Down
25 changes: 25 additions & 0 deletions tetris_gymnasium/components/tetromino.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Data structures for Tetris."""
from dataclasses import dataclass

import numpy as np


@dataclass
class Pixel:
"""A single pixel of a Tetris game.
A pixel can be part of a tetromino or part of the game board (empty, bedrock).
"""

id: int
color_rgb: list


@dataclass
class Tetromino(Pixel):
"""A Tetris piece.
A tetromino is a geometric shape composed of multiple pixels.
"""

matrix: np.ndarray
8 changes: 4 additions & 4 deletions tetris_gymnasium/components/tetromino_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from collections import deque
from typing import Optional

import numpy as np
from tetris_gymnasium.components.tetromino import Tetromino


class TetrominoHolder:
Expand All @@ -17,15 +17,15 @@ def __init__(self, size=1):
self.size = size
self.queue = deque(maxlen=size)

def _get_tetromino(self) -> Optional[np.ndarray]:
def _get_tetromino(self) -> Optional[Tetromino]:
"""Get the next tetromino from the holder."""
return self.queue.popleft()

def _store_tetromino(self, tetromino: np.ndarray):
def _store_tetromino(self, tetromino: Tetromino):
"""Store a tetromino in the holder."""
self.queue.append(tetromino)

def swap(self, tetromino: np.ndarray) -> Optional[np.ndarray]:
def swap(self, tetromino: Tetromino) -> Optional[Tetromino]:
"""Swap the given tetromino with the one in the holder.
This implementation uses a queue to store the tetrominoes. Tetromioes are only returned once the queue is full.
Expand Down
Loading

0 comments on commit 691871a

Please sign in to comment.