CS21 Lab 12: 2048

Due 11:59pm Saturday, April 26th

For this lab you will write one class and use two others to help you design a graphical version of the popular game 2048. If you are unfamiliar with the game, you can play it online to get good idea of the rules of the game.

We give you quite a lot of code for this lab, but you will need to learn how to use the code we give you and then add to it in order to complete the lab. Be sure to comment your functions, classes and methods, and be sure to test incremetally.

First, run update21, if you haven't already, to create the cs21/labs/12 directory. Then cd into your cs21/labs/12 where you will find the starter files you will need for this week's lab. You should not need to create any new files.

The game 2048 and 2048_text.py

In order to understand how to complete the lab, you need to understand how to play 2048. If you've never played, the best way to learn is to play online. The remainder of the lab assumes that you know how to play. If you don't understand the game after playing once or twice, be sure to ask!

We give you a fully functional version of the game 2048. However, in its current state, it is a text-based game. Although it works, it's not nearly as fun as the graphics version. You should try out the text-based version by running the program in 2048_text.py. Note that the output below may differ from yours because the starting condition of the game is random.

$ python 2048_text.py
---- ---- ----    2
---- ---- ---- ----
   2 ---- ---- ----
---- ---- ---- ----
(u)p, (d)own, (l)eft, (r)ight, (q)uit: u
   2 ---- ----    2
---- ---- ---- ----
---- ---- ---- ----
---- ----    2 ----
(u)p, (d)own, (l)eft, (r)ight, (q)uit: r
---- ---- ----    4
---- ---- ---- ----
---- ---- ---- ----
---- ----    2    2
(u)p, (d)own, (l)eft, (r)ight, (q)uit: r
---- ---- ----    4
---- ---- ---- ----
   2 ---- ---- ----
---- ---- ----    4
(u)p, (d)own, (l)eft, (r)ight, (q)uit: u
   2 ---- ----    8
---- ---- ---- ----
   2 ---- ---- ----
---- ---- ---- ----
(u)p, (d)own, (l)eft, (r)ight, (q)uit: q

If you look at the program in 2048_text.py, you'll see it's quite simple. First, we create an instance of the Game class by saying game = Game(). The game instance keeps track of where all the tiles are and the current score. If you tell the game to push your tiles up, down, left, or right, your tiles will move to the correct locations and merge with their neighbors if applicable. If you print the game, the text version of the board is printed, along with your score.

The Game class has a number of useful methods which are highlighted for you in the main() function in the 2048_text.py file. You can view all of the methods and the documentation for them here, or by running python, importing the game module, and getting help:

$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import game
>>> help(game)

Note that the code for the Game class is in game.py. You're welcome to read through it, but you will not need to understand how the code works nor will you need to make modifications to the code in order to complete this lab.

The Grid class and grid.py

In order to make a graphical representation of the game, you will need a Grid where you can place the game tiles. An empty Grid looks like this:

We have created a fully functional Grid class for you in the file grid.py. Like you did with the 2048_text.py, you can run the grid.py program to get some examples of how the Grid class works. All of the methods in the Grid class are demonstrated in the main() function of grid.py:

$ python grid.py

You can view the complete documentation for the Grid class here, or by running python, importing the game module, and getting help.

The Tile class and tile.py

The Tile object represents a Tile that you display on the Grid. Here is an example of two tiles placed on an otherwise empty grid. (Note that if you follow the style guidelines provided later on, you should get the same picture by running tile.py once you've implemented the Tile class.)

Writing the Tile class is up to you. You should define your class such that each tile maintains the following data (also called instance variables):

As we discussed in class, objects interact via methods. Your Tile class should define the following methods:

We have provided you with a skeleton of the Tile class in tile.py. You are welcome to add other methods to the class, but the ones we've listed are required.

If you'd like your game of 2048 to look like the game online, this table should help you. You're welcome to make your version of 2048 even snazzier than what's listed here.

ValueTile ColorFont ColorFont Size

Be sure to fully test your Tile class before moving on. One way you can test your tile class is by adding to the main() function we've given you in tile.py

Putting it all together

Once you've completed (and tested!) your Tile class, you'll want to use them to turn the text-based version of 2048 found in 2048_text.py into a graphics version of the game which you'll write in 2048.py.

Here are the things you'll need to do:

One final note: The only two files you should be editing for this lab are 2048.py and tile.py. The other files (2048_text.py, grid.py, and game.py should remain as we distributed them.)

Hacker Challenge
Once you are satisfied with your programs, hand them in by typing handin21 at the Linux prompt.

You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize, after handing in some programs, that you'd like to make a few more changes to them.