【问题标题】:Search a 2D array for a character在二维数组中搜索字符
【发布时间】:2012-09-25 19:46:44
【问题描述】:

我正在编写一个迷宫程序。用户由字符“P”表示,我需要能够找到该字符以便为我的移动命令分配值。我对如何在迷宫中找到“P”感到困惑。

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");

}

我已将它设置为将“P”从其起始位置移动,但不知道如何定位它以进行下一次运行。有什么想法吗?

【问题讨论】:

  • 使用if(treasureMaze[0-1][0] == '.') 之类的代码会给自己带来麻烦。 0-1 = -1,尝试引用数组位置 -1 会得到一个ArrayIndexOutOfBoundsException。你的意思可能是currentLocation - 1
  • 是的,我之前看到有没有办法说如果数组位置不存在然后做 x?
  • 您可以计算数组位置,然后如果它小于零或大于数组大小,则执行某些操作。否则,它是一个有效的位置,并执行其他操作。
  • 有道理,非常感谢!

标签: java arrays search 2d


【解决方案1】:

我建议跟踪当前位置,这样您就不必找到它。为玩家的水平和垂直位置声明两个变量。在设置棋盘时初始化它们(看起来应该是 (0, 0))并在玩家移动时更新它们。

【讨论】:

  • 所以水平和垂直位置都有自己的变量?从水平 = 0 开始,与垂直相同。然后将这些值粘贴到 TreasureMaze 数组中并随着动作更新?
  • @Eric - 我不知道您所说的“将这些值粘贴到 TreasureMaze 数组中”是什么意思。它们应该作为不同的变量保存,与数组分开,并用于索引到数组中。
  • 是的,我想通了。我只是措辞不好非常感谢您的帮助。
【解决方案2】:

Ted 的回答可能最适合您当前的程序设置方式。

作为替代方案,您可以将二维数组替换为一棵树。这种方法的一个附带好处是您不必担心数组索引超出范围。每个房间都会引用它所连接的其他房间,无效的方向可能只是null,您可以保留对玩家当前房间的引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 2020-07-27
    • 2014-05-05
    • 1970-01-01
    相关资源
    最近更新 更多