2048 Gameplay

Click Here to Download this Answer Instantly

In this assignment, you will incrementally develop a program to play the game 2048. This assignment is designed to help you understand how to compartmentalize your code into isolated functioning components and then bring them together to achieve the main task. While the recommended approach you should take for this task has been described for you, the skeleton code provided will allow you to harness your creativity whilst placing certain restrictions to help you meet the program specifications.

2048 Gameplay

As introduced above, 2048 is a tile-sliding puzzle game where the objective is to maximize your score. The game is (usually) played on (4 x 4) grid of tiles. Each tile can be a blank tile or be marked with a value that is a power of 2. Figure 1 illustrates what a typical 2048 state (which we will call s0from here on) looks like mid-play.

During each move, the player can take one of four possible actions: {LEFT, RIGHT, UP, DOWN} which collapses the grid left, right, up or down respectively. When the grid is collapsed in any direction, all the numbers shift to blank tiles in that direction till they reach the end of the table or reach a tile with a number in it already. During the shift, identical numbers are added together, and different numbers stack next to each other. For example, for state s0, if the player chooses the UP action, the table will be collapsed upward resulting in the new state, s’0¬†¬†shown in Figure 2.

In this states’0 ( Figure 2), we can see that the 2 in B2 moved up to A2 and the 8 in A3 remained where it was as it was at the topmost end of the column initially. The 4 in A4 also did not move but when the 4 in C4 moved up, it was added to the first one since identical numbers are merged (summed up). The 2 in D4 moved up to B4. However, if a tile with a number was already merged with an identical number during one round, it will not be merged again with another identical tile during the same move.

In the example above, when sliding to the left the row on the left, it results in the row on the right. The two 4s are merged to give an 8, but now the two resulting 8s are not merged in this current move.

How do we score?

Points are scored for any additions that occur during a move with the value being equal to the sum of the addition i.e., the action to go from s0to s’0 in Figures 1 and 2, results in 8 points. After each move, a (2) or a (4) is placed on a random empty tile, therefore over time the number of blank tiles decreases. Please note that a (2) or (4) will appear on a random empty tile only if there is change in the status of the grid i.e. there is a slide or slide and merge. The player “wins” if a tile with (2048) is formed, however, the game does not stop. The game stops when the grid is no longer collapsible in any of the four directions (when there are no legal actions available.).

To get used to the rules of the game you can play the game athttps://gabrielecirulli.github.io/2048/.

Tasks to do

In this assignment you will be building a program to play 2048 piece by piece. To do so you are provided with a skeleton code in the file game.py. This file contains two classes: Grid and Game. You will be working (primarily) in the Grid class. This class contains attributes and methods that allow you to perform actions involving the grid of the game. The __init__() method is parameterized by:

row, the number of rows of the grid (default value of 4);

col, the number of columns;

initial, the number of random initial tiles filled at the beginning of the game.

This class also contains the attribute

score, the score of the game

emptiesSet, a list of lists which contains the set of all empty tiles of the grid. Each list in emptiesSet contains the row index and column index of a grid tile if that tile is empty. For example, an empty grid of dimensions 4 x 4 would produce an emptiesSet of [[0, 0], [0, 1], …, [3, 3]].

Finally, the class contains the grid attribute, which is the variable that stores the grid/state of the game and it is initialized by createGrid().

Task 1

Your first task will be to implement the method createGrid() that takes two parameters row and col and returns the grid for the game. You will be using a list of lists to represent the grid. The list should contain row number of lists, each with a length col. All the elements of this data structure should be 0. Once you are done implementing the method, comment out the line that calls the main function and append the following lines to the bottom of your program:

grid = Grid()


If your method is implemented correctly, you should see the following output:

[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]

Task 2

Implement the method updateEmptiesSet(). This method is used after every collapse action (see below) in the game. The method should update the emptiesSet list to contain the new set of empty cells after a collapse. Tests for this method can be found in the check.py file (described below.)

Task 3

Implement the collapsible() method. This method should return True if the grid can be collapsed in any of the four directions. It should return False otherwise. Uncomment the line near the bottom that calls the function testCollapsible() and run the file with Python. If your implementation is correct it should print that all the tests have passed.

Hint: One of the tests that should be done to determine if the grid is collapsible or not is to find if there is an empty tile in the grid.

Task 4

In Task 4 you must complete the collapseRow() method. This method is given a list of numbers as an argument and it should return a LEFT-collapsed list, and a True if the list was collapsed or False if it was not. The code for your method must collapse the list conforming to the rules of 2048. That is, if it is passed the list [2, 0, 2, 4], after collapsing to the left the method should return the list [4, 4, 0, 0] and the bool value True, that is, two comma-separated return values. If the numbers are merged during the collapse process, then the score must be updated by the result of the addition. For example, in the above example if initial score was 0, then the score after the collapse should be 4. In order to test your implementation, you can use the check.py file that is given to you. Uncomment the line near the bottom that calls the function testCollapseRow() and run the file with Python. If your implementation is correct it should print that all the tests have passed. Please note that the testCollapseRow() ONLY checks whether the collapsing has occurred in the list correctly or not. It does not test if the score is updated correctly.

Task 5

In this task, you will use the collapseRow() method to implement collapseLeft(), collapseRight(), collapseDown() and collapseUp(). For collapseLeft(), you merely need to collapse every row of the grid using collapseRow(). collapseRight() is implemented similarly except, in this case, you would reverse each row before collapsing them. Use this idea, to implement collapseDown() and collapseUp(). All four methods should return True if the grid was collapsed and False otherwise. The check.py file contains tests for collapseLeft(), collapseRight(), collapseUp() and collapseDown() that you can use to check your implementation. The methods must also update the score after a collapse in any direction.

Running Your Code

Once you are finished implementing all the methods successfully, erase/comment any extra test code you have in game.py. Uncomment the line of code that calls the main() function. Running the program now should run 2048 in the shell. Instructions for playing are given when the game is run.