这次是一个推箱子小游戏,因为 java 限制,不能直接获取按键响应,只能使用输入wasd的方式来控制移动。
基本上和走迷宫差不对,只不过逻辑上增加了对箱子位置的判断。
↓简陋的界面

演示视频
程序流程

程序代码
1import java.util.HashMap;
2import java.util.Scanner;
3
4public class BoxMan {
5 private static char[][] map = {
6 {' ', ' ',' ', '#', '#', '#', ' ', ' ', ' ',},
7 {' ', ' ',' ', '#', '*', '#', ' ', ' ', ' ',},
8 {' ',' ', ' ', '#', ' ', '#', ' ', ' ', ' ',},
9 {' ', ' ',' ', '#', 'O', '#', '#', '#', '#',},
10 {' ',' ', ' ', '#', 'M', 'O', ' ', '*', '#',},
11 {'#','#', '#', '#', 'O', '#', '#', '#', '#',},
12 {'#', '*', ' ','O', ' ', '#', ' ', ' ', ' ',},
13 {'#','#', '#', '#', ' ', '#', ' ', ' ', ' ',},
14 {' ',' ', ' ', '#', '*', '#', ' ', ' ', ' ',},
15 {' ',' ', ' ', '#', '#', '#', ' ', ' ', ' ',},
16 };
17 //存储一些位置数据 格式 名称,位置(x,y)
18 private static HashMap<String, String> coordinateMap = new HashMap<String, String>();
19 private static boolean isGameEnd =false;
20
21 public static void main(String[] args) {
22 while (true){
23 Scanner scanner=new Scanner(System.in);
24 printfMap(map);
25 findCoordinate(map);
26 printfCoordinate();
27
28 if (isGameEnd){
29 System.out.println("恭喜,成功过关!");
30 break;
31 }else {
32 doMove(scanner.nextLine());
33 }
34 }
35
36 }
37 //人物移动
38 private static void doMove(String move){
39 String[] manCoordinate = coordinateMap.get("MAN").split(",");
40 int manX=Integer.valueOf(manCoordinate[0]);
41 int manY=Integer.valueOf(manCoordinate[1]);
42
43
44 //上
45 if("w".equals(move)||"W".equals(move)){
46 //判断是否是墙,或已经到位的箱子
47 if('#'!=map[manX-1][manY]&&'@'!=map[manX-1][manY]){
48 //前进方向上有箱子?有推箱子
49 if('O'==map[manX-1][manY]){
50 //箱子前面是墙?不是推箱子
51 if('#'!=map[manX-2][manY]){
52 //箱子前面是终点?推箱子,并改变箱子的样子
53 if('*'==map[manX-2][manY]){
54 map[manX][manY]=' ';
55 map[manX-1][manY]='M';
56 map[manX-2][manY]='@';
57 }else{
58 map[manX][manY]=' ';
59 map[manX-1][manY]='M';
60 map[manX-2][manY]='O';
61 }
62 }
63 }else{
64 map[manX][manY]=' ';
65 map[manX-1][manY]='M';
66 }
67 }
68 }
69 //下
70 if("s".equals(move)||"S".equals(move)){
71 if('#'!=map[manX+1][manY]&&'@'!=map[manX+1][manY]){
72 if('O'==map[manX+1][manY]){
73 if('#'!=map[manX+2][manY]){
74 if('*'==map[manX+2][manY]){
75 map[manX][manY]=' ';
76 map[manX+1][manY]='M';
77 map[manX+2][manY]='@';
78 }else{
79 map[manX][manY]=' ';
80 map[manX+1][manY]='M';
81 map[manX+2][manY]='O';
82 }
83 }
84 }else{
85 map[manX][manY]=' ';
86 map[manX+1][manY]='M';
87 }
88 }
89 }
90 //左
91 if("a".equals(move)||"A".equals(move)){
92 if('#'!=map[manX][manY-1]&&'@'!=map[manX][manY-1]){
93 if('O'==map[manX][manY-1]){
94 if('#'!=map[manX][manY-2]){
95 if('*'==map[manX][manY-2]){
96 map[manX][manY]=' ';
97 map[manX][manY-1]='M';
98 map[manX][manY-2]='@';
99 }else{
100 map[manX][manY]=' ';
101 map[manX][manY-1]='M';
102 map[manX][manY-2]='O';
103 }
104 }
105 }else{
106 map[manX][manY]=' ';
107 map[manX][manY-1]='M';
108 }
109 }
110 }
111 //右
112 if("d".equals(move)||"D".equals(move)){
113 if('#'!=map[manX][manY+1]&&'@'!=map[manX][manY+1]){
114 if('O'==map[manX][manY+1]){
115 if('#'!=map[manX][manY+2]){
116 if('*'==map[manX][manY+2]){
117 map[manX][manY]=' ';
118 map[manX][manY+1]='M';
119 map[manX][manY+2]='@';
120 }else{
121 map[manX][manY]=' ';
122 map[manX][manY+1]='M';
123 map[manX][manY+2]='O';
124 }
125 }
126 }else{
127 map[manX][manY]=' ';
128 map[manX][manY+1]='M';
129 }
130 }
131 }
132 }
133
134 //自动寻找关键点坐标
135 private static void findCoordinate(char[][] map) {
136 int countMan = 0;
137 int countBox = 0;
138 int countEnd = 0;
139 coordinateMap.clear();
140 for (int i = 0; i < map.length; i++) {
141 for (int j = 0; j < map[i].length; j++) {
142 if ('M' == map[i][j]) {
143 countMan++;
144 coordinateMap.put("MAN", i + "," + j);
145 }
146 if ('O' == map[i][j]) {
147 countBox++;
148 coordinateMap.put("BOX" + countBox, i + "," + j);
149 }
150 if ('*' == map[i][j]) {
151 countEnd++;
152 coordinateMap.put("EMD" + countEnd, i + "," + j);
153 }
154 }
155 }
156 if(countBox!=countEnd){
157 System.err.println("箱子或终点数量有误,请检查关卡地图!");
158 System.exit(1);
159 }
160 if(countMan!=1){
161 System.err.println("人物位置错误,请检查关卡地图!");
162 System.exit(1);
163 }
164 if(countBox==0){
165 isGameEnd =true;
166 }
167
168 }
169 //打印寻找的坐标
170 private static void printfCoordinate() {
171 for (String key : coordinateMap.keySet()) {
172 System.out.print("| "+key + ": " + coordinateMap.get(key)+" | ");
173 }
174 System.out.println();
175 }
176 //打印地图
177 private static void printfMap(char[][] map) {
178 for (char[] chars : map) {
179 for (char c : chars) {
180 System.out.print(c);
181 }
182 System.out.println();
183 }
184 }
185}