0

私は迷路であるプログラムを書いています。ユーザーは文字「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'を開始位置から移動するように設定しましたが、次の実行のためにそれを見つける方法がわかりません。何か案は?

4

2 に答える 2

3

現在の場所を追跡することをお勧めします。そうすれば、それを見つける必要はありません。プレーヤーの水平位置と垂直位置の2つの変数を宣言します。ボードをセットアップするときにそれらを初期化し((0、0)である必要があるように見えます)、プレーヤーが移動するたびに更新します。

于 2012-09-25T19:49:00.847 に答える
1

テッドの答えは、プログラムが現在設定されている方法におそらく最適です。

別の方法として、2D配列をツリーに置き換えることができます。このアプローチの副次的な利点は、配列インデックスが範囲外になることを心配する必要がないことです。各部屋には、接続している他の部屋への参照があり、無効な方向が存在する可能null性があり、プレーヤーの現在の部屋への参照を保持できます。

于 2012-09-25T20:02:42.640 に答える