2016-05-16 10 views
-3

メインプログラムとBattleshipクラスの2つの別々のプログラムを作成する必要があります。他の誰かが主プログラムを取得してユーザーに座標を推測するように求めている場合は、ユーザーが推測できるように設定変数を入力するだけです。私はどこ船知っておく必要があるだろう要素の2次元配列を検索するにはどうすればよいですか?

パブリッククラスMainMethod {

public static void main(String[] args) { 

    Battleship_BraydenH_R1 var = new Battleship_BraydenH_R1(); 

    //initialize variables 
    int[][] LAYOUT = new int[7][7]; 

    int ROW = 1, COLUMN = 2; 
    Battleship_BraydenH_R1.hitDetect(ROW, COLUMN); 

} 

}

パブリッククラスBattleship_BraydenH_R1 {

/** 
*Program Header 
*Program Name: Battleship 
*Program Description: This class tells the main method whether or not a ship has been hit or whether or not a ship has been sunk. 
*Program Creator: Brayden H 
*Revision 1 
*Date: May 9, 2016 
*/ 

public static void batClass() 
{ 
    // initialize variables 
    int[][] LAYOUT = { 
     {0,0,0,1,0,0,0}, 
     {0,0,1,0,0,0,0}, 
     {0,1,0,1,0,0,1}, 
     {1,0,0,1,0,1,0}, 
     {0,0,0,1,1,0,0}, 
     {0,0,0,1,0,0,0}, 
     {0,0,1,0,0,0,0}, 
    }; // layout array 

} // batClass 

public static boolean[] hitDetect (int X, int Y) 
{ 
    int[] counter = new int[3]; // counter array 
    //counter[0] = boat[0]; 
    boolean HIT = false, SUNK = false; // sets 'HIT' and 'SUNK' booleans to false 
    boolean[] STATUS = new boolean [2]; // array with 2 elements to be able to return 'HIT' and 'SUNK' 
    STATUS[0] = false; // MISS 
    STATUS[1] = true; // HIT 

    // if the user guessed the correct coordinates for boat 1 
    if ((X == 3 && Y == 4)||(X == 4 && Y == 4)||(X == 5 && Y == 4)) 
    { 
     STATUS[1] = true; // the boat was hit 
     X = 4; // turns 1 into 4 so the program knows when a ship is sunk 
     Y = 4; // turns 1 into 4 so the program knows when a ship is sunk 
    } // boat 1 

    // if the user guessed the correct coordinates for boat 2 
    else if ((X == 1 && Y == 4)||(X == 2 && Y == 3)||(X == 3 && Y == 2)||(X == 4 && Y == 1)) 
    { 
     STATUS[1] = true; // the boat was hit 
     X = 4; // turns 1 into 4 so the program knows when a ship is sunk 
     Y = 4; // turns 1 into 4 so the program knows when a ship is sunk 
    } // boat 2 

    // if the user guessed the correct coordinates for boat 3 
    else if ((X == 3 && Y == 7)||(X == 4 && Y == 6)||(X == 5 && Y == 5)||(X == 6 && Y == 4)||(X == 7 && Y == 3)) 
    { 
     STATUS[1] = true; // the boat was hit 
     X = 4; // turns 1 into 4 so the program knows when a ship is sunk 
     Y = 4; // turns 1 into 4 so the program knows when a ship is sunk 
    } // boat 3 

    // if a boat was not hit 
    else 
    { 
     STATUS[1] = false; // the boat was not hit 
    } 

    (THIS IS WHERE I WANT THE PROGRAM TO SEARCH THE LAYOUT) 

    // searches layout 
    double a[][]=new double[7][7]; 
    for(int row = 0 ; row < 3 ; row++) 
    { 
     for(int col = 0 ; col < 7 ; col++) 
     { 

     } 
    } 


    return STATUS; // returns 'STATUS' to main method 
} // hitDetect method 

} //戦艦クラス

+1

あなたの質問にいくつかのテキストを追加してください。コードのコメントではなく、わかりやすい方法で問題を説明すると、より良い回答が得られます。 –

+0

完了、説明が助けてくれることを願っています! –

答えて

1

まずオフあなたのマトリックスにあります。その後、ユーザーの入力を考慮して、入力がどの船とも一致するかどうかを調べるために行列を検索するだけで済みます。

は、あなたが位置に船を持っていると言うことができます[3] [3]。前記位置の内容を1(船がある場合)に設定し、0がない場合に0に設定することを確認する最良の方法。

ので、ユーザーは次のように推測し得るとき:X = 1; y = 4;

あなたはその1(持っている船)または0(そこなし船)かどうかを確認するために、受信位置を確認されなければならないだろうすべて:

if(a[x][y] == 1) { 
    //user guesses correctly 
} else { 
    //user found no ship 
} 
関連する問題