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.
But you have to finish implementing the Controller class.
To start, we're going to need a constructor. It will have one Model parameter, initialize the model field, and also save a new View object in the view field, passing the current controller (this) to the constructor.
Next, we need a resetGame method that can return the game board to its initial state. You need to reset the score, set the isGameWon and isGameLost flags on the view to false, and call the resetGameTiles method on the model.
Note: set the field values directly, without using setters.
Add a private constant int WINNING_TILE = 2048. It will determine the tile weight that, when achieved, constitutes a win.
And now, the most important thing! To be able to process user input, you need to override the keyPressed method with a single KeyEvent parameter.
The logic of the method should be as follows:
1) If the ESC key was pressed, call the resetGame method.
2) If the model's canMove method returns false, set the isGameLost flag to true.
3) If both the isGameLost and isGameWon flags are false, process the movement events:
a) for KeyEvent.VK_LEFT, call the left method on the model;
b) for KeyEvent.VK_RIGHT, call the right method on the model;
c) for KeyEvent.VK_UP key, call the up method on the model;
d) for KeyEvent.VK_DOWN key, call the down method on the model.
4) If the model's maxTile field becomes equal to WINNING_TILE, set the isGameWon flag to true.
5) Finally, call the repaint method on the view.
P.S. To get the key code, use the KeyEvent class's getKeyCode method.
- A private static constant int WINNING_TILE = 2048 must be created in the Controller class.
- The Controller class constructor with one Model parameter must be implemented in accordance with the task conditions.
- The resetGame method should return the game to its initial state, as described in the task conditions.
- The keyPressed method should call the resetGame method if the ESC key is pressed.
- The keyPressed method must set the isGameLost flag to true if no move is possible.
- The keyPressed method should call the correct methods for moving the game board if a corresponding key is pressed and both the isGameLost and isGameWon flags are false.
- The keyPressed method should set the isGameWon flag to true if the model.maxTile and WINNING_TILE fields become equal after the movement.
- The keyPressed method should call the repaint method on the object saved in the view field.