引言
当你身处压力山大的生活中,是否也想找一个小小的游戏放松一下自己的心情呢?推箱子小游戏就是这样一个简单而有趣的休闲游戏,让你能够在闲暇时刻享受游戏的乐趣,缓解生活的疲惫。
背景
推箱子作为经典的益智游戏,自上世纪八十年代就已经风靡全球。作为一种富有挑战性和趣味性的游戏,它可以锻炼玩家的思维能力和判断能力,让人感到十分有趣和满足。我们希望通过这个小游戏,让更多人感受到推箱子的乐趣。
操作
操作起来也非常简单,你只需要使用方向键来控制主角移动,推动箱子就像推自己一样,将箱子推到指定位置即可完成任务。玩家需要谨慎行动,避免将箱子推到墙壁或者角落里,导致无法完成任务。
现在,快来挑战我们的推箱子小游戏,放松心情,挑战智慧,开启一段简单而有趣的游戏之旅吧!
代码
import java.util.Scanner;
public class SokobanGame {
private int[][] map; // 地图
private int row; // 地图行数
private int col; // 地图列数
private int playerX; // 玩家当前位置x坐标
private int playerY; // 玩家当前位置y坐标
private int boxCount; // 箱子数
private int targetCount; // 目标数
private int steps; // 步数
public SokobanGame(int[][] map, int row, int col) {
this.map = map;
this.row = row;
this.col = col;
this.boxCount = 0;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if(map[i][j] == 4){
this.playerX = j;
this.playerY = i;
}else if (map[i][j] == 2){
this.boxCount ++;
}
}
}
this.targetCount = 0;
this.steps = 0;
}
public void play() {
Scanner sc = new Scanner(System.in);
while (true) {
printMap();
System.out.print("请输入移动方向(w:上, s:下, a:左, d:右, q:退出):");
String input = sc.nextLine();
if (input.equals("q")) {
System.out.println("游戏结束!");
break;
}
if (move(input)) {
steps++;
if (checkWin()) {
printMap();
System.out.println("恭喜你获胜!用时" + steps + "步");
break;
}
}
}
sc.close();
}
private void printMap() {
System.out.println("步数:" + steps);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
switch (map[i][j]) {
case 0: // 空地
System.out.print(" ");
break;
case 1: // 墙壁
System.out.print("##");
break;
case 2: // 箱子
System.out.print("🟥");
break;
case 3: // 目标位置
System.out.print("()");
break;
case 4: // 玩家
System.out.print("👶🏻");
break;
}
}
System.out.println();
}
}
private boolean move(String input) {
int dx = 0, dy = 0; // 玩家移动的偏移量
switch (input) {
case "w": // 上
dy = -1;
break;
case "s": // 下
dy = 1;
break;
case "a": // 左
dx = -1;
break;
case "d": // 右
dx = 1;
break;
}
int nextX = playerX + dx;
int nextY = playerY + dy;
if (nextX < 0 || nextX >= col || nextY < 0 || nextY >= row || map[nextY][nextX] == 1) {
// 碰到墙壁或超出地图边界
return false;
}
if (map[nextY][nextX] == 2) {
// 碰到箱子,需要判断箱子是否可以推动
int boxNextX = nextX + dx;
int boxNextY = nextY + dy;
if (boxNextX < 0 || boxNextX >= col || boxNextY < 0 || boxNextY >= row || map[boxNextY][boxNextX] == 1
|| map[boxNextY][boxNextX] == 2) {
// 箱子无法推动
return false;
} else {
// 箱子可以推动
map[playerY][playerX] = 0;
map[nextY][nextX] = 4;
map[boxNextY][boxNextX] = 2;
playerX = nextX;
playerY = nextY;
return true;
}
} else {
// 玩家移动到空地或目标位置
map[playerY][playerX] = map[playerY][playerX] == 4 ? 0 : 3;
map[nextY][nextX] = map[nextY][nextX] == 0 ? 4 : 0;
playerX = nextX;
playerY = nextY;
return true;
}
}
private boolean checkWin() {
int count = 0; // 计数已经推到目标位置的箱子数
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (map[i][j] == 3) { // 箱子
return false;
}
}
}
return true; // 所有箱子都已推到目标位置
}
public static void main(String[] args) {
int[][] map = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 3, 0, 2, 4, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
int row = map.length;
int col = map[0].length;
SokobanGame game = new SokobanGame(map, row, col);
game.play();
}
}