"Hello, soldier!"

"Congratulations on upgrading your skills. We need guys who are prepared to do anything."

"I'm sure you still have many unfinished tasks. It's time to finish some of them!"

9
Java Multithreading, level 10, lesson 15
Locked
2048 (part 1)
Hi! I hope you've grown tired of ordinary tasks and are eagerly anticipating a big one! Today we'll write a Java implementation of the game 2048. In short, if you haven't heard anything about this game, the goal is to get a tile with a value of 2048 on a 4x4 board. You can read more on Wikipedia.
18
Java Multithreading, level 10, lesson 15
Locked
2048 (part 2)
In the game 2048 game, the board consists of 16 tiles, each of which has a certain weight. In addition to weight, a tile has its own color and text color used to display the tile's weight. Tile colors range from light gray to red, and the text color depends on the color of the tile.
9
Java Multithreading, level 10, lesson 15
Locked
2048 (part 3)
Let's implement the Model class. It will be responsible for all manipulations performed on the game board. But to manipulate the game board, we ought to start by creating it! We're going to need: 1. A private constant FIELD_WIDTH = 4, which defines the width of the game board.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 4)
The game 2048 begins on a board where two tiles already have initial values. But our board is still empty :(. Before you rush to write code, let's think about how this could be implemented. I suggest creating a private addTile method that will look at which tiles are empty.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 5)
The main actions that we need to enable are movements left, right, up and down. If you've already played 2048, then you know that when you move in a direction, tiles with values move to empty tiles, and tiles with the same value combine.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 6)
So, you've implemented tile consolidation and merging. The combination of these operations gives us the ability to move to the left. Great! But we also need to add a new tile if the game board was changed by the movement.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 7)
We've implemented movement to the left. Now we need to implement the right, up, and down methods. I'm sure you can handle it without my help, so I'll give you just one hint.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 8)
The model is almost ready! Let's add a couple more simple methods and start implementing the controller. Our model lacks a way to get the game board in order to pass it to the view for rendering, as well as a method that can determine whether a move is possible in the current position.
18
Java Multithreading, level 10, lesson 15
Locked
2048 (part 9)
You're doing really well! It's so good that I decided to help you a little and have already implemented the View class. It's quite simple. It inherits the JPanel class, overrides the paint method, and displays the current state of the model obtained using the controller.
9
Java Multithreading, level 10, lesson 15
Locked
2048 (part 10)
It's time to start implementing the main method in the Main class—to finally be able to launch the game and relax! We only need the main method to run the application. We've already implemented all the guts.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 11)
Good work! At this stage, we already have a full-featured application, but there’s no limit to perfection. Let's do some more work. If you've managed to play 2048 a little, you've noticed that sometimes you really want to be able to undo your last move.
9
Java Multithreading, level 10, lesson 15
Locked
2048 (part 12)
Well, shall we try our algorithm in action? We still need to add logic to save the game state at the beginning of each movement method, as well as another key processing case for undoing the last move.
9
Java Multithreading, level 10, lesson 15
Locked
2048 (part 13)
Your progress is impressive! To change things up, I suggest giving the game the ability to choose the next move on its own. Let's start with the simple. In the Model class, implement a randomMove that randomly calls one of the movement methods.
9
Java Multithreading, level 10, lesson 15
Locked
2048 (part 14)
A random move is certainly not bad, but it's much cooler to implement the ability to make smart moves. We won't get bogged down in neural networks. To start, we'll focus on a fairly simple idea. Obviously, a good move should eventually bring us closer to victory, namely, a tile with 2048.
36
Java Multithreading, level 10, lesson 15
Locked
2048 (part 15)
To compare the fitness of different moves, we need to implement support for the Comparable interface in the MoveFitness class. In the compareTo method, compare the numbers of empty tiles (numberOfEmptyTiles) first. If the number of empty tiles is equal, then compare the scores (score).
9