A simple Tetris game implemented in Java.
- 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
- 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
- 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.
- 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
First, install the project:
make installRun the game by providing input through STDIN:
make run < ./input.txt- After processing all moves, the program prints the height of the tallest column.
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 testBehavior-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
- Cucumber Reports: Automatically generated and published online after test execution
- JUnit Reports: Available in
target/surefire-reports/
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.txtOutput Example:
5
2
- Java 11+
- Make
- Maven
- JUnit 5
- Cucumber for BDD