如何在二维数组中搜索元素?

时间:2016-05-16 13:52:55

标签: java arrays battlenet-api

你必须创建两个独立的程序,一个主程序和Battleship类。其他人正在让主程序要求用户猜测一个坐标,你可以输入设置变量来假装是用户的猜测。

公共类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
}
相关问题