Old as this world but popular as openai api, classic game 2048. The original is here : click
Done
-
The game board is 4x4 (16 cells), but it can be easily adjusted to any size in the code.
-
Each cell can be empty or contain a number from the sequence: 2, 4, 8, ... 2^n.
-
The game begins with 2 cells filled with either a 2 or a 4.
-
Click the Start button to begin the game.
-
Use the keyboard arrows to move the cells.
-
All numbers move in the chosen direction until all empty cells are filled.
-
Two identical cells merge into a doubled number.
-
A merged cell can't merge again in the same move.
-
A move is valid if at least one cell changes.
-
After each move, a 2 or 4 appears in a random empty cell, with a 10% chance of being a 4.
-
When a cell reaches the value of 2048, a win message is displayed.
-
The game ends if no moves are available.
-
The score increases by the total of all merged cells.
-
The highest score is saved in local storage.
-
Click the Restart button to start a new game.
git clone
npm i
npm start
Creating a 2048 game in vanilla JavaScript was a challenging and rewarding task that improved my algorithmic and OOP skills. Setting up a dynamic 4x4 grid and implementing cell movements and mergers based on arrow key inputs enhanced my coding and problem-solving abilities.
Managing the game state, including scores, win/loss detection, and real-time UI updates, taught me efficient DOM manipulation. Adding user-friendly features like Start and Restart buttons and saving high scores in local storage made the game more engaging.
Overall, this project deepened my understanding of JavaScript and increased my confidence in tackling complex coding challenges. Keep in touch 👨🦲