Welcome to the 2048 Game project! This Java program implements the classic 2048 game where the player combines tiles with the same number to reach the elusive 2048 tile. The project includes functionality for saving and loading game states.
The project consists of a Java class named Main2048, which serves as the entry point for the game. The main features of the program include:
-
Game Initialization:
- Checks for the existence of a save file (
save-2048.txt). - If the save file exists, loads the game state; otherwise, starts a new game.
- Checks for the existence of a save file (
-
Runtime Shutdown Hook:
- Utilizes a shutdown hook to automatically save the game state when the program is terminated.
-
User Interface:
- Accepts user input for moves (up, right, down, left, quit).
- Allows the player to restart the game.
-
Game State Management:
- Utilizes the
Gameclass to manage the state of the 2048 game. - Implements the logic for combining tiles and generating new tiles.
- Utilizes the
-
Save and Load Functionality:
- Saves the game state to the
save-2048.txtfile when the program is terminated. - Loads the game state from the save file if it exists.
- Saves the game state to the
-
Compile and Run:
- Compile the Java program using a Java compiler.
- Run the compiled program to start the 2048 game.
-
Gameplay:
- Use the following keys to make moves:
u: Move tiles upr: Move tiles to the rightd: Move tiles downl: Move tiles to the leftn: Restart the gameq: Quit the game
- Use the following keys to make moves:
-
Save and Load:
- The game state is automatically saved when the program is terminated.
- The saved state is loaded if a save file (
save-2048.txt) exists.
-
Exiting the Game:
- To exit the game, enter
qduring your turn.
- To exit the game, enter
Here's a quick example of how to play the game:
Enter move (u/r/d/l/n/q):- Input
uto move tiles up,rto move tiles to the right,dto move tiles down, andlto move tiles to the left. - Input
nto restart the game. - Input
qto quit the game.
Feel free to explore and modify the code to enhance or customize the 2048 game. Enjoy playing!