-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b41ec3e
commit 358f99c
Showing
5 changed files
with
267 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.example.game2048javafx; | ||
|
||
public class Controller { | ||
private final Model model; | ||
private final View view; | ||
|
||
public Controller(Model model, View view) { | ||
this.model = model; | ||
this.view = view; | ||
} | ||
|
||
public void startGame() { | ||
model.resetBoard(); | ||
model.addRandomTile(); | ||
model.addRandomTile(); | ||
view.updateUI(model.getBoard(), model.getScore(), model.getBestScore()); | ||
} | ||
|
||
public void handleKeyPress(String direction) { | ||
if (model.move(direction)) { | ||
model.addRandomTile(); | ||
view.updateUI(model.getBoard(), model.getScore(), model.getBestScore()); | ||
if (model.checkGameOver()) { | ||
System.out.println("Game Over!"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package org.example.game2048javafx; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
public class Model { | ||
private final int SIZE = 4; | ||
private final int[][] board = new int[SIZE][SIZE]; | ||
private int score = 0; | ||
private int bestScore = 0; | ||
|
||
public int[][] getBoard() { | ||
return board; | ||
} | ||
|
||
public int getScore() { | ||
return score; | ||
} | ||
|
||
public int getBestScore() { | ||
return bestScore; | ||
} | ||
|
||
public void resetBoard() { | ||
for (int row = 0; row < SIZE; row++) { | ||
for (int col = 0; col < SIZE; col++) { | ||
board[row][col] = 0; | ||
} | ||
} | ||
score = 0; | ||
} | ||
|
||
public void addRandomTile() { | ||
List<int[]> emptySpaces = new ArrayList<>(); | ||
for (int row = 0; row < SIZE; row++) { | ||
for (int col = 0; col < SIZE; col++) { | ||
if (board[row][col] == 0) { | ||
emptySpaces.add(new int[]{row, col}); | ||
} | ||
} | ||
} | ||
if (!emptySpaces.isEmpty()) { | ||
Random rand = new Random(); | ||
int[] space = emptySpaces.get(rand.nextInt(emptySpaces.size())); | ||
board[space[0]][space[1]] = rand.nextDouble() < 0.9 ? 2 : 4; | ||
} | ||
} | ||
|
||
public boolean move(String direction) { | ||
boolean moved = false; | ||
for (int i = 0; i < SIZE; i++) { | ||
int[] line = getLine(direction, i); | ||
int[] merged = mergeLine(line); | ||
if (!arrayEquals(line, merged)) moved = true; | ||
setLine(direction, i, merged); | ||
} | ||
return moved; | ||
} | ||
|
||
private int[] getLine(String direction, int index) { | ||
int[] line = new int[SIZE]; | ||
for (int i = 0; i < SIZE; i++) { | ||
switch (direction) { | ||
case "UP" -> line[i] = board[i][index]; | ||
case "DOWN" -> line[i] = board[SIZE - 1 - i][index]; | ||
case "LEFT" -> line[i] = board[index][i]; | ||
case "RIGHT" -> line[i] = board[index][SIZE - 1 - i]; | ||
} | ||
} | ||
return line; | ||
} | ||
|
||
private void setLine(String direction, int index, int[] line) { | ||
for (int i = 0; i < SIZE; i++) { | ||
switch (direction) { | ||
case "UP" -> board[i][index] = line[i]; | ||
case "DOWN" -> board[SIZE - 1 - i][index] = line[i]; | ||
case "LEFT" -> board[index][i] = line[i]; | ||
case "RIGHT" -> board[index][SIZE - 1 - i] = line[i]; | ||
} | ||
} | ||
} | ||
|
||
private int[] mergeLine(int[] line) { | ||
int[] merged = new int[SIZE]; | ||
int pos = 0; | ||
|
||
for (int i = 0; i < SIZE; i++) { | ||
if (line[i] == 0) continue; | ||
if (pos > 0 && merged[pos - 1] == line[i]) { | ||
merged[pos - 1] *= 2; | ||
score += merged[pos - 1]; | ||
} else { | ||
merged[pos++] = line[i]; | ||
} | ||
} | ||
if (score > bestScore) bestScore = score; | ||
return merged; | ||
} | ||
|
||
private boolean arrayEquals(int[] a, int[] b) { | ||
for (int i = 0; i < SIZE; i++) { | ||
if (a[i] != b[i]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
public boolean checkGameOver() { | ||
for (int row = 0; row < SIZE; row++) { | ||
for (int col = 0; col < SIZE; col++) { | ||
if (board[row][col] == 0) return false; | ||
if (row < SIZE - 1 && board[row][col] == board[row + 1][col]) return false; | ||
if (col < SIZE - 1 && board[row][col] == board[row][col + 1]) return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.example.game2048javafx; | ||
|
||
import javafx.animation.ScaleTransition; | ||
import javafx.geometry.Pos; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.layout.*; | ||
import javafx.scene.paint.Color; | ||
import javafx.scene.shape.Rectangle; | ||
import javafx.scene.text.Font; | ||
import javafx.scene.text.Text; | ||
import javafx.util.Duration; | ||
|
||
public class View extends GridPane { | ||
private final int SIZE = 4; | ||
private final StackPane[][] tiles = new StackPane[SIZE][SIZE]; | ||
private final Label scoreLabel = new Label("SCORE: 0"); | ||
private final Label bestScoreLabel = new Label("BEST: 0"); | ||
|
||
public View() { | ||
setHgap(10); | ||
setVgap(10); | ||
setAlignment(Pos.CENTER); | ||
setStyle("-fx-padding: 10; -fx-background-color: #bbada0;"); | ||
} | ||
|
||
public void setupGrid() { | ||
Font poppinsBold = Font.loadFont(getClass().getResourceAsStream("/org/example/game2048javafx/fonts/Poppins-Bold.ttf"), 30); | ||
|
||
for (int row = 0; row < SIZE; row++) { | ||
for (int col = 0; col < SIZE; col++) { | ||
StackPane stack = new StackPane(); | ||
Rectangle bg = new Rectangle(100, 100); | ||
bg.setArcWidth(15); | ||
bg.setArcHeight(15); | ||
bg.setFill(Color.web("#cdc1b4")); | ||
|
||
Text text = new Text(); | ||
text.setFont(poppinsBold); | ||
text.setFill(Color.web("#776e65")); | ||
stack.getChildren().addAll(bg, text); | ||
tiles[row][col] = stack; | ||
add(stack, col, row); | ||
} | ||
} | ||
} | ||
|
||
public HBox getHeader() { | ||
Font poppinsBold = Font.loadFont(getClass().getResourceAsStream("/org/example/game2048javafx/fonts/Poppins-Bold.ttf"), 32); | ||
|
||
Label titleLabel = new Label("2048 Game"); | ||
titleLabel.setFont(poppinsBold); | ||
titleLabel.setStyle( | ||
"-fx-font-size: 32px; " + | ||
"-fx-background-color: #edc22e; " + | ||
"-fx-text-fill: white; " + | ||
"-fx-padding: 15; " + | ||
"-fx-border-radius: 15; " + | ||
"-fx-background-radius: 15;"); | ||
|
||
HBox header = new HBox(20, titleLabel, scoreLabel, bestScoreLabel); | ||
header.setAlignment(Pos.CENTER_LEFT); | ||
return header; | ||
} | ||
|
||
public Label getFooter() { | ||
return new Label("Join the numbers and get to the 2048 tile!"); | ||
} | ||
|
||
public void updateUI(int[][] board, int score, int bestScore) { | ||
scoreLabel.setText("SCORE: " + score); | ||
bestScoreLabel.setText("BEST: " + bestScore); | ||
// Update tiles | ||
// Similar logic as in the original | ||
} | ||
} |