2048 is the newest game to really go viral. No one even remembers Flappy Bird anymore, now it's all 2048 all day long. So what is 2048? It's a sliding puzzle game you can play here. And it's open source and up on Github. It can be summed up as this: you have a 4x4 grid of numbers and empty spaces. Slide all the tiles in any direction and any times of the same value that touch combine into a single tile. Your objective is to make larger and larger tiles, all the way up to (and beyond) 2048. Spend a few minutes playing the game and you'll see, it's a simple game.
Read the whole series at Cloning 2048 in Ruby
All the important methods were already talked about in the previous articles. We really need to talk about spawning new cells and seeing if we've met the game over condition though.
Read more at: The Final Few Methods
The slide method is the core of the whole 2048 game. It slides all the rows to the left, collapsing any empty tiles and combining any adjacent tiles with the same value. Remember that this method onlyslides the puzzles to the left. In order to slide the puzzle to the right, you need to rotate the puzzle twice (so it's essentially upside down, and left is right), slide the tiles and rotate the puzzle twice again. Similarly, to move up and down you rotate, slide and rotate again (always completing four rotations total).
Read more at: The Core Algorithm
Now that we have the algorithm sorted out, the ability to create two dimensional arrays and to rotate two dimensional arrays, it's time to start looking at the actual code. The code can be found on this gist, be we'll be pasting relevant parts of it here into the article. So if you want to see it in action or play around with it, get it from the gist.
Read more at: The Board Class