Panagiotis Dimtsoudis Electrical Engineering, Penn
← All projects

Java Tetris

Software systems · Java · event-driven design

A playable Java Swing Tetris I wrote solo for CIS 1200, with a 20 × 18 board, explicit game states, file-based save/load, JUnit tests, and Easy-mode undo.

Course CIS 1200
Status Completed
Platform Java desktop application
Tools Java 17, Swing, Maven, JUnit

Objective

I wrote a desktop Tetris game in Java for CIS 1200, on my own. The finished application has menu states, scoring, pause behavior, save/load, high-score display, and undo in Easy mode.

Software architecture

I split it into model, rendering, input, and persistence so each piece could be tested on its own. TetrisGame manages gameplay state and timers. GamePanel renders the Swing interface, InputHandler maps keys to state-aware actions, and SaveManager handles file I/O.

Model TetrisGame

Game state, timers, scoring, current and next pieces, undo snapshots.

View GamePanel

Swing rendering for board, active piece, next piece, score, menus, overlays.

Input InputHandler

Keyboard routing for menu, playing, paused, game-over, and high-score states.

Persistence SaveManager

Human-readable autosave file for state restore and top-score loading.

Board and game logic

  • GameBoard stores the board as a 20 × 18 two-dimensional integer grid.
  • Four invisible wall columns on each side use -1 values for boundary handling.
  • TetrisGame.canMove validates movement and rotation by checking bounds and occupied cells.
  • Placed tetrominoes are written into the grid by piece ID, which also drives rendering color.
  • Full rows are detected, cleared, and shifted downward by clearFullRows.

Input and state management

The game uses explicit states: MENU, COUNTDOWN, PLAYING, PAUSED, GAME_OVER, and VIEWING_SCORES. Keyboard behavior changes based on the active state, which keeps menu navigation, gameplay controls, saving, loading, pausing, and restart behavior from collapsing into one input path.

  • Arrow keys move and rotate the active piece during gameplay.
  • Space drops the piece instantly.
  • P pauses or resumes depending on state.
  • S saves during gameplay or while paused.
  • L loads a saved game from the menu.
  • H opens high scores, and Escape returns from the high-score view.

Persistence and undo

Persistence is implemented through SaveManager, which writes the game state, difficulty, score, top scores, board grid, and current/next tetromino data to autosave.txt. Undo is implemented in Easy mode using a Deque of snapshots containing the board grid, current piece, next piece, and score before placement.

Testing and debugging

The repository includes JUnit tests for core logic in TetrisTest.java, including row clearing, score updates, tetromino rotation, and factory output validation.

Run locally

The project builds with Maven and uses the exec plugin to run org.cis1200.Game. Java 17 is required by the Maven configuration.

cd "Tetris Full Project"
mvn compile
mvn exec:java