下面是一个简单的基于Java的推箱子游戏系统的示例代码。它实现了游戏的地图生成、角色移动、箱子推动、胜利条件检查等功能。您可以根据自己的需求进行修改和扩展。
```java
import java.util.Scanner;
public class SokobanGame {
private char[][] map; // 游戏地图
private int playerX; // 玩家的x坐标
private int playerY; // 玩家的y坐标
private int boxCount; // 箱子的数量
private int boxOnTargetCount; // 已经放置在目标位置的箱子的数量
private boolean isGameOver; // 游戏是否结束
public SokobanGame() {
map = new char[][]{
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', ' ', ' ', ' ', '#', ' ', ' ', ' #', ' ', '#'},
{'#', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', '#', '#', ' ', ' ', ' ', '#', '#', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '@', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
};
playerX = 8;
playerY = 8;
boxCount = 3;
boxOnTargetCount = 0;
isGameOver = false;
}
// 显示游戏地图
public void displayMap() {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
// 移动角色
public void movePlayer(int dx, int dy) {
if (isGameOver) {
return;
}
int newX = playerX + dx;
int newY = playerY + dy;
// 检查目标位置是否合法
if (map[newX][newY] == ' ' || map[newX][newY] == '.') {
// 从目标位置移开箱子
if (map[newX][newY] == '.') {
map[newX][newY] = ' ';
boxOnTargetCount--;
}
if (map[playerX][playerY] == '@') {
map[playerX][playerY] = ' ';
} else {
map[playerX][playerY] = '.';
}
playerX = newX;
playerY = newY;
if (map[playerX][playerY] == ' ') {
map[playerX][playerY] = '@';
} else {
map[playerX][playerY] = '+';
}
}
// 检查是否推动箱子
if (map[newX][newY] == '$' || map[newX][newY] == '*') {
int newBoxX = newX + dx;
int newBoxY = newY + dy;
// 检查箱子推动目标位置是否合法
if (map[newBoxX][newBoxY] == ' ') {
// 从目标位置移开箱子
if (map[newBoxX][newBoxY] == '.') {
map[newBoxX][newBoxY] = ' ';
boxOnTargetCount--;
}
map[newX][newY] = '@';
map[newBoxX][newBoxY] = '$';
if (map[playerX][playerY] == '@') {
map[playerX][playerY] = ' ';
} else {
map[playerX][playerY] = '.';
}
playerX = newX;
playerY = newY;
// 检查是否推动箱子到目标位置
if (map[newBoxX][newBoxY] == '.') {
map[newBoxX][newBoxY] = '*';
boxOnTargetCount++;
}
}
}
// 检查游戏是否胜利
if (boxOnTargetCount == boxCount) {
isGameOver = true;
}
}
// 打印游戏结果
public void printGameResult() {
if (isGameOver) {
System.out.println("恭喜您完成了游戏!");
} else {
System.out.println("很遗憾,您未能完成游戏!");
}
}
public static void main(String[] args) {
SokobanGame game = new SokobanGame();
Scanner scanner = new Scanner(System.in);
while (!game.isGameOver) {
game.displayMap();
System.out.println("请输入移动方向: ");
System.out.println("1. 上");
System.out.println("2. 下");
System.out.println("3. 左");
System.out.println("4. 右");
System.out.println("5. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
game.movePlayer(-1, 0);
break;
case 2:
game.movePlayer(1, 0);
break;
case 3:
game.movePlayer(0, -1);
break;
case 4:
game.movePlayer(0, 1);
break;
case 5:
return;
default:
System.out.println("无效的选择!");
}
}
game.printGameResult();
}
}
```
希望这个示例代码对您有所帮助。它只是一个简单的推箱子游戏系统,仅供参考。在实际应用时,您可以根据自己的需求进行修改和扩展,例如添加更多关卡、优化游戏界面等。请根据自己的需求进行调整。另外,请注意,在毕业设计论文中,还需要包括游戏系统的设计思路、功能介绍、代码分析、测试结果等相关内容。