Skip to content

Commit 77cb613

Browse files
authored
Add files via upload
Signed-off-by: Son Nguyen <[email protected]>
1 parent 23f5c0a commit 77cb613

File tree

71 files changed

+2671
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2671
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Son Nguyen
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
# 2048 Game in JavaFX
2+
3+
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.
4+
5+
## Table of Contents
6+
7+
- [Overview](#overview)
8+
- [Features](#features)
9+
- [Preview](#preview)
10+
- [Gameplay](#gameplay)
11+
- [Objective](#objective)
12+
- [Controls](#controls)
13+
- [How It Works](#how-it-works)
14+
- [Code Structure](#code-structure)
15+
- [Styling](#styling)
16+
- [Installation](#installation)
17+
- [Usage](#usage)
18+
- [File Structure](#file-structure)
19+
- [Known Issues](#known-issues)
20+
- [Contributing](#contributing)
21+
- [License](#license)
22+
- [Acknowledgments](#acknowledgments)
23+
24+
## Overview
25+
26+
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.
27+
28+
**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.
29+
30+
**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.
31+
32+
## Features
33+
34+
- **Interactive Gameplay**: Use arrow keys to move tiles (Up, Down, Left, Right).
35+
- **Score Tracking**: Displays current and best scores at the top of the game.
36+
- **Responsive UI**:
37+
- Animated tile movements and merging effects.
38+
- Color-coded tiles based on values for easy recognition.
39+
- Modern styling with rounded borders and proper alignment.
40+
- **Game State Management**:
41+
- Automatically adds new tiles after each move.
42+
- Checks for available moves and detects game over conditions.
43+
- **Intelligent Animations**:
44+
- Smooth scaling animations for tile appearances and merges.
45+
- Dynamic color updates based on tile values.
46+
- **Build Automation**:
47+
- Maven project configuration for easy setup and dependency management.
48+
- JUnit testing for validating game logic and state transitions.
49+
50+
## Preview
51+
52+
### Welcome Screen
53+
54+
<p align="center">
55+
<img src="img/welcome.png" alt="2048 Game in JavaFX" width="70%" style="border-radius: 8px">
56+
</p>
57+
58+
### Game Interface
59+
60+
<p align="center">
61+
<img src="img/ui.png" alt="2048 Game in JavaFX" width="70%" style="border-radius: 8px">
62+
</p>
63+
64+
### Instructions Interface
65+
66+
<p align="center">
67+
<img src="img/instructions.png" alt="2048 Game in JavaFX" width="70%" style="border-radius: 8px">
68+
</p>
69+
70+
## Gameplay
71+
72+
### Objective
73+
74+
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.
75+
76+
> **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.
77+
78+
### Controls
79+
80+
- **Arrow Keys**:
81+
- **Up**: Moves all tiles upward.
82+
- **Down**: Moves all tiles downward.
83+
- **Left**: Moves all tiles to the left.
84+
- **Right**: Moves all tiles to the right.
85+
- **New Game** Button: Resets the game.
86+
- **Exit Game** Button: Closes the application.
87+
88+
## How It Works
89+
90+
### Game Mechanics
91+
92+
1. **Sliding and Merging**:
93+
- Tiles slide in the direction of the arrow key pressed.
94+
- Tiles with the same value merge into one tile, doubling their value.
95+
- A tile can merge only once per move.
96+
2. **Adding New Tiles**:
97+
- After every valid move, a new tile (value `2` or `4`) appears in a random empty spot.
98+
3. **Game Over**:
99+
- The game checks for possible moves after each action.
100+
- If no moves are available (all tiles are full and non-mergeable), the game ends.
101+
102+
### Code Structure
103+
104+
The game is built using JavaFX, following Model-View-Controller (MVC) architecture for clean separation of concerns. The key components are:
105+
106+
#### **1. Class: `Game2048`**
107+
This is the main class responsible for rendering the game grid, handling user inputs, managing the board state, and applying animations.
108+
109+
Key Components:
110+
- **Grid Management**:
111+
- `SIZE`: Size of the grid (default is 4x4).
112+
- `board`: 2D array representing the logical state of the grid.
113+
- `tiles`: 2D array of `StackPane` objects representing the UI tiles.
114+
- **UI Elements**:
115+
- `scoreLabel`: Displays the current score.
116+
- `bestScoreLabel`: Displays the best score achieved.
117+
- **Tile Colors**:
118+
- Dynamically updates colors and text based on tile values.
119+
- **Animations**:
120+
- Smooth scaling animations for tile appearances and merges.
121+
122+
#### 2. **Class: `Main`**
123+
This is the entry point for the application, where the game window is initialized and displayed.
124+
125+
Key Components:
126+
- **JavaFX Setup**:
127+
- Configures the game window and scene.
128+
- Loads the FXML file for the game UI.
129+
- Initializes the game controller (`Game2048`).
130+
- **Event Handling**:
131+
- Registers key listeners for arrow keys.
132+
- Handles button actions for New Game and Exit Game.
133+
- **CSS Loading**:
134+
- Loads the CSS file for styling the game.
135+
136+
#### 3. **Maven XML Configuration: `pom.xml`**
137+
This file contains the Maven project configuration, including dependencies for JavaFX and JUnit.
138+
139+
Key Components:
140+
- **JavaFX Dependencies**:
141+
- Includes JavaFX modules for controls and FXML.
142+
- Configures the JavaFX plugin for running the application.
143+
- **JUnit Dependency**:
144+
- Adds JUnit for testing the game logic.
145+
- Configures the Maven Surefire plugin for running tests.
146+
147+
#### 4. **Controller, Model, and View Classes**
148+
These classes follow the MVC pattern for managing game logic, UI interactions, and rendering.
149+
150+
Key Components:
151+
- **Controller**:
152+
- Handles user inputs and game actions.
153+
- Updates the model and view based on user interactions.
154+
- **Model**:
155+
- Manages the game state and logic.
156+
- Checks for valid moves and game over conditions.
157+
- **View**:
158+
- Renders the game grid and UI elements.
159+
- Updates the UI based on the model state.
160+
161+
### Styling
162+
163+
The game is styled with modern aesthetics, featuring:
164+
- Rounded borders for tiles and buttons.
165+
- Background colors based on tile values.
166+
- Proper alignment of header (title, score, and best score).
167+
- Button styles with hover effects.
168+
169+
**Color Codes for Tiles:**
170+
- `2`: Light cream (`#eee4da`)
171+
- `4`: Pale beige (`#ede0c8`)
172+
- `8`: Light orange (`#f2b179`)
173+
- Higher values progressively darker and more vibrant.
174+
175+
## Installation
176+
177+
1. **Clone the Repository**:
178+
```bash
179+
git clone https://github.com/hoangsonww/Game-2048-JavaFX.git
180+
cd Game-2048-JavaFX
181+
```
182+
183+
2. **Set Up JavaFX**:
184+
- Ensure that JavaFX is installed. If not, download JavaFX from the [official site](https://openjfx.io/).
185+
- Configure JavaFX in your IDE:
186+
- Add JavaFX libraries to your project.
187+
- Set JVM arguments to include JavaFX runtime, e.g.:
188+
```bash
189+
--module-path /path-to-javafx/lib --add-modules javafx.controls,javafx.fxml
190+
```
191+
192+
3. **Run the Game**:
193+
- Compile and run the `Main` class.
194+
- The game window will open, and you can start playing immediately.
195+
196+
## Usage
197+
198+
### Running the Game
199+
200+
1. **Start the Application**:
201+
- Run the `Main` class from your IDE or terminal.
202+
2. **Play**:
203+
- Use the arrow keys to move and merge tiles.
204+
- Aim for a tile value of 2048 (or higher)!
205+
3. **Reset**:
206+
- Click the **New Game** button to restart the game.
207+
4. **Exit**:
208+
- Click the **Exit Game** button to close the application.
209+
210+
## File Structure
211+
212+
The project structure is organized as follows:
213+
214+
```
215+
src/
216+
├── org.example.game2048javafx/
217+
│ ├── Controller.java # Controller for the game
218+
│ ├── Model.java # Model for the game
219+
│ ├── View.java # View for the game
220+
│ ├── Game2048.java # Main game logic and UI
221+
│ ├── Main.java # Entry point for the application
222+
│ └── resources/ # Optional resources like images or stylesheets
223+
└── README.md # Comprehensive documentation
224+
```
225+
226+
## Known Issues
227+
228+
1. **Game Freezing**:
229+
- Ensure animations do not block the main UI thread.
230+
2. **Performance**:
231+
- For extremely high tile values, UI updates may lag.
232+
233+
## Contributing
234+
235+
Feel free to contribute to this project! Open issues, submit pull requests, or suggest new features.
236+
237+
1. **Fork the Repository**:
238+
```bash
239+
git fork https://github.com/hoangsonww/Game-2048-JavaFX.git
240+
```
241+
242+
2. **Create a New Branch**:
243+
```bash
244+
git checkout -b feature/feature-branch-name
245+
```
246+
247+
3. **Commit and Push**:
248+
```bash
249+
git add .
250+
git commit -m "Add your message here"
251+
git push origin feature/feature-branch-name
252+
```
253+
254+
4. **Submit a Pull Request**:
255+
- Open a pull request on GitHub describing your changes.
256+
257+
5. **We will review your PR** and merge it if it meets the project's guidelines!
258+
259+
## License
260+
261+
This project is licensed under the [MIT License](LICENSE).
262+
263+
## Acknowledgments
264+
265+
- Inspired by the original 2048 game.
266+
- Built with love using JavaFX.
267+
- Author: [Son Nguyen](https://github.com/hoangsonww)
268+
269+
Enjoy playing 2048 in JavaFX! If you encounter any issues, feel free to raise them in the repository.
270+
271+
---
272+
273+
**Happy Coding! 🚀**
Loading
Loading
Loading

0 commit comments

Comments
 (0)