1. Technology

Writing a 2048 Clone in Ruby

In this series, we'll create a complete 2048 clone in Ruby, from scratch.  We'll discuss the algorithm and all details involved in implementing that algorithm in Ruby.  We'll be using a two dimensional array, rotating that two dimensional array and doing many other array operation. 

The Algorithm
Before we start coding, we take some time to look at the actual game of 2048.

Two Dimensional Arrays in Ruby
We'll use a two dimensional array to represent the 2048 game board, but how do we create a 2D array in Ruby?

Rotating a Two Dimensional Array in Ruby
In order to implement the 2048 algorithm cleanly, we'll need to know how to rotate a 2D array by 90 degrees in Ruby.

The Board Class
Before we dive into the heavy stuff, we'll look at the array we use to store the board, as well as how to print the board to the console and a few other things.

The Core Algorithm
Finally, onto the meat of 2048. We take a look at how the board is rotated and slid to implement the game of 2048.

The Final Few Methods
Now that the core of the game is done, we take a few minutes and look at a few odds and ends. Among those, the code that spawns new tiles and the code that looks for the game over state.

Cloning 2048 in Ruby
In this series of articles, a complete clone of the game 2048 is written from scratch.

You can opt-out at any time. Please refer to our privacy policy for contact information.

©2014 About.com. All rights reserved.