Skip to content

wolfymmm/2048-game

Repository files navigation

🧩 2048 Game

A fully interactive, responsive, and pixel-perfect implementation of the famous 2048 puzzle game. Built from scratch using modern Vanilla JavaScript (ES6+ Class architecture), object-oriented programming (OOP) principles, and semantic component design.


Build Status License JavaScript HTML5 SCSS


DEMO

Live Demo

Game Rules & Mechanics

The game is played on a smooth 4 × 4 grid using your keyboard arrow keys:

  • Movement: When a direction is chosen, all numbered tiles slide as far as they can in that direction.
  • Merging: Two tiles of the same number collide and merge into a single tile with a doubled value (e.g., $2 + 2 \rightarrow 4$).
  • Mathematical Restriction: A tile cannot be merged more than once during a single turn.
  • Random Spawning: After every successful move, a new tile randomly spawns in an empty cell. It has a $90%$ chance of being a 2 and a $10%$ chance of being a 4.
  • Scoring: Your score increases dynamically by the exact sum of all combined tiles during that turn.
  • Win Condition: The game triggers a victory screen as soon as a 2048 tile is achieved.
  • Game Over: Triggers automatically when the board is completely full and no valid adjacent merges remain.

Architecture & Codebase Design

The project is strictly separated into two distinct layers (following decoupled UI/Logic principles):

  1. Core Logic Engine (Game.class.js): A stateful, framework-agnostic JavaScript class that completely handles data structures, board state array transformations, merge vectors, probability matrix calculation, and win/loss assertions.
  2. UI & Event Management Layer (main.js): Directs raw DOM manipulation, keydown event handlers, dynamic class rendering (field-cell--%value%), screen state management (start, restart, modulations), and modal injections.

Obligatory API Specifications:

  • constructor(initialState) — Instantiates core properties with optional predefined matrix states.
  • getState() / getScore() / getStatus() — Public getter APIs ensuring decoupled data tracking.
  • moveLeft() / moveRight() / moveUp() / moveDown() — Structural grid translation algorithms.
  • start() / restart() — State reset configurations.

Gameplay Preview

image


Installation & Local Setup

Follow these steps to run the project locally:

1. Clone the repository

git clone https://github.com/wolfymmm/2048-game.git

2. Navigate to the project directory

cd 2048-game

3. Install dependencies

npm install

4. Start the development server

npm start

5. Open the application

After starting the development server, open your browser and navigate to:

http://localhost:8080

The page will automatically reload whenever you make changes to the source files.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors