简体   繁体   中英

Search a 2D array for a character

I am writing a program that is a maze. The user is denoted by the character 'P', and I need to be able to locate that character in order to assign the values for my move commands. I am confused as to how to find 'P' in my maze.

public static void main(String[] args) throws Exception{

    //Display the maze
    char treasureMaze[][] = {{'P','.','X','X','.'},{'.','X','.','.','.'},{'.','.','.','X','.'},{'X','X','T','.','.'},{'.','.','X','.','.'}}; 
    display(treasureMaze);


    //Give Move Options
    options();

    //Get Users Decision
    Scanner moveChoice = new Scanner(System.in);
    int choice = moveChoice.nextInt();

    if(choice == 1){
        System.out.println("You chose to Move up");
    }
    else if(choice == 2){
        System.out.println("You chose to Move down");
    }
    else if(choice == 3){
        System.out.println("You chose to Move left");
    }
    else if(choice == 4){
        System.out.println("you chose to Move right");
    }
    else{
        return;
    }


    //Move the Player
    //Move Up
    if(choice == 1){
        if(treasureMaze[0-1][0] == '.'){
            treasureMaze[0-1][0] = 'P';
            treasureMaze[0-1][0] = '.';
        }
        else if(treasureMaze[0-1][0] == 'T'){
            System.out.println("Congratulations you won!");
        }
        else{
            System.out.println("Cannot move there! Try something else");
        }
    }

    //Move Down
    else if(choice == 2){
        if(treasureMaze[0+1][0] == '.'){
            treasureMaze[0+1][0] = 'P';
            treasureMaze[0][0] = '.';
        }               
        else if(treasureMaze[0+1][0] == 'T'){
            System.out.println("Congratulations you won!");
        }
        else{                   
            System.out.println("Cannot move there! Try something else");
            }
            }

    //Move Left
    else if(choice == 3){
        if(treasureMaze[0][0-1] == '.'){
            treasureMaze[0][0-1] = 'P';
            treasureMaze[0][0] = '.';
        }
        else if(treasureMaze[0][0-1] == 'T'){
            System.out.println("Congratulations you won!");
        }
        else{
            System.out.println("Cannot move there! Try something else");
        }
    }

    //Move Right
    else if(choice == 4){
    if(treasureMaze[0][0+1] == '.'){
        treasureMaze[0][0+1] = 'P';
        treasureMaze[0][0] = '.';
    }
    else if(treasureMaze[0][0+1] == 'T'){
        System.out.println("Congratulations you won!");
    }
    else{
        System.out.println("Cannot move there! Try something else");
    }
    }
    else{
        return;
    }
    display(treasureMaze);
    options();
}



//Display Object: prints out the maze for the user
public static void display(char x[][]){
    for(int row = 0; row < x.length; row++){
        for(int column = 0; column < x[row].length; column++){
            System.out.print(x[row][column] + "\t");
        }
        System.out.println();
    }
}

//Options Object: gives the options menu to the user
 static void options(){
     System.out.println("You may:");
        System.out.println("\t1) Move up");
        System.out.println("\t2) Move down");
        System.out.println("\t3) Move left");
        System.out.println("\t4) Move right");
        System.out.println("\t0) Quit");

}

I have it setup to move the 'P' from its start location, but do not know how to locate it for the next run through. Any ideas?

I'd suggest keeping track of the current location, so you don't have to find it. Declare two variables for the horizontal and vertical position of the player. Initialize them when you set up the board (looks like they should be (0, 0)) and update them whenever the player moves.

Ted's answer is probably best for the way your program is currently set up.

As an alternative, you could replace your 2D array with a tree. A side benefit of that approach would be that you wouldn't have to worry about the array index going out of bounds. Each room would have references to the other rooms it connected to, invalid directions could just be null and you could keep a reference around to the player's current room.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM