2048 Report
2048 Report
The project aims to simulates the board game 2048. As indicated in the project requirement:
Its output should always be a board in some state.
The game should start in a random state, with the two starting blocks somewhere on
the board.
The user can then use the current board as input, together with their next move
(up/down/left/right), with which the board is updated, and returned to the reader.
Make use of colors to make the game look attractive and easier to read.
Send the user a congratulatory message when they reach 2048, but the game may
continue if you want.
Method/Module Used:
1. Numpy
The most commonly used module while dealing the numbers, array (matrix). It contains
many useful functions such as numpy.array_equal(), numpy.random.randint(),
numpy.zeros(). Since in this project the number in the grid of 2048 are stored in matrix,
these functions from numpy make it convenient to perform matrix comparisons and
transformations.
2. Tkinter (tkinter)
Module for coding a user-friendly interface as introduced in class. It contains many
function such as create_text(), create_rectangle(), label(), button(), bind_al()l that makes
the user interface interactive.
Pseudocode/Diagrams/Examples
1. Add a new number to the board
o Function new_number():
1. (A function to generate number 2 or 4 since new number only appear
as 2 or 4)
2. Use 2 * np.random.randint(1, 3) to generate number 2 or 4.
3. Return: a number of 2 or 4.
o Function add_numer(current_matrix):
1. (A function to add random 2 or 4 to the board)
2. use np.random.randint(4) to generate random x and y axis.
3. Use new_number() to generate a new number 2 or 4 and place it on the
point (x, y). If this point is already taken, repeat the process above.
4. Return: a new matrix with an additional number.
PIC 16
Example:
More specifically:
Condition (index of number in a column) Result
[0] == [1] 2*[0], [2], [3]
[0] == [1] and [2] == [3] 2*[0], 2*[2]
[1] == [2] [0], 2*[1], [2]
[2] == [3] [0], [1], 2*[2]
[0] == [3] and [1] == 0 and [2] == 0 2*[0]
[0] == [2] and [1] == 0 2*[0], [3]
PIC 16
Example:
o Function game_over(m_matrix)
1. (Input the current matrix and check whether there are valid moves
remaining)
2. Put the current matrix into movment() function with direction as up,
down, right, left, if the returned 4 matrix are the same, it means
there is no more valid moves.
3. Return: True if there are no more valid moves, False if there are.
o Function check_2048(m_matrix, already_2048)
1. (Check whether the current matrix just got 2048)
2. Since the notification only appear once, if the matrix has already reached
2048 (already_2048 == True). We will not check in this case.
3. Else, check each number in the matrix, if there is 2048, show a
congratulation note in the users interface and a button to exit the game.
4. Return: True if the current matrix just got 2048. False otherwise.
o Function pressed(event) also the main function
1. (Main function)
PIC 16
2. Bind the arrow key pressed with the parameter Up, Down, Right,
Left, so every time the user presses a arrow key, this function will be
called.
3. Every time the press of a key makes a movement of the numbers in the
matrix, call function add_number() to generate a new number in the
current matrix.
4. Every time after making a movement, we call the function check_2048()
and game_over().
Example: When the user reaches 2048, Reach 2048 appears, but the user can
continue playing.
Example: The user can keep playing when reached 2048 and after that he can
quit anytime by pressing the Exit button.
PIC 16
Example: If the user reached 2048 and there are no more valid moves.
Time Evaluation:
Since plotting in the users interface cannot be improved a lot, we should evaluate the
time to run the function movement(), which is used when moving all the numbers in the board
to the direction indicated by the user.
We can design a function to test the average time used in the operation of moving
(up/down/right/left).
Pseudocode: (Use time module)
1. For each direction (up/down/right/left), we record the time before and after
run the function movement(), calculate the time difference and record it in a
list.
2. Repeat the simulation 100 times.
3. Calculate the mean time used for each run time of movement() by dividing
the sum of the list by 400.
Result:
It is not easy to do a complexed analysis of the time using big-O notation as indicated by
the professor in the final project. In this case, the amount of time used for each
PIC 16
operation of movement() is really small and in the actual testing of the program, we can
see that the game runs really smoothly without any delay.
Possible Improvement:
If I have more time, I can make the program closer to real 2048 game and create a
more user-friendly interface by:
1. Add a score section so that the user can know whats his/her current score.
2. Make the board extensible. i.e. The users can play 5 x 5, 6 x 6, etc. 2048 game as they
indicate.
3. Make the interface looks nicer by improving the layout, decorate buttons.
4. Add flash to the movement of numbers.