2016-05-16 116 views
-3

你必须创建两个独立的程序,一个主程序和一个战列舰类。其他人正在让主程序询问用户猜测坐标,您可以输入设置变量来伪装成用户的猜测。如何在元素中搜索2d数组?

公共类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

首先我需要知道在哪里的船在你的矩阵中。之后,在用户输入的情况下,您只需搜索矩阵以确定输入是否与任何船舶匹配。

假设你有位置为a [3] [3]的船。验证的最佳方法是将所述位置的内容设置为1(有船时),当没有时为0。

所以,当你得到一个用户猜测如:x = 1; y = 4;

所有你必须是检查所接收到的位置,以找出是否它是一个1(有船)或0(无船有):

if(a[x][y] == 1) { 
    //user guesses correctly 
} else { 
    //user found no ship 
}