以下是按照最新技术要求,为Java项目实战-数字华容道/石头迷阵游戏编写的实操内容。文章将结合Java 17+的特性及现代GUI开发理念,提供完整的实现方案。
sealed class
、record
、switch
表达式等新特性<!-- pom.xml -->
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.2</version>
</dependency>
</dependencies>
使用Java 17的record
和sealed class
简化数据模型:
// 位置记录类
public record Position(int row, int col) {}
// 游戏状态枚举
public enum GameState {
PLAYING, WON, RESET
}
// 密封类定义移动方向
public sealed interface Direction permits Up, Down, Left, Right {
Position getDelta();
}
public record Up() implements Direction {
@Override
public Position getDelta() {
return new Position(-1, 0);
}
}
// 其他方向实现类似...
实现游戏核心逻辑与用户交互:
public class GameController {
private final int size = 4;
private int[][] board;
private Position emptyPos;
private GameState state = GameState.RESET;
public GameController() {
resetGame();
}
public void resetGame() {
board = new int[size][size];
int value = 1;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = value++;
}
}
board[size-1][size-1] = 0; // 空白块
emptyPos = new Position(size-1, size-1);
shuffleBoard();
state = GameState.PLAYING;
}
private void shuffleBoard() {
// 使用Fisher-Yates算法随机打乱
Random random = new Random();
for (int i = size * size - 1; i > 0; i--) {
int row = i / size;
int col = i % size;
int randomIndex = random.nextInt(i + 1);
int randomRow = randomIndex / size;
int randomCol = randomIndex % size;
swap(row, col, randomRow, randomCol);
}
// 确保游戏有解(此处简化处理)
if (!isSolvable()) shuffleBoard();
}
public boolean move(Direction direction) {
Position delta = direction.getDelta();
int newRow = emptyPos.row() + delta.row();
int newCol = emptyPos.col() + delta.col();
if (isValidMove(newRow, newCol)) {
swap(emptyPos.row(), emptyPos.col(), newRow, newCol);
emptyPos = new Position(newRow, newCol);
if (isGameWon()) {
state = GameState.WON;
}
return true;
}
return false;
}
// 其他方法...
}
使用JavaFX的FXML文件定义UI结构:
<!-- src/main/resources/com/example/game/main.fxml -->
<VBox alignment="CENTER" spacing="20" xmlns:fx="http://javafx.com/fxml">
<Label text="数字华容道" style="-fx-font-size: 24px; -fx-font-weight: bold;" />
<GridPane fx:id="gameBoard" hgap="5" vgap="5" alignment="CENTER" />
<HBox alignment="CENTER" spacing="20">
<Button text="重置游戏" onAction="#handleReset" />
<Button text="退出" onAction="#handleExit" />
</HBox>
<Label fx:id="statusLabel" text="游戏开始!" style="-fx-font-size: 16px;" />
</VBox>
绑定FXML与游戏逻辑:
public class GameViewController implements Initializable {
@FXML private GridPane gameBoard;
@FXML private Label statusLabel;
private final GameController controller = new GameController();
private final Map<Integer, Button> tileButtons = new HashMap<>();
@Override
public void initialize(URL location, ResourceBundle resources) {
initBoard();
controller.addListener(this::updateView);
}
private void initBoard() {
gameBoard.getChildren().clear();
tileButtons.clear();
int[][] board = controller.getBoard();
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
int value = board[i][j];
Button tile = createTileButton(value, i, j);
gameBoard.add(tile, j, i);
tileButtons.put(value, tile);
}
}
}
private Button createTileButton(int value, int row, int col) {
Button button = new Button(value == 0 ? "" : String.valueOf(value));
button.setPrefSize(80, 80);
button.getStyleClass().add("tile");
if (value != 0) {
button.setOnAction(e -> controller.move(getDirection(row, col)));
} else {
button.setVisible(false);
}
return button;
}
// 其他方法...
}
为方块移动添加平滑过渡动画:
private void animateMove(int fromRow, int fromCol, int toRow, int toCol) {
Button tile = tileButtons.get(controller.getValueAt(fromRow, fromCol));
TranslateTransition transition = new TranslateTransition(
Duration.millis(300), tile
);
double deltaX = (toCol - fromCol) * tile.getPrefWidth();
double deltaY = (toRow - fromRow) * tile.getPrefHeight();
transition.setByX(deltaX);
transition.setByY(deltaY);
transition.setInterpolator(Interpolator.EASE_BOTH);
transition.play();
}
使用JavaFX CSS美化界面:
/* src/main/resources/com/example/game/styles.css */
.tile {
-fx-font-size: 24px;
-fx-background-color: #4a86e8;
-fx-text-fill: white;
-fx-background-radius: 10px;
-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.3), 10, 0.5, 0.0, 0.0);
}
.tile:pressed {
-fx-background-color: #3a76d8;
}
#statusLabel {
-fx-text-fill: #333333;
}
使用Maven JLink插件创建可执行程序:
<!-- pom.xml -->
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>com.example.game.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
执行以下命令生成可执行文件:
mvn clean javafx:jlink
MediaPlayer
类添加移动和胜利音效ResourceBundle
实现国际化通过以上步骤,你可以构建一个功能完整、界面美观的数字华容道/石头迷阵游戏。现代Java技术的应用使代码更加简洁、可维护,同时提供了更好的用户体验。
Java 项目实战,数字华容道,石头迷阵游戏,现代 Java 实现,Java 游戏开发,Java 实战教程,Java 编程技巧,华容道游戏开发,Java 项目案例,Java 实战项目,Java 游戏编程,石头迷阵开发,Java 实战技巧,Java 项目教程,数字华容道实现
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。