用javafx和GUI实现一个数字华容道,要求有代码,并且能计时,并且能够重置盘面和完成后有反馈
时间: 2024-03-06 13:52:07 浏览: 116
好的,以下是实现数字华容道的JavaFX代码。实现了计时,重置盘面和完成后有反馈的要求。
```java
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.util.ArrayList;
import java.util.Collections;
public class DigitalHuarongDao extends Application {
private static final int ROWS = 4;
private static final int COLS = 4;
private static final int CELL_SIZE = 80;
private static final String WIN_MESSAGE = "Congratulations! You've won!";
private static final String RESET_MESSAGE = "Puzzle reset!";
private static final String TIMER_PREFIX = "Time: ";
private int emptyRow;
private int emptyCol;
private int movesCount;
private Timeline timeline;
private Label timerLabel;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setHgap(5);
pane.setVgap(5);
ArrayList<Integer> numbers = new ArrayList<>();
for (int i = 1; i < ROWS * COLS; i++) {
numbers.add(i);
}
numbers.add(null);
Collections.shuffle(numbers);
int index = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
Label label = new Label(numbers.get(index) == null ? "" : String.valueOf(numbers.get(index)));
label.setPrefSize(CELL_SIZE, CELL_SIZE);
label.setStyle("-fx-border-color: black");
label.setAlignment(Pos.CENTER);
pane.add(label, col, row);
if (numbers.get(index) == null) {
emptyRow = row;
emptyCol = col;
}
label.setOnMouseClicked(event -> {
int clickedRow = GridPane.getRowIndex(label);
int clickedCol = GridPane.getColumnIndex(label);
if (isValidMove(clickedRow, clickedCol)) {
swapLabels(label, emptyRow, emptyCol);
emptyRow = clickedRow;
emptyCol = clickedCol;
movesCount++;
if (isWin()) {
timeline.stop();
Label winLabel = new Label(WIN_MESSAGE);
winLabel.setStyle("-fx-font-size: 24");
Scene scene = new Scene(winLabel, 300, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}
});
index++;
}
}
Button resetButton = new Button("Reset");
resetButton.setOnAction(event -> {
resetPuzzle(pane, numbers);
movesCount = 0;
timerLabel.setText(TIMER_PREFIX + "0");
timeline.play();
Scene scene = new Scene(pane, CELL_SIZE * COLS, CELL_SIZE * ROWS + 30);
primaryStage.setScene(scene);
primaryStage.show();
});
timerLabel = new Label(TIMER_PREFIX + "0");
timeline = new Timeline(new KeyFrame(Duration.seconds(1), event -> {
int time = Integer.parseInt(timerLabel.getText().substring(TIMER_PREFIX.length())) + 1;
timerLabel.setText(TIMER_PREFIX + time);
}));
timeline.setCycleCount(Timeline.INDEFINITE);
GridPane buttonPane = new GridPane();
buttonPane.setAlignment(Pos.CENTER);
buttonPane.setHgap(10);
buttonPane.add(resetButton, 0, 0);
buttonPane.add(timerLabel, 1, 0);
Scene scene = new Scene(new GridPane(), CELL_SIZE * COLS, CELL_SIZE * ROWS + 30);
((GridPane) scene.getRoot()).add(pane, 0, 0);
((GridPane) scene.getRoot()).add(buttonPane, 0, 1);
primaryStage.setTitle("Digital Huarong Dao");
primaryStage.setScene(scene);
primaryStage.show();
timeline.play();
}
private boolean isValidMove(int row, int col) {
return (row == emptyRow && Math.abs(col - emptyCol) == 1) || (col == emptyCol && Math.abs(row - emptyRow) == 1);
}
private void swapLabels(Label label, int row, int col) {
int emptyIndex = emptyRow * COLS + emptyCol;
int labelIndex = row * COLS + col;
Collections.swap((ArrayList) label.getParent().getChildrenUnmodifiable(), emptyIndex, labelIndex);
}
private boolean isWin() {
int index = 1;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
Label label = (Label) ((GridPane) timeline.getNode().getScene().getRoot()).getChildren().get(row * COLS + col);
if (label.getText().isEmpty()) {
continue;
}
int number = Integer.parseInt(label.getText());
if (number != index) {
return false;
}
index++;
}
}
return true;
}
private void resetPuzzle(GridPane pane, ArrayList<Integer> numbers) {
Collections.shuffle(numbers);
int index = 0;
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
Label label = (Label) pane.getChildren().get(index);
label.setText(numbers.get(index) == null ? "" : String.valueOf(numbers.get(index)));
if (numbers.get(index) == null) {
emptyRow = row;
emptyCol = col;
}
index++;
}
}
Label resetLabel = new Label(RESET_MESSAGE);
resetLabel.setStyle("-fx-font-size: 24");
Scene resetScene = new Scene(resetLabel, 300, 100);
Stage resetStage = new Stage();
resetStage.setScene(resetScene);
resetStage.show();
timeline.stop();
}
}
```
代码说明:
- `ROWS` 和 `COLS` 定义了数字华容道的行数和列数。
- `CELL_SIZE` 是每个方格的长度和宽度。
- `WIN_MESSAGE` 和 `RESET_MESSAGE` 是赢得游戏和重置盘面的提示信息。
- `emptyRow` 和 `emptyCol` 记录当前空白方格的行和列。
- `movesCount` 记录用户移动方块的次数。
- `timeline` 是计时器。
- `timerLabel` 显示当前时间。
- `start` 方法创建了数字华容道的界面和按钮,并设置了事件监听器。
- `isValidMove` 方法判断用户是否可以移动方块。
- `swapLabels` 方法交换两个方块的位置。
- `isWin` 方法判断用户是否赢得了游戏。
- `resetPuzzle` 方法重置盘面。
阅读全文