githubEdit

18. Design Tetris

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:

  1. Timer fires (e.g., every 500ms).

  2. Active Piece moves down 1 unit.

  3. 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:

  1. Player presses 'Up/Rotate'.

  2. System calculates new coordinates for the shape.

  3. 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

spinner

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(); }."


SOLID Principles Checklist

  • S (Single Responsibility): Board manages grid state, Tetromino manages shape logic.

  • O (Open/Closed): Add new Shapes to Factory without changing Game logic.

  • L (Liskov Substitution): N/A.

  • I (Interface Segregation): N/A.

  • D (Dependency Inversion): Game depends on Tetromino abstraction (if made abstract/interface).

Last updated