简单的推箱子
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
char map[][] = new char[8][10];
Scanner sc = new Scanner(System.in);
int x = 1, y = 1;
boolean finish = false;
for (int i = 0; i < map.length; i++) {
if (i == 0 || i == 7) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = 'H';
}
} else {
map[i][0] = 'H';
map[i][9] = 'H';
}
}
map[1][3] = 'H';
map[2][3] = 'H';
map[3][3] = 'H';
map[2][5] = 'H';
map[3][5] = 'H';
map[3][6] = 'H';
map[3][8] = 'H';
map[4][8] = 'H';
map[6][4] = 'H';
map[5][4] = 'H';
map[5][5] = 'H';
map[5][6] = 'H';
map[x][y] = '&';
map[2][2] = 'o';
map[6][5] = '*';
while (true) {
System.out.println("--------------------");
for (char row[] : map) {
for (char column : row) {
System.out.print(column + " ");
}
System.out.println();
}
System.out.println("--------------------");
if (finish) {
break;
}
System.out.println("A左移,D右移,W上移,S下移,请输入你的指令:");
String code = sc.nextLine();
switch (code.toLowerCase()) {
case "a" :// 如果输入的是a
if (map[x][y - 1] == 0) {
map[x][y] = 0;
map[x][y - 1] = '&';
y--;
} else if (map[x][y - 1] == 'o') {
if (map[x][y - 2] != 'H') {
if (map[x][y - 2] == '*') {
finish = true;
}
map[x][y] = 0;
map[x][y - 1] = '&';
map[x][y - 2] = 'o';
y--;
}
}
break;
case "d" :// 如果输入的是d
if (map[x][y + 1] == 0) {
map[x][y] = 0;
map[x][y + 1] = '&';
y++;
} else if (map[x][y + 1] == 'o') {
if (map[x][y + 2] != 'H') {
if (map[x][y + 2] == '*') {
finish = true;
}
map[x][y] = 0;
map[x][y + 1] = '&';
map[x][y + 2] = 'o';
y++;
}
}
break;
case "w" :// 如果输入的是w
if (map[x - 1][y] == 0) {
map[x][y] = 0;
map[x - 1][y] = '&';
x--;
} else if (map[x - 1][y] == 'o') {
if (map[x - 2][y] != 'H') {
if (map[x - 2][y] == '*') {
finish = true;
}
map[x][y] = 0;
map[x - 1][y] = '&';
map[x - 2][y] = 'o';
x--;
}
}
break;
case "s" :// 如果输入的是s
if (map[x + 1][y] == 0) {
map[x][y] = 0;
map[x + 1][y] = '&';
x++;
} else if (map[x + 1][y] == 'o') {
if (map[x + 2][y] != 'H') {
if (map[x + 2][y] == '*') {
finish = true;
}
map[x][y] = 0;
map[x + 1][y] = '&';
map[x + 2][y] = 'o';
x++;
}
}
break;
default :// 如果输入的是其他指令
System.out.println("您输入的指令有误!");
}
}
System.out.println("游戏结束");
sc.close();
}
}
仅供参考
转载请注明原文地址:https://ipadbbs.8miu.com/read-53.html