콘텐츠로 이동

Piece

Piece 클래스는 테트리스 피스(테트로미노)의 형태, 회전, 위치를 관리합니다.

API Reference

Piece

Piece(piece_type: int, x: int = 0, y: int = 0)

Represents a Tetris piece (tetromino).

Responsibilities: - Store piece shape and type - Provide rotation operations - Manage piece position

Initialize a new piece.

Parameters:

Name Type Description Default
piece_type int

Index of the piece type (0-6)

required
x int

Initial column position

0
y int

Initial row position

0
Source code in rl_tetris/core/piece.py
def __init__(self, piece_type: int, x: int = 0, y: int = 0):
    """
    Initialize a new piece.

    Args:
        piece_type: Index of the piece type (0-6)
        x: Initial column position
        y: Initial row position
    """
    if not 0 <= piece_type < len(self.SHAPES):
        raise ValueError(f"Invalid piece type: {piece_type}")

    self.piece_type = piece_type
    self.shape = [row[:] for row in self.SHAPES[piece_type]]
    self.x = x
    self.y = y

rotate_clockwise

rotate_clockwise() -> None

Rotate the piece 90 degrees clockwise (modifies piece state).

Source code in rl_tetris/core/piece.py
def rotate_clockwise(self) -> None:
    """
    Rotate the piece 90 degrees clockwise (modifies piece state).
    """
    self.shape = self.get_rotated_clockwise(self.shape)

move

move(dx: int, dy: int) -> None

Move the piece by the given offset.

Parameters:

Name Type Description Default
dx int

Change in x position

required
dy int

Change in y position

required
Source code in rl_tetris/core/piece.py
def move(self, dx: int, dy: int) -> None:
    """
    Move the piece by the given offset.

    Args:
        dx: Change in x position
        dy: Change in y position
    """
    self.x += dx
    self.y += dy

set_position

set_position(x: int, y: int) -> None

Set the piece position.

Parameters:

Name Type Description Default
x int

New x position

required
y int

New y position

required
Source code in rl_tetris/core/piece.py
def set_position(self, x: int, y: int) -> None:
    """
    Set the piece position.

    Args:
        x: New x position
        y: New y position
    """
    self.x = x
    self.y = y

get_all_rotations

get_all_rotations() -> List[List[List[int]]]

Get all unique rotations of the current piece.

Returns:

Type Description
List[List[List[int]]]

List of rotated shapes (up to 4 rotations)

Source code in rl_tetris/core/piece.py
def get_all_rotations(self) -> List[List[List[int]]]:
    """
    Get all unique rotations of the current piece.

    Returns:
        List of rotated shapes (up to 4 rotations)
    """
    rotations = []
    current_shape = self.get_shape_copy()

    for _ in range(4):
        # Check if this rotation is unique
        if current_shape not in rotations:
            rotations.append([row[:] for row in current_shape])
        current_shape = self.get_rotated_clockwise(current_shape)

    return rotations

copy

copy() -> Piece

Create a copy of this piece.

Returns:

Type Description
Piece

New Piece instance with the same state

Source code in rl_tetris/core/piece.py
def copy(self) -> 'Piece':
    """
    Create a copy of this piece.

    Returns:
        New Piece instance with the same state
    """
    new_piece = Piece(self.piece_type, self.x, self.y)
    new_piece.shape = self.get_shape_copy()
    return new_piece

Piece Types

Type Name Shape Rotations
0 I ████ 2
1 O ██
██
1
2 T ███
4
3 S ██
██
2
4 Z ██
██
2
5 J
███
4
6 L
███
4

Usage Examples

피스 생성 및 회전

from rl_tetris.core import Piece

# T 피스 생성
piece = Piece(piece_type=2)
print(f"Initial position: ({piece.x}, {piece.y})")

# 회전
piece.rotate_clockwise()

# 모든 회전 상태 확인
rotations = piece.get_all_rotations()
print(f"Total rotations: {len(rotations)}")

피스 이동

# 상대 이동
piece.move(dx=1, dy=0)  # 오른쪽으로
piece.move(dx=0, dy=1)  # 아래로

# 절대 위치 설정
piece.set_position(x=5, y=10)

피스 복사

# 시뮬레이션을 위한 복사
original = Piece(piece_type=0)
copy = original.copy()

copy.rotate_clockwise()
copy.move(1, 0)

# 원본은 변경되지 않음
assert original.x != copy.x

See Also