Android Java战舰游戏:在2D网格上绘制战舰

Android Java战舰游戏:在2D网格上绘制战舰,java,android,grid-layout,Java,Android,Grid Layout,我目前正在编写一个战舰游戏,我用2个for循环制作了10x10网格。现在我想做的是根据是否有飞船改变单元格的颜色 目前作为一个测试用例,我有一艘3号飞船,坐标为1,4,水平航行。船舶存储为Ships[i]=ShipDataint size、布尔值ISHORISTAL、int left、int top。在我看来,我有几种不同的油漆,shipPaint应该以黑色填充单元格,但是我不确定如何继续,因为我不知道如何在onDraw方法期间检查船舶 这是我在onDraw方法中绘制初始网格的方法: for (

我目前正在编写一个战舰游戏,我用2个for循环制作了10x10网格。现在我想做的是根据是否有飞船改变单元格的颜色

目前作为一个测试用例,我有一艘3号飞船,坐标为1,4,水平航行。船舶存储为Ships[i]=ShipDataint size、布尔值ISHORISTAL、int left、int top。在我看来,我有几种不同的油漆,shipPaint应该以黑色填充单元格,但是我不确定如何继续,因为我不知道如何在onDraw方法期间检查船舶

这是我在onDraw方法中绘制初始网格的方法:

for (int i = 0; i < noOfColumns; i++) {
    int ystart = i * boxWidth;
    for (int j = 0; j < noOfRows; j++) {
        int xstart = j * boxWidth;


            box = new Rect(ystart, xstart, (ystart+boxWidth), (xstart+boxWidth));
            int var = mGame.cellStatus(i, j);
            switch (var)
                {
                    case -1: paint = fillPaint;

                    case 0: case 1: case 2: case 3: case 4: paint = shipPaint;
                }


            canvas.drawRect(box, paint);
            canvas.drawRect(box, borderPaint);

    }
}
所以我需要检查1,4 2,4 3,4并从cellStatus返回0,但我不知道如何写入cellStatus。cellStatus的参数包括列、行及其在我的游戏类中的公共int

为了澄清,我的cellStatus方法需要检查在任何给定坐标处是否存在ship块,可能是通过读取ShipData类变量类型。ShipData有几种方法,如getLeft、getTop、getRight和getBottom,我不确定如何将列、行的输入转换为关于每个单元格的实际数据,如上所述。因此,cellStatus的输出对于一个空单元格需要为-1,对于5艘船中的每艘飞船需要为0-4

对于这个冗长的问题我深表歉意,我已经尽力做到了尽可能精确。如果您需要进一步的信息,请让我知道,我会提供它

不幸的是,我以前的帖子没有得到任何答案,从那以后我取得了一些进展

@Override
public int cellStatus(int column, int row) {
int cellStatus = 0;
for (int i = 0; i < 5; i++){
    int maxSize = ships[i].getSize();
    for (int size = 0; size < maxSize; size++ ) {
        ships[i].getLeft();
    }
    cellStatus = ships[i].getLeft();

}

return cellStatus;
}    
这就是我目前得到的,但我不确定如何使cellStatus返回船舶每个坐标的所需值,我只是不知道如何使用StartEFT、startTop和orientation等迭代这些值

在这个例子中,x≡ col和y≡ 划船

将此方法添加到船舶类别:

所以这个测试用例:

    ships[0] = new Ship(3, true, 1, 4);
    ships[1] = new Ship(2, false, 1, 6);
    ships[2] = new Ship(2, true, 0, 9);
    ships[3] = new Ship(4, true, 5, 1);
    ships[4] = new Ship(5, false, 7, 5);
使用此测试代码:

    for (int row = 0; row < 10; row++) { 
        for (int col = 0; col < 10; col++) {
            int cellStat = cellStatus(col, row);
            String shipSymbol = (cellStat == -1 ? "-" : Integer.toString(cellStat));
            System.out.print(shipSymbol+" ");
        }
        System.out.println();
    }
如果无法修改Ship类,只需将上面定义的contains方法更改为:

public static boolean contains(Ship ship, int x, int y) {
    // modify all field accesses to ship.get...() accesses.
}
并将ship循环修改为:

if (contains(ships[shipIdx],column,row) {...}
    for (int row = 0; row < 10; row++) { 
        for (int col = 0; col < 10; col++) {
            int cellStat = cellStatus(col, row);
            String shipSymbol = (cellStat == -1 ? "-" : Integer.toString(cellStat));
            System.out.print(shipSymbol+" ");
        }
        System.out.println();
    }
  - - - - - - - - - - 
  - - - - - 3 3 3 3 - 
  - - - - - - - - - - 
  - - - - - - - - - - 
  - 0 0 0 - - - - - - 
  - - - - - - - 4 - - 
  - 1 - - - - - 4 - - 
  - 1 - - - - - 4 - - 
  - - - - - - - 4 - - 
  2 2 - - - - - 4 - - 
public static boolean contains(Ship ship, int x, int y) {
    // modify all field accesses to ship.get...() accesses.
}
if (contains(ships[shipIdx],column,row) {...}