推箱子
package Demo;
import java.util.*;
public class Learn {
// 游戏地图
public static char[][] map = {
{'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'},
{'H', '&', ' ', 'H', ' ', ' ', ' ', ' ', ' ', 'H'},
{'H', ' ', 'o', 'H', ' ', 'H', ' ', ' ', ' ', 'H'},
{'H', ' ', ' ', 'H', ' ', 'H', 'H', ' ', 'H', 'H'},
{'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'H', 'H'},
{'H', ' ', ' ', ' ', 'H', 'H', 'H', ' ', ' ', 'H'},
{'H', ' ', ' ', ' ', 'H', '*', ' ', ' ', ' ', 'H'},
{'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H'}
};
// 玩家位置
static int playerRow = 1;
static int playerCol = 1;
// 箱子位置
static int boxRow = 2;
static int boxCol = 2;
// 终点位置
static final int destinRow = 6;
static final int destinCol = 5;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
// 打印当前游戏地图
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 10; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("请输入W/A/S/D来移动人物&");
String option = sc.next();
// 玩家操作
switch(option) {
case "W":
// 判断玩家是否撞墙
if(map[playerRow - 1][playerCol] != 'H') {
// 判断是否推箱
if(map[playerRow - 1][playerCol] == 'o') {
// 判断箱子是否撞墙
if(map[playerRow - 2][playerCol] != 'H') {
// 箱子不撞墙结果
map[playerRow][playerCol] = ' ';
map[playerRow - 1][playerCol] = '&';
map[playerRow - 2][playerCol] = 'o';
playerRow--;
boxRow--;
} else {
// 箱子撞墙结果
System.out.println("无法移动!");
}
} else {
// 不推箱移动结果
map[playerRow][playerCol] = ' ';
map[playerRow - 1][playerCol] = '&';
playerRow--;
}
} else {
// 玩家撞墙结果
System.out.println("无法移动!");
}
break;
case "A":
// 判断玩家是否撞墙
if(map[playerRow][playerCol - 1] != 'H') {
// 判断是否推箱
if(map[playerRow][playerCol - 1] == 'o') {
// 判断箱子是否撞墙
if(map[playerRow][playerCol - 2] != 'H') {
// 箱子不撞墙结果
map[playerRow][playerCol] = ' ';
map[playerRow][playerCol - 1] = '&';
map[playerRow][playerCol - 2] = 'o';
playerCol--;
boxCol--;
} else {
// 箱子撞墙结果
System.out.println("无法移动!");
}
} else {
// 不推箱移动结果
map[playerRow][playerCol] = ' ';
map[playerRow][playerCol - 1] = '&';
playerCol--;
}
} else {
// 玩家撞墙结果
System.out.println("无法移动!");
}
break;
case "S":
// 判断玩家是否撞墙
if(map[playerRow + 1][playerCol] != 'H') {
// 判断是否推箱
if(map[playerRow + 1][playerCol] == 'o') {
// 判断箱子是否撞墙
if(map[playerRow + 2][playerCol] != 'H') {
// 箱子不撞墙结果
map[playerRow][playerCol] = ' ';
map[playerRow + 1][playerCol] = '&';
map[playerRow + 2][playerCol] = 'o';
playerRow++;
boxRow++;
} else {
// 箱子撞墙结果
System.out.println("无法移动!");
}
} else {
// 不推箱移动结果
map[playerRow][playerCol] = ' ';
map[playerRow + 1][playerCol] = '&';
playerRow++;
}
} else {
// 玩家撞墙结果
System.out.println("无法移动!");
}
break;
case "D":
// 判断玩家是否撞墙
if(map[playerRow][playerCol + 1] != 'H') {
// 判断是否推箱
if(map[playerRow][playerCol + 1] == 'o') {
// 判断箱子是否撞墙
if(map[playerRow][playerCol + 2] != 'H') {
// 箱子不撞墙结果
map[playerRow][playerCol] = ' ';
map[playerRow][playerCol + 1] = '&';
map[playerRow][playerCol + 2] = 'o';
playerCol++;
boxCol++;
} else {
// 箱子撞墙结果
System.out.println("无法移动!");
}
} else {
// 不推箱移动结果
map[playerRow][playerCol] = ' ';
map[playerRow][playerCol + 1] = '&';
playerCol++;
}
} else {
// 玩家撞墙结果
System.out.println("无法移动!");
}
break;
}
System.out.println();
// 判断玩家操作结果
if(boxRow == destinRow && boxCol == destinCol) {
System.out.println("恭喜你,挑战成功!");
return;
}
}
}
}