Tetris Clone — Java (Console, OOP)
A console-playable Tetris built with clean object-oriented design: clear class boundaries, testable logic, and small reusable components (factory, strategy, state). Focused on readability and maintainability.
Overview
This Tetris clone runs entirely in the console with an update loop that processes input, applies gravity, checks collisions/line clears, updates score & level, and renders the board as ASCII. The codebase is structured around small, single-responsibility classes to keep logic testable and modular.
Core Features
- Seven distinct tetrominoes (I, O, T, S, Z, J, L) with rotation & wall-kicks.
- Soft drop / hard drop, hold (optional), and next-queue preview.
- Line clear detection (single/double/triple/Tetris) with classic scoring & level-up pacing.
- 7-bag randomizer for fair piece distribution (seedable for tests).
- Pause / resume; top-out detection; high-score persistence (optional).
Controls (Console)
← / →
move left/right↑
rotate (clockwise),Z
rotate (counter-clockwise)↓
soft drop,Space
hard dropP
pause/resume,Q
quit
Object-Oriented Design
- Game: main loop (input → update → render), tick timing, state transitions.
- Board: grid state, collision checks, line-clear detection & compaction.
- Tetromino (entity) + Shape/Rotation utilities.
- PieceFactory: creates pieces via 7-bag Randomizer.
- InputHandler: maps keys to commands (debounce/repeat).
- Renderer (console): draws board & HUD using ASCII.
- ScoreManager / LevelManager: scoring, gravity speed, level rules.
- GameState: Running • Paused • GameOver (state pattern).
Patterns Applied
- Factory — central piece creation with a 7-bag source.
- Strategy — rotation/wall-kick rules swappable (e.g., basic vs. SRS-like).
- State — game states encapsulate allowed actions & rendering.
- Command — input → discrete commands for testable moves.
- Observer (optional) — events for line clears / score updates.
Testing
- Unit tests for collision, rotation/wall-kicks, line clears, scoring, and randomizer fairness.
- Seeded 7-bag to reproduce scenarios; deterministic step-through of the game loop.