null
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 1/18)
Hi! Today we'll start writing an interesting and addictive game — 2048. Are you ready? Then let's go. Rules of the game: The game board is square. The standard board size is 4x4 tiles. The objective of the game is to get a tile whose value is 2048. 1. A tile with a value of 2 (probability of 90%) o
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 2/18)
Have you tried running your program? The game board is too small. Let's fix that. To do this, we need to override the initialize() method of the Game parent class. The initialize() method lets you set the size of the game board, decide whether to display the grid, etc. To set the size of the game
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 3/18)
We'll use the game engine to display the current state of the game. And a 4x4 array is perfect for storing the state. So, create an int[][] gameField field and initialize with a SIDExSIDE int[][] array. By default, the elements of the int array are initialized with zeros. Let zeros represent an em
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 4/18)
Somehow a black screen doesn't look very good, right? Let's change that. The Game class — and the Game2048 class, since it inherits Game — has a special setCellColor(int x, int y, Color color) method that sets the color of the cell with coordinates (x, y). You can find a list of available colors i
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 5/18)
According to the rules, when you start a game, the values of two random cells must be 2 or 4 (with a probability of 90% and 10%, respectively). To satisfy this requirement, create a createNewNumber() method and call it twice in the createGame() method. The createNewNumber() method: 1) selects a ra
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 6/18)
It's already much better. But I think we can do more. Let's paint cells with the same values the same colors. To do this, write a setCellColoredNumber method that will take as arguments a cell's coordinates and value, and: - calculate the cell color (using the getColorByValue method, which is desc
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 7/18)
Based on the rules of the game, we can move all tiles to one of the 4 sides: up, down, right, or left. When we do this, tiles with a value of 2 or more go in one direction (the direction of the move), and empty cells move in the opposite direction. At this stage, we'll implement a left shift. To d
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 8/18)
You already know how to move tiles across the board. But that's not all you can do in this game :) If a shift causes two tiles with the same value to "collide", then they merge into one tile whose value is equal to the sum of the merged tiles. Let's implement it. Task conditions: Create a private
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 9/18)
You can play the game using the keyboard, specifically the up arrow, down arrow, right arrow, and left arrow keys. When a key is pressed, the corresponding action should happen (shift, merge). To specify the action, you need to override the Game class's onKeyPress(Key key) method, where the paramet
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 10/18)
We already know which methods are invoked by which keystrokes. So let's start implementing them. Let's start with the moveLeft method(). In the moveLeft() method, we shift the elements of each row of the gameField matrix using the compressRow(int[]) method. We merge their values using the mergeRow
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 11/18)
The moveLeft() method is implemented, but there are still 3 other methods. Let's implement them as follows: if we need to move tiles down, we take our gameField matrix, rotate it 90 degrees clockwise (so the bottom becomes the left edge), shift to the left, and rotate the matrix back (rotate clockw
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 12/18)
Now you can start implementing the moveRight(), moveUp(), and moveDown() methods. In the moveRight() method, rotate the gameField matrix clockwise twice, then call moveLeft(), which we've already written, and again rotate the gameField clockwise two more times. It should work. In the moveDown() m
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 13/18)
The basic game logic is in place, but it's not entirely complete. Based on the rules and the name of the game, the game ends when the value of one of the tiles becomes 2048. You need to search for the maximum value on the game board in a separate int getMaxTileValue() method that returns the maxim
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 14/18)
Great! In the last part, we identified the win conditions and implemented them. But there's another side of the coin: loss. If the game board has no tile whose value is 2048 and no move can be made, then the player has lost. A move can be made if there is at least one empty tile or there are no em
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 15/18)
Just as we need to report wins, we must also report loses. Let's do it in a separate method: void gameOver(), by analogy with the win() method. If the game is lost, you also need to set the isGameStopped flag equal to true. At the beginning of the onKeyPress(Key) method, check whether a move is po
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 16/18)
It's time to find out why we need the isGameStopped flag. We need it to implement the ability to restart the game. If the flag is true, the game is stopped due to a win or loss. When you press the space bar (Key.SPACE), you can restart the game. Restarting the game is the same as starting a new gam
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 17/18)
I think you long ago noticed the line at the bottom of the game board: "Score: 0". Judging by the name, this is the score :) According to the rules of the game, each time tiles are merged, the score increases by the value of the merged tile. To implement this, we'll introduce a private int score va
3
Task
Games, level 0, lesson 2
Locked
2048 (Part 18/18)
Congratulations! The game is done! Run it and see what happens. If you have time and motivation, you can improve the game by adding some features of your own. For example: • change the size of the game board; • continue the game even if a tile with the value 2048 is found; • instead of the nu