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.
├── 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
- Node.js (v14 or higher)
- npm (comes with Node.js)
- Clone the repository
- Install dependencies:
npm installThe 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.txtEach 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)
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.
Run the tests with:
npm testThe 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)
npm start- Run the applicationnpm test- Run testsnpm run lint- Check code stylenpm run lint:fix- Fix code style issuesnpm run format- Format code with Prettier