A modern, modular implementation of the classic 2048 game in C++. This project features a clean architecture that separates core logic from the presentation layer, offering both a Terminal/Console version and a graphical GUI version powered by Raylib with smooth animations.
- Modular Architecture: Separated into
core,game(logic),input,renderer(console), andgui(raylib) libraries. - Dual Interface:
- Console: Uses ANSI escape codes for colors and raw mode for direct input.
- GUI: Built with Raylib, featuring sliding animations, spawn effects (Zoom), and merge effects (Pop/Pulse).
- Modern C++: Written in C++20 standard.
- Unit Testing: Core logic is tested using GoogleTest.
- Cross-Platform Build: Managed via CMake with automated dependency fetching.
├── src/
│ ├── core/ # Game logic (Board, Tile merging, Rules) - No external deps
│ ├── input/ # Input handling (Unix raw mode / Abstract input)
│ ├── renderer/ # Console rendering implementation
│ ├── gui/ # Graphical rendering with Raylib (Animations, Theme)
│ ├── game/ # Main Console Game Loop
│ └── utils/ # Random number generators
├── tests/ # Unit tests for core logic
└── CMakeLists.txt # Root build configuration
- C++ Compiler: Support for C++20 (GCC, Clang, or MSVC).
- CMake: Version 3.15 or higher.
- Internet Connection: Required for CMake to fetch Raylib and GoogleTest automatically.
-
Clone the repository:
git clone [https://github.com/minhduc5a15/2048-cpp.git](https://github.com/minhduc5a15/2048-cpp.git) cd 2048-cpp -
Create a build directory and compile:
mkdir build cd build cmake .. cmake --build .
After building, the executables will be located in the bin/ folder inside your build directory.
Enjoy smooth animations and colors.
./bin/2048-guiControls: Use Arrow Keys or WASD to move.
Classic terminal experience.
./bin/2048-gameControls: WASD to move, Q to quit.
Verify the core logic integrity.
./bin/unit_tests- Animations: The GUI uses a state-based animation system. Moving tiles interpolate positions linearly, while spawning and merging tiles use
EaseOutBackandEasePopeasing functions for a polished feel. - Input Handling: The console version modifies
termiosto enable raw mode, allowing key presses to be detected immediately without hitting Enter.
This project is open-source and available for educational purposes