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.
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 SCORE: 0 ---- ---- ---- 2 ---- ---- ---- ---- 2 ---- ---- ---- ---- ---- ---- ---- (u)p, (d)own, (l)eft, (r)ight, (q)uit: u SCORE: 0 2 ---- ---- 2 ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 2 ---- (u)p, (d)own, (l)eft, (r)ight, (q)uit: r SCORE: 4 ---- ---- ---- 4 ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- 2 2 (u)p, (d)own, (l)eft, (r)ight, (q)uit: r SCORE: 8 ---- ---- ---- 4 ---- ---- ---- ---- 2 ---- ---- ---- ---- ---- ---- 4 (u)p, (d)own, (l)eft, (r)ight, (q)uit: u SCORE: 16 2 ---- ---- 8 ---- ---- ---- ---- 2 ---- ---- ---- ---- ---- ---- ---- (u)p, (d)own, (l)eft, (r)ight, (q)uit: q Quitting.
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.
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 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:
def __init__(self, upperLeft, sideLength, value):where the parameters specify the Point of where the upperLeft corner of the tile will go, the sideLength of each of the tiles (which are square), and the numerical value of the tile.
if value <= 0 or (value & (value-1)) != 0: # this number is not a power of 2If the value is not a power of two, set the value of the Tile to be 2.
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.
Value | Tile Color | Font Color | Font Size |
2 | #eee4da | #776e65 | 45 |
4 | #ede0c8 | #f9f6f2 | 45 |
8 | #f2b179 | #f9f6f2 | 45 |
16 | #f59563 | #f9f6f2 | 45 |
32 | #f67c5f | #f9f6f2 | 45 |
64 | #f65e3b | #f9f6f2 | 45 |
128 | #edcf72 | #f9f6f2 | 45 |
256 | #edcc61 | #f9f6f2 | 45 |
512 | #edc850 | #f9f6f2 | 45 |
1024 | #edc53f | #f9f6f2 | 35 |
2048 | #edc22e | #f9f6f2 | 35 |
4096+ | #3c3a32 | #f9f6f2 | 30 |
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
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.)
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.