This is a JavaFX implementation of the popular 2048 puzzle game, where the objective is to slide tiles on a grid to combine them into a tile with the value 2048. This implementation includes animations, styling, and responsive UI design, offering an enjoyable experience for players.
The 2048 game is a single-player sliding block puzzle game where the player combines tiles to create a tile with the value 2048. The game is played on a 4x4 grid, with tiles that slide in four directions (Up, Down, Left, Right). When two tiles with the same value collide, they merge into a single tile with double the value. The game continues until no valid moves are left.
Architecture: The game follows the Model-View-Controller (MVC) pattern, with separate classes for game logic, UI interactions, and rendering. The main class Game2048
manages the game grid, user inputs, and animations, while the Main
class initializes the game window and handles event listeners. The game is styled with modern aesthetics, featuring rounded borders, color-coded tiles, and proper alignment of UI elements.
Technology Stack: The game is built using JavaFX, a Java library for building rich client applications. It uses FXML for defining the UI layout and CSS for styling the game. The project is managed using Maven, with dependencies for JavaFX and JUnit for testing. Also used CSS for styling the game.
Combine tiles with the same value to create a tile with the value 2048 or higher. The game ends when no valid moves are left.
Note: The game can be won by creating a tile with the value 2048 or higher. The player can continue playing even after reaching the 2048 tile.
2
or 4
) appears in a random empty spot.The game is built using JavaFX, following Model-View-Controller (MVC) architecture for clean separation of concerns. The key components are:
Game2048
This is the main class responsible for rendering the game grid, handling user inputs, managing the board state, and applying animations.
Key Components:
SIZE
: Size of the grid (default is 4x4).board
: 2D array representing the logical state of the grid.tiles
: 2D array of StackPane
objects representing the UI tiles.scoreLabel
: Displays the current score.bestScoreLabel
: Displays the best score achieved.Main
This is the entry point for the application, where the game window is initialized and displayed.
Key Components:
Game2048
).pom.xml
This file contains the Maven project configuration, including dependencies for JavaFX and JUnit.
Key Components:
These classes follow the MVC pattern for managing game logic, UI interactions, and rendering.
Key Components:
The game is styled with modern aesthetics, featuring:
Color Codes for Tiles:
2
: Light cream (#eee4da
)4
: Pale beige (#ede0c8
)8
: Light orange (#f2b179
)git clone https://github.com/hoangsonww/Game-2048-JavaFX.git
cd Game-2048-JavaFX
--module-path /path-to-javafx/lib --add-modules javafx.controls,javafx.fxml
Main
class.Main
class from your IDE or terminal.The project structure is organized as follows:
src/
├── org.example.game2048javafx/
│ ├── Controller.java # Controller for the game
│ ├── Model.java # Model for the game
│ ├── View.java # View for the game
│ ├── Game2048.java # Main game logic and UI
│ ├── Main.java # Entry point for the application
│ └── resources/ # Optional resources like images or stylesheets
└── README.md # Comprehensive documentation
Feel free to contribute to this project! Open issues, submit pull requests, or suggest new features.
git fork https://github.com/hoangsonww/Game-2048-JavaFX.git
git checkout -b feature/feature-branch-name
git add .
git commit -m "Add your message here"
git push origin feature/feature-branch-name
This project is licensed under the MIT License.
Enjoy playing 2048 in JavaFX! If you encounter any issues, feel free to raise them in the repository.
Happy Coding! 🚀