簡體   English   中英

在2D數組中搜索字符

[英]Search a 2D array for a character

我正在編寫一個迷宮程序。 用戶由字符“ 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”,但不知道如何為下一次運行找到它。 有任何想法嗎?

我建議您跟蹤當前位置,因此不必找到它。 為播放器的水平和垂直位置聲明兩個變量。 設置板卡時初始化它們(看起來它們應該是(0,0)),並在玩家移動時進行更新。

泰德(Ted)的答案可能最適合您當前程序的設置方式。

另外,您也可以用樹替換2D陣列。 這種方法的附帶好處是您不必擔心數組索引超出范圍。 每個房間都將引用與其連接的其他房間,無效的指示可能只是null ,您可以保留對玩家當前房間的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM