Game¶
Game 클래스는 테트리스 게임의 전체 흐름을 제어하고, 스코어링, 피스 스폰, 게임 오버 처리를 담당합니다.
API Reference¶
Game ¶
Manages Tetris game logic and orchestration.
Responsibilities: - Game state management (score, lines cleared, game over) - Piece spawning and movement - Reward calculation - Game flow orchestration
Initialize a new game.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
board
|
Board
|
Board instance to use |
required |
queue
|
TetrominoQueue
|
TetrominoQueue instance for piece generation |
required |
initial_position
|
str
|
How to position new pieces ("center" or "left") |
'center'
|
Source code in rl_tetris/core/game.py
reset ¶
spawn_piece ¶
Spawn a new piece at the top of the board.
Returns:
| Type | Description |
|---|---|
bool
|
True if piece was spawned successfully, False if game over |
Source code in rl_tetris/core/game.py
move_piece ¶
Move the current piece by the given offset if valid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dx
|
int
|
Change in x position |
required |
dy
|
int
|
Change in y position |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if move was successful, False otherwise |
Source code in rl_tetris/core/game.py
rotate_piece ¶
Rotate the current piece clockwise if valid.
Returns:
| Type | Description |
|---|---|
bool
|
True if rotation was successful, False otherwise |
Source code in rl_tetris/core/game.py
hard_drop ¶
Drop the current piece to the lowest valid position.
Returns:
| Type | Description |
|---|---|
int
|
Number of cells the piece dropped |
Source code in rl_tetris/core/game.py
lock_piece ¶
Lock the current piece to the board and handle line clearing.
Returns:
| Type | Description |
|---|---|
Tuple[int, bool]
|
Tuple of (lines_cleared, is_game_over) |
Source code in rl_tetris/core/game.py
calculate_reward ¶
Calculate reward based on lines cleared and game state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines_cleared
|
int
|
Number of lines cleared |
required |
is_overflow
|
bool
|
Whether the piece caused overflow (game over) |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Reward value |
Source code in rl_tetris/core/game.py
Usage Examples¶
게임 시작¶
from rl_tetris.core import Board, Game
from rl_tetris.tetromino_queue import TetrominoQueue
from rl_tetris.randomizer import BagRandomizer
# 컴포넌트 생성
board = Board(height=20, width=10)
queue = TetrominoQueue(BagRandomizer())
game = Game(board, queue)
# 게임 시작
game.reset()
print(f"Current piece type: {game.current_piece.piece_type}")
피스 조작¶
# 피스 이동
if game.move_piece(dx=1, dy=0):
print("Moved right")
if game.move_piece(dx=0, dy=1):
print("Moved down")
# 피스 회전
if game.rotate_piece():
print("Rotated")
# 하드 드롭
rows_dropped = game.hard_drop()
print(f"Dropped {rows_dropped} rows")
게임 루프¶
game.reset()
while not game.gameover:
# 피스 조작
game.move_piece(1, 0)
game.rotate_piece()
# 하드 드롭 및 고정
game.hard_drop()
lines_cleared, is_game_over = game.lock_piece()
# 보상 계산
reward = game.calculate_reward(lines_cleared, is_game_over)
print(f"Lines: {lines_cleared}")
print(f"Score: {game.score}")
print(f"Reward: {reward}")
if is_game_over:
print("Game Over!")
break
스코어링¶
# 줄 클리어에 따른 점수
# 1줄: 100점
# 2줄: 300점
# 3줄: 500점
# 4줄 (Tetris): 800점
lines_cleared, _ = game.lock_piece()
print(f"Score: {game.score}")
print(f"Total lines: {game.cleared_lines}")
Scoring System¶
| Lines Cleared | Points |
|---|---|
| 1 | 100 |
| 2 | 300 |
| 3 | 500 |
| 4 (Tetris) | 800 |
Reward System¶
강화학습을 위한 보상:
- 줄 클리어:
lines_cleared²(1줄=1, 2줄=4, 3줄=9, 4줄=16) - 게임 오버: -10
- 일반 이동: 0
See Also¶
- Board - 보드 관리
- Piece - 피스 관리
- Tetris Environment - Gymnasium 환경