Piece¶
Piece 클래스는 테트리스 피스(테트로미노)의 형태, 회전, 위치를 관리합니다.
API Reference¶
Piece ¶
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
rotate_clockwise ¶
move ¶
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 |
set_position ¶
Set the piece position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
int
|
New x position |
required |
y
|
int
|
New y position |
required |
get_all_rotations ¶
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
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