0% found this document useful (0 votes)
949 views9 pages

2048 Report

The document describes a project to simulate the 2048 board game. It provides pseudocode and examples to generate a new board after each user move by adding new random numbers, moving existing numbers, and checking for winning or losing conditions. Functions are defined to handle displaying the board, detecting valid moves, checking for a 2048 win, and handling user input to trigger moves. The project aims to create an interactive 2048 game with a graphical user interface.

Uploaded by

api-362845526
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
949 views9 pages

2048 Report

The document describes a project to simulate the 2048 board game. It provides pseudocode and examples to generate a new board after each user move by adding new random numbers, moving existing numbers, and checking for winning or losing conditions. Functions are defined to handle displaying the board, detecting valid moves, checking for a 2048 win, and handling user input to trigger moves. The project aims to create an interactive 2048 game with a graphical user interface.

Uploaded by

api-362845526
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

PIC 16

Final Project - 2048


Introduction:

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:

2. Generate new matrix after every operation


o Function row_addition(current_matrix, go_up, transpose)
1. (Given a direction up/down, we can add same number from top to
bottom or from bottom to top just like 2048. Like up/down, we can want
to get the numbers in a row after we move them to the right or to the
left, we can take the transpose of a matrix and add the numbers upward
or downward.)
2. Take the transpose of current_matrix if we want to move to the right or
left (transpose = True)
Moving Direction Go_up Transpose
Up True False
Down False False
Right True True
Left False True

3. Flip current_matrix if we want to move down or to the right.


4. For every column, we compare every number in the row to the number
following. As we add same number together, we add the result to a
storage, whose keys are 0, 1, 2,3 (column number).
5. Return: a dictionary with the numbers in each column/row after
movement to a direction.

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

[1] == [3] and [2] == 0 [0], 2*[1]


Else [0], [1], [2], [3]
(Note: if column[i] == 0, we will not take it into the result.)

Example: Take the following matrix as current_matrix:

# Calculate the number in the column if moving up


# Become one 2 in coln 0, two 4s in coln 1, nothing in the last 2 colns.
>>> row_addition(current_matrix, go_up=True, transpose=False)
>>> {0: [2], 1: [4, 4], 2: [], 3: []}

# Calculate the number in the row if moving right


# Become one 2 in row 0, one 4 in row 1, one 4 in row 3.
>>> row_addition(current_matrix, go_up=False, transpose=True)
>>> {0: [2], 1: [4], 2: [], 3: [4]}

o Function add_zeros(m_dict, after)


1. (We add 0s in the dictionary we obtain previously so the lists of every
key become lists with length 4. We need to indicate if we want to add 0s
before the list of after the list)
Moving Direction After
Up True
Down False
Right True
Left False
2. For every list in m_dict, if after == False, flip the list using m_list[::-1].
3. Add 0s at each list until the length reaches 4.
4. If after == False, flip each list back.
5. Return m_dict. (Current corresponding list of each key has length 4)

Example: Take the previous dictionary as an example.


>>> test_dict = {0: [2], 1: [4, 4], 2: [], 3: []}
>>> add_zeros(test_dict, True)
>>> {0: [2, 0, 0, 0], 1: [4, 4, 0, 0], 2: [0, 0, 0, 0], 3: [0, 0, 0, 0]}
PIC 16

o Function movement(current_matrix, direction)


1. (As we have the above function, we can put the dictionary we obtained
previous into a 4 x 4 matrix)
2. If direction == Up or Down (move perpendicularly), we put the list of
each key perpendicularly into the column of the new matrix.
If direction == Right or Left (move horizontally), we put the list of
each key horizontally into the rows of the new matrix.
3. Return: a new matrix with numbers from the previous dictionary.

Example: Take the dictionary in the previous function for example.


>>> movement(current_matrix, down)
>>>

(Compare to the dictionary we obtain above)


>>> {0: [2, 0, 0, 0], 1: [4, 4, 0, 0], 2: [0, 0, 0, 0], 3: [0, 0, 0, 0]}

3. Implementation into Users Interface


o Function Display(m_matrix)
1. (Display in the users interface, display numbers in each row in the matrix
we obtained)
2. Create a Tk() object and a canvas. In the canvas, create 9 grids and put
the numbers of m_matrix in the middle of each grids.
3. Return: (No returned value)
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 users have no more valid moves.


PIC 16

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy