0

ゲーム戦艦の placeShips メソッドを書いています。ほとんどの場合は機能します...船を配置しようとしているときにエラー状態に遭遇した場合(ボードから外れたり、別の船がその場所にあるなど)、船の文字コードを.そうすべきではないことに気付くまで、スペースを空けてください。カスケードされた if ステートメントを使用しようとしましたが、この問題を解決する他の方法がわかりません。これがコードのスニペットです。5 つの異なる船がありますが、これはすべての文字が ~ に初期化された 10x10 配列に最初の船を配置するためのコードです。

int count=1;
while(count<=5){
    start: {
    if(count==1){
    System.out.println("What row and column would you like to place your aircraft carrier?");
    System.out.println("Row?");
    int row1 = IO.readInt();
                    if(row1>9 || row1<0){
                    IO.reportBadInput();
                    break start;
                    }
    System.out.println("Column?");
    int column1 = IO.readInt();
                    if(column1>9 || column1<0){
                    IO.reportBadInput();
                    break start;
                    }

    System.out.println("How would you like to orient your aircraft carrier? 1. Down, 2. Up, 3. Left, 4. Right");
    int orientation = IO.readInt();
    if(orientation==1){ //oriented down

        for(int z=0; z<5; z++){ //places the aircraft carrier, DOWN
        if((row1+z)>=board.length){
            System.out.println("This will go off the board. You can't place your ship here! Try again.");
            IO.reportBadInput();
            break start;
        }

        else if(board[row1+z][column1]!='~'){
            System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
            IO.reportBadInput();
            break start;
        } //checks if there's anything else already in that space

        else{
            board[row1 + z][column1]= 'A';
        } 

        } 
    }
        if(orientation==2){ //oriented up

            for(int z=0; z<5; z++){ //places the aircraft carrier, UP ORIENTATION
            if((row1-z)<0){
                System.out.println("This will go off the board. You can't place your ship here! Try again.");
                IO.reportBadInput();
                break start;
            }
            else if(board[row1-z][column1]!='~'){
                System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
                IO.reportBadInput();
                break start;
            } //checks if there's anything else already in that space
            else{
                board[row1 - z][column1]= 'A';
            }

            }
        }

            if(orientation==3){

                for(int z=0; z<5; z++){ //places the aircraft carrier, LEFT ORIENTATION
                if((column1-z)<0){
                    System.out.println("This will go off the board. You can't place your ship here! Try again.");
                    IO.reportBadInput();
                    break start;
                }
                else if(board[row1][column1-z]!='~'){
                    System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
                    IO.reportBadInput();
                    break start;
                } //checks if there's anything else already in that space
                else{
                    board[row1][column1-z]= 'A'; 
                }

                }
            }


            if(orientation==4){

                for(int z=0; z<5; z++){ //places the aircraft carrier, RIGHT ORIENTATION
                if((column1+z)>=9){
                    System.out.println("This will go off the board. You can't place your ship here! Try again.");
                    IO.reportBadInput();
                    break start;
                }
                else if(board[row1][column1+z]!='~'){
                    System.out.println("Oops! You can't place your ship there. Ship already in this location. Try again.");
                    IO.reportBadInput();
                    break start;
                } //checks if there's anything else already in that space
                else{
                    board[row1][column1+z]= 'A';
                }

                }
}
    count++;
    }
4

0 に答える 0