2048 — Javascript game development

Govardhan Srinivas
2 min readApr 26, 2020

--

This game I have developed has an interesting start let me give a brief before we go forward. As the country is going around Quarantine lockdown to reduce the spread of coronavirus, hence my activities are restricted within my home. Instead of getting glued to Netflix, Amazon Prime, or TV today I started to browse LinkedIn and found an interesting coding challenge #7days7websites by Florin pop. It started an itch of coding that I should also spend my day in something useful and attempt this challenge.

I scribbled few options on a piece of paper, later decided to start with something that I never attempted ‘Game development’ this would be interesting and also intuitive that can be used to get away with my current boredom. :)

Here are some of the links where you can find the git codebase and live demo links.

After deciding the game, I started to scribble the algorithm to be implemented. Below you can find the logic I used to code.

  • Based on matrix size, Generate a 2d matrix with a default value of zeros
  • Fill a random cell with a number
  • Capture keyboard arrow events
  • Based on the key pressed(Up/Down/Left/Right) respond with updated matrix
  • Loop over the matrix either from start/end of the matrix based on key pressed
  • If i & i+1 are the same number, then sum up the result for i+1, also set i to zero
  • If i+1 is of value zero, then swap the number position
  • During swapping if you find zero in-between, then bubble up zero to top
  • If i & i+1 are different or than zero, then do nothing

After the implementation of the above, it started to get into shape, Updated some styling to look & feel good. Finally with a bit of hiccup here and there during development was able to host in netlify and made my 1st day submission to the challenge.

Screenshot of the submitted app
Screenshot of the live site

I ended my day with a positive feeling. :)

--

--