X表示起点,E表示出口。问题是我无法移动X。有人可以帮我弄这个吗。我们只允许使用main类和main方法,除此之外别无其他。每次用户输入时,迷宫地图都会显示,显示玩家的当前位置。当用户选择向左或向右时,当前位置不应更改。
import java.util.*;
public class g7 {
public static void main(String []args) throws Exception {
String[][] M = {{"0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "}
,{"X"," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "}
,{"0"," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," "," "," "," "," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "}
,{"0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "}
,{"0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "}
,{"0"," "," "," "," "," "," "," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "}
,{"0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," "}
,{"0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "}
,{"0"," "," "," "," "," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," "," "," "," "," "," "," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," "," "," ","0"," ","0"," "," "," ","0"," ","0"," "," "," ","0"," ","0"," ","0"," ","0"," ","0"," "}
,{"0"," ","0"," ","0"," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," ","0"," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," ","0"," ","0"," "," "," "," "," "," "," "," "," "," "," ","0"," "}
,{"0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","0"," ","E"," ","0"," "}};
do{
System.out.println("-------------------------MAZE------------------------------");
System.out.println();
for(String[] a:M){
for(String b:a){
System.out.print(b);
}
System.out.println();
}
System.out.println("-----------------------------------------------------------");
System.out.print("F - move Forward\tB - move Backward\t\nR - face Right\t\tL - face Left\n");
System.out.print("Enter your move: ");
String c = new Scanner(System.in).next();
if(c.equals("F")){
M[1][2] = M[1][0];
M[1][0] = " ";
}
}while(true);
}
}
一种便宜的解决方案是,在每次输入键盘后都以更新的位置打印迷宫
任务:
- 将M从主要方法移至常数-
static String[][] M = ...;
- 键盘输入后绘制的设置循环-
static boolean isDone
- 创建用于绘制迷宫的方法
static void printMaze()
- 创建用于处理输入
static void readInput()
的方法- 使用变量处理玩家位置-
static int xPlayer
和static int yPlayer
static String[][] M = ...
static boolean isDone;
static int xPlayer;
static int yPlayer;
public static void main(String []args){
//repeated drawing of the screen after input
while(!isDone){
printMaze();
readInput();
}
}
static void printMaze(){
int height = M.length;
for(int y = 0; y < height; y++){
int width = M[0].length;
for(int x = 0; x < width; x++){
System.out.print(M[x][y]);
if(x == xPlayer && y == yPlayer){
System.out.print("X");
}
}
System.out.println();
}
System.out.println();
}
static void readInput(){
System.out.print("F - move Forward\tB - move Backward\t\nR - face Right\t\tL - face Left\n");
System.out.print("Enter your move: ");
String c = new Scanner(System.in).next();
//handle input
if (c.equalsIgnore("f")){
yPlayer = yPlayer - 1;
}
//TODO: handle the other 3 directions the same way
//BONUS: quit the loop
if (c.equalsIgnore("quit)){
isDone = true;
}
}