Thanks to visit codestin.com
Credit goes to github.com

Skip to content

kvatz/tetris-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tetris Game

A simple Tetris game implemented in Java.

Project Structure

  • controller: Handles game flow (start, play piece, get height, print grid)
  • model: Contains core classes (Grid, Piece, PieceType, RowLevel)
  • repository: Manages grid state persistence
  • service: Implements game logic (piece placement, row clearing, height calculation)
  • util: Utility classes/constants

Code Overview

  • Following SOLID principles
  • Piece abstraction for easy addition of new pieces of different shapes
  • Individual classes for each piece type, supports easy extension for other features like rotation etc.
  • Separation of concerns between game logic and data management
  • Data encapsulation in model classes
  • Used Adapter pattern for repository interface
  • Factory pattern for piece creation
  • Strategy pattern for piece placement logic
  • Command pattern for game commands
  • Extensive unit and component tests

Features

  • 7 Piece Types: Q, Z, S, T, I, L, J (no rotation, each with a unique shape)
  • Row Clearing: If an entire row is filled, it gets cleared.
  • Piece Drop: Pieces enter from the top and come to rest at the bottom or on top of another piece.
  • Grid Size: 10 units wide, up to 100 units tall.
  • Height Calculation: After every piece drops, the program returns the height of the tallest column.
  • Command Line Interface: Program is invoked from the command line, reading input from a file or STDIN and outputting to STDOUT.

Input Format

  • Each line of the input file is a comma-separated list.
  • Each entry is a single letter (piece type) and a single digit integer (column).
  • The integer represents the column where the piece will be dropped.
  • The letter represents the type of piece.
  • Example input line:
    Q0,Z1,S2,T3,I4,L5,J6

Usage

Build and Install

First, install the project:

make install

Run

Run the game by providing input through STDIN:

make run < ./input.txt

Output

  • After processing all moves, the program prints the height of the tallest column.

Testing

Unit Tests

The project includes comprehensive unit tests for both TetrisController and TetrisEngine:

  • Testing piece placement and movement
  • Row clearing validation
  • Score calculation
  • Edge cases and error conditions

Run unit tests with:

make test

Component Tests (Cucumber/Gherkin)

Behavior-Driven Development (BDD) tests are implemented using Cucumber:

  • Feature files in natural language (Gherkin syntax)
  • Covers end-to-end game scenarios
  • Validates game mechanics through real gameplay sequences
  • Input file processing validation

Key test scenarios include:

  • Single piece placement
  • Multiple piece interactions
  • Full row clearing
  • Complex game sequences

Test Reports

  • Cucumber Reports: Automatically generated and published online after test execution
  • JUnit Reports: Available in target/surefire-reports/

Example

Input (input.txt):

Q0,Z1,S2,T3,I4,L5,J6
Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8

Commands:

make install
make run < ./input.txt

Output Example:

5
2

Development

  • Java 11+
  • Make
  • Maven
  • JUnit 5
  • Cucumber for BDD

About

Tetris game in Java

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published