Fusion Coding— 2048

This blog is dedicated to a small project I built for this coding assignment. I decided to go with coding a text-based 2048 game since I’ve played the game a lot from a long time ago, and I think it would be really interesting to try to bring my own flair into it.

So what is 2048?

2048 by Gabriele Cirulli commonly starts with a 4×4 grid of blank tiles, with 2 tiles filled with either a 2 or a 4. With every move, you move the board in your preferred direction, combining adjacent tiles as you go, and another random tile appears. The goal of the game is to reach the number 2048, in any way that you please.

“Failing to plan is planning to fail”— Benjamin Franklin

Planning the Flow Chart

Flowcharts are a great way to map out your thinking, especially if you have a big project at hand. I decided to map out the main components of the game in the flowchart, asking Google Gemini for any gaps in my flowchart.

Flowchart of the 2048 game loop made in Freeform

Functions in 2048

A function in code is a specific set of instructions that you map to a certain name in order to reference it easily throughout the code. In terms of 2048, I listed the functions I would need in 2048 on the right of the flowchart. I’m guessing overall this saved me hundreds of lines of code throughout the whole game code.

Translating the Flowchart into Python

Making a flowchart is one thing, but actually coding the game in Python is another thing entirely. I first have to import I’ll go through each of the functions one by one and explain each of them:

This clear console function isn’t really needed but it’s easier to reference. I asked Gemini how I would clear the terminal in Google Colab so I did what it told me and imported the library along with the random library for picking random tiles.

This function creates a 2D array for the board based on its size. You can see it calls the add_new_tile function which will make sense soon.

This function first finds all the empty cells by looping through each cell and checking if it’s 0. Next if there are any empty cells it chooses a random cell(this is where the random library becomes useful) and chooses a number randomly between 2 and 4.

This print_board function was a bit annoying to figure out because I had to mess around a lot with the text. What it does is when the game is running, it prints the game title, score, and board. It then turns the board from just a simple grid of numbers into an actually nice looking text-based graphic. The comment on the bottom helps explain most of the function, or at least the hardest part.

This function is part of what is responsible for what happens on the board after a move. Let’s look into the logic of the function when it slides a row left. It starts with making a copy of the current row without 0s. I decided to go with the left direction because when the code iterates through most data structures it goes from left to right in our case. The next few lines combine the duplicates and in the end it returns the row with 0s filled in on the right.

Obviously when you make a move it’s not just one row that slides left, it’s the whole board, so this function just goes through each row, slides it left and returns the new board. Notice the merge_score and total_score variables in the two functions, these will be used to display scores in the main function.

This function is a little deceiving to the eye because even though it’s only one line it took a bit of thinking to get right. The comment explains everything.

This function, despite its size isn’t that complicated. I did make a little bit of a mistake naming the variables because total_score isn’t actually the total score of the game, just the total score of the one move. score is actually the total score, so the naming is a bit misleading. You can see the rotate_board function be put to full use here. I could’ve spent some more time maybe making a counterclockwise “rotate_board” function but I don’t think it makes too much of a difference. I also don’t use for loops here because it won’t really save much space compared to just repeating the line over a few times. The direction variable is referring to the main function later on where we prompt the user for the move.

The following 2 functions are going to decide when you lose or win the game. Most of the logic is explained in the comments. As a reminder you lose the game when the following 2 criteria are met:

  1. There are no more blank cells on the board
  2. There are no adjacent cells that you can merge

This function is much more simple than the loss condition because there’s only one condition that has to be met to win the game— if your biggest tile is 2048 or more.

This main function is so big I’m going to split it into 2 images because my screen couldn’t fit the whole function. This is Part 1 where the functions handles the input responsible for the board size, printing the board, and the win and loss conditions. You might notice that the score variable I mentioned earlier is defined here— where the actual score is saved. If you really like the game and want to play past 2048, the asked_continue variable is responsible for letting you do that. If I didn’t make that variable, for every move you do after winning it will prompt you after every move to ask you if you want to continue, which is a hassle if you really want to keep playing.

This second part of the main function will handle what happens after a move. If you decide to quit it will print how many moves you made. This might be useful in future use cases if for some reason I have to work on this further. It also handles invalid inputs. After that it works on the score, adding a new tile, and increasing the move count. The if statement at the end is something I learned a long time ago and don’t really remember what its use is. It might be used to only run if it’s the main program in the file, but I’m not sure.

Links


Comments

One response to “Fusion Coding— 2048”

  1. mcrompton Avatar
    mcrompton

    This is an outstanding assignment, Trison! You have clearly explained your process and exactly how the code works. I feel like you have some experience with Python already and this made your job fairly easy. I appreciate how you used AI to check your work when you need that, even when writing this post. My one suggestions would be to think about the aesthetics and readability of your post. The images being different resolutions and sizes make it challenging in spots (particularly your flow chart) to read the text easily. Minor point but could clean up the look of the post. Excellent work!

Leave a Reply to mcrompton Cancel reply

Your email address will not be published. Required fields are marked *