Skip to content

Repository files navigation

Tetris Game

A console-based Tetris game built with C# .NET 8 using Test-Driven Development (TDD).

Features

  • Classic Tetris gameplay with all 7 standard pieces (I, O, T, S, Z, J, L)
  • Simple character-based UI using [ ] blocks
  • 4-state rotation system for all pieces
  • Line clearing with gravity (pieces drop down)
  • Score tracking (100 points per line, with bonus for multiple lines)
  • Fixed fall speed gameplay
  • Clean architecture with separated game logic and console UI

Project Structure

TetrisGame/
├── src/
│   ├── TetrisGame.Core/          # Game logic (domain layer)
│   │   ├── Position.cs            # Position record for coordinates
│   │   ├── TetrominoType.cs       # Enum for 7 piece types
│   │   ├── Tetromino.cs           # Tetromino class with rotation
│   │   └── GameBoard.cs           # 20x10 game board with collision detection
│   └── TetrisGame.Console/        # Console UI layer
│       ├── ConsoleRenderer.cs     # Display rendering
│       ├── GameEngine.cs          # Main game loop and logic
│       └── Program.cs             # Entry point
└── tests/
    └── TetrisGame.Core.Tests/     # Unit tests for core logic
        ├── PositionTests.cs
        ├── TetrominoTests.cs
        ├── GameBoardTests.cs
        └── LineClearingTests.cs

How to Run

  1. Navigate to the project directory:

    cd C:\dev\AgentsGame
  2. Run the game:

    dotnet run --project src\TetrisGame.Console

Controls

  • ← → : Move piece left/right
  • : Soft drop (move down faster)
  • SPACE : Rotate piece clockwise
  • ESC : Quit game

Running Tests

Run all unit tests:

dotnet test

Run tests with detailed output:

dotnet test --verbosity normal

Game Rules

  • The game board is 20 rows tall by 10 columns wide
  • Pieces fall at a fixed speed
  • When a horizontal line is completely filled, it is cleared
  • Score 100 points per line cleared
  • Multiple lines cleared at once give bonus points (e.g., 2 lines = 400 points)
  • Game ends when a new piece cannot be placed at the top

Test Coverage

The core game logic has comprehensive test coverage including:

  • Position Tests: Coordinate movement and equality
  • Tetromino Tests: All 7 piece types and their rotations
  • GameBoard Tests: Grid management, piece placement, collision detection
  • Line Clearing Tests: Complete line detection, removal, and gravity

All 26 unit tests pass successfully.

Development Approach

This project was built using Test-Driven Development (TDD):

  1. Write failing tests first (Red)
  2. Implement minimum code to make tests pass (Green)
  3. Refactor while keeping tests green (Refactor)

The clean separation between core game logic (TetrisGame.Core) and console UI (TetrisGame.Console) makes the code:

  • Easy to test
  • Easy to maintain
  • Portable to other UI frameworks if needed

Technical Details

  • Framework: .NET 8
  • Language: C# 12 with modern features (records, pattern matching, etc.)
  • Testing: xUnit
  • Architecture: Clean architecture with domain-driven design principles

Future Enhancements

Potential improvements for the future:

  • Variable fall speed based on level
  • Preview of next piece
  • Hold piece functionality
  • High score persistence
  • Sound effects
  • Color-coded pieces
  • T-spin detection
  • Hard drop feature

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages