Difficulty: Medium Topics: Matrix Manipulation, Game Loop, Factory Pattern Key Concepts: Rotation Matrix, Collision Detection, Game Loop.
Phase 1: Requirements Gathering
Goals
Design the core logic for Tetris.
Handle different shapes (Tetrominoes), rotation, movement, and line clearing.
1. Who are the actors?
Player: Controls the active piece.
Game Loop: Forces gravity (ticks).
2. What are the must-have features? (Core)
7 Shapes: I, O, T, S, Z, J, L.
Movement: Left, Right, Down (Soft Drop), Rotate.
Game Physics: Gravity, Collision detection (walls, other blocks).
Clearing: Full rows disappear, blocks above fall down.
3. What are the constraints?
Grid Size: Standard is 10 cols x 20 rows.
Rotation: 90 degrees clockwise.
Phase 2: Use Cases
UC1: Game Tick (Gravity)
Actor: System Flow:
Timer fires (e.g., every 500ms).
Active Piece moves down 1 unit.
System checks collision.
If Collision: Undo move, Lock piece to board, Check for full lines, Spawn next piece.
If No Collision: Update Display.
UC2: User Rotate
Actor: Player Flow:
Player presses 'Up/Rotate'.
System calculates new coordinates for the shape.
System checks if new coordinates collide with walls/blocks.
If Safe: Apply rotation.
If Collision: Ignore input (or "Wall Kick" - advanced feature).
Phase 3: Class Diagram
Step 1: Core Entities
TetrisGame: Main controller.
Board: N*M grid state.
Tetromino: The active piece (Strategy for shape).
Point: Helper class.
UML Diagram
Phase 4: Design Patterns
1. Factory Pattern
Description: A creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
Why used: TetrominoFactory centralizes the complex logic of creating different shapes (I, L, Z, T) with their specific initial coordinates and colors. The Game Loop simply asks for a "Random Piece".
2. Command Pattern
Description: Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Why used: (Optional) Mapping user inputs (Up, Down, Left, Right) to Command objects allows for remapping controls easily and implementing an "Undo" feature for debugging or training AI agents.
Phase 5: Code Key Methods
Java Implementation
Phase 6: Discussion
Rotation Logic
Q: How does rotation math work?
A: "Basic Linear Algebra. Rotating a point $(x, y)$ 90 degrees around origin $(0,0)$ results in $(y, -x)$. Since our shapes store relative coordinates to a center pivot, we just apply this transform to every point in the shape list."
Collision
Q: Optimal collision detection?
A: "Since grid is small (10x20) and shape is small (4 blocks), checking all 4 blocks against the grid array O(1) is extremely fast. No need for QuadTrees."
Concurrency
Q: What if user presses 'Rotate' exactly when Gravity tick happens?
A: "Game Loop pattern usually handles input and updates sequentially in a single thread to avoid race conditions. while(running) { handleInput(); update(); render(); }."