1. Computing
Send to a Friend via Email
Michael Morin

Michael's Ruby Blog


Follow me on:

Cloning 2048 in Ruby

Monday March 31, 2014

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

Cloning 2048 in Ruby: The Final Few Methods

Monday March 31, 2014

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

Cloning 2048 in Ruby: The Core Algorithm

Monday March 31, 2014

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

Cloning 2048 in Ruby: The Board Class

Monday March 31, 2014

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

Cloning 2048 in Ruby: Rotating a Two Dimensional Array in Ruby

Monday March 31, 2014

In order to implement our 2048 clone, we need to be able to rotate a two dimensional array. What do we mean by rotate? We want to turn the entire puzzle clockwise by 90 degrees. So in the example below, array A turns into array B.

a) [ [1,2],    b) [ [3,1],
     [3,4]          [4,2]
   ]               ]

Read more at: Rotating a Two Dimensional Array in Ruby.

Cloning 2048 in Ruby: Two Dimensional Arrays in Ruby

Monday March 31, 2014

Now that we know how the algorithm will work, it's time to think about the data this algorithm will work on. There are two main choices here: a flat array of some kind, or a two dimensional array. Each have their advantages, but before we make a decision, we need to take something into account.

Read more at: Two Dimensional Arrays in Ruby

Cloning 2048 in Ruby: The Algorithm

Monday March 31, 2014

Before we can really start laying down some code, we need to know how we're going to be working with our data. I went through a few iterations of this (including one failed iteration I'll outline below), and there are definitely easy ways and hard ways to do this. If your data is structured in the wrong way, it just makes everything hard.

Read more at: The Algorithm.

Finishing the Project

Friday February 28, 2014

Well, this road was longer than I expected.  In this article, we'll literally just staple the work from the previous articles to finish off the project.

AJAX With Sinatra

Friday February 28, 2014

AJAX is a powerful technique that allows you to make HTTP requests from an HTML page without having to reload the entire page.  The results can even be inserted into any point of the page that you'd like, allowing you to make very dynamic web applications that behave like real applications, instead of static web pages.  In this article, we'll look at how to implement an AJAX web interface with jQuery and Sinatra.

Sinatra Web Applications in JRuby

Friday February 28, 2014

Sinatra is hands down one of the most useful libraries in Ruby for smaller web applications.  Rails is great, but it's...  huge.  Sinatra is small, Sinatra is fast, and it takes just minutes to learn.  We'll look at creating web applications in Sinatra running on jRuby.

©2014 About.com. All rights reserved.