问题
X signifies the starting point and E is the Exit. The problem is I cannot make my X move. Can someone help me with this. We are only allowed to used the main class and main method, no any other than that. Every time the user input, the maze map will display showing the current position of the player. When the user choose to face left or right, the current position should not change.
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);
}
}
回答1:
one cheap solution would be that you print the maze with the updated position after every keyboard input
Tasks:
- move M from the main method into a constant -
static String[][] M = ...;
- setup loop that draws after keyboard input -
static boolean isDone
- create a method for drawing the maze
static void printMaze()
- create a method for handling input
static void readInput()
- handle the player position with variables -
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;
}
}
来源:https://stackoverflow.com/questions/58987624/java-text-based-maze-game