Skip to content

FFFra/tetris-game-resolver

Repository files navigation

Tetris Game Resolver

A TypeScript implementation of a simplified Tetris game resolver that processes a sequence of tetromino pieces and outputs the maximum height of the remaining blocks in the grid.

Project Structure

├── src/                  # Source code
│   ├── tetris.ts         # Main game logic
│   ├── types.ts          # TypeScript type definitions
│   ├── constants.ts      # Game constants
│   └── test.ts           # Test cases
├── data/                 # Data files
│   ├── games.txt         # Sample input
│   ├── output.txt        # Sample output
│   ├── test-games.txt    # Test inputs
│   └── test-output.txt   # Expected test outputs
└── package.json          # Project configuration

Prerequisites

  • Node.js (v14 or higher)
  • npm (comes with Node.js)

Installation

  1. Clone the repository
  2. Install dependencies:
npm install

Running the Application

The program reads from STDIN and writes to STDOUT:

# Basic usage
ts-node src/tetris.ts

# With input redirection
ts-node src/tetris.ts < data/games.txt

# With output redirection
ts-node src/tetris.ts < data/games.txt > data/output.txt

# Using npm start (adds '> tetris-game@1.0.0 start' headers to output)
npm start < data/games.txt > data/output.txt

Input Format

Each line of input represents one game. A game consists of a series of tetromino pieces and their starting X coordinates:

"L2,J4,O1,L6,J8"

Where:

  • The letter (L, J, O, etc.) represents a tetromino piece type
  • The number represents the starting X coordinate for that piece

Examples:

  • "O0,O2,O4,O6,O8" (outputs 0 - two rows removed)
  • "L0,I2,O4,L6,J8" (outputs 2 - one row removed)
  • "T0,Z3,T5,O2,O8" (outputs 3 - no rows removed)

Output

For each game (input line), the program outputs a single integer representing the maximum height of blocks remaining in the grid after all pieces have fallen.

Running Tests

Run the tests with:

npm test

Code Design

The code is structured with:

  • Functional Programming Approach: Using map, reduce, filter, and other functional patterns
  • Strong Type Safety: TypeScript types for all game elements
  • Clean Separation of Concerns:
    • Game logic (tetris.ts)
    • Type definitions (types.ts)
    • Constants and game parameters (constants.ts)
    • Tests (test.ts)

Available Scripts

  • npm start - Run the application
  • npm test - Run tests
  • npm run lint - Check code style
  • npm run lint:fix - Fix code style issues
  • npm run format - Format code with Prettier

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors