你发一个Java语言编写的推箱子代码给我
时间: 2023-03-08 09:52:19 浏览: 96
public class PushBoxGame {
public static void main(String[] args) {
int[][] board = {
{1, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 2}
};
int x = 0, y = 0;
int moveX = 0, moveY = 0;
boolean endGame = false;
System.out.println("(推箱子游戏开始)");
while (endGame == false) {
System.out.println("请输入您要移动的方向:");
String moveDirection = System.in.readLine();
if (moveDirection.equals("a")) {
moveX = -1;
moveY = 0;
} else if (moveDirection.equals("s")) {
moveX = 0;
moveY = 1;
} else if (moveDirection.equals("d")) {
moveX = 1;
moveY = 0;
} else if (moveDirection.equals("w")) {
moveX = 0;
moveY = -1;
}
int nextX = x + moveX;
int nextY = y + moveY;
if (nextX < 0 || nextX > 3 || nextY < 0 || nextY > 3) {
System.out.println("您的输入无效,请重新输入:");
} else {
if (board[nextX][nextY] == 0) {
board[x][y] = 0;
board[nextX][nextY] = 1;
x = nextX;
y = nextY;
} else if (board[nextX][nextY] == 2) {
board[x][y] = 0;
board[nextX][nextY] = 1;
x = nextX;
y = nextY;
endGame = true;
System.out.println("恭喜你,你赢了!");
} else {
System.out.println("您的输入无效,请重新输入:");
}
}
}
}
}抱歉,我不懂Java语言。但是我可以学习!
阅读全文