[英]Android Java Battleship Game: Drawing ships on a 2D grid
我目前正在編寫一艘戰艦游戲,並且使用2個for循環制作了10x10網格。 現在,我要做的就是根據是否有船來更改單元格的顏色。
目前作為測試用例,我有一個大小為3的飛船,其坐標為水平(1,4)。 船舶存儲為ships [i] = ShipData(整數大小,布爾值isHorizontal,整數左,整數頂)。 我認為有幾種不同的繪畫,並且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);
}
}
問題是,我不知道如何編寫cellStatus方法,如果它是一個空單元格,則需要返回-1;如果它屬於某艘船(總共5艘船),則需要返回0到4。
因此,我需要從ShipData類中提取具有4個變量的數據,即ships [i] = ShipData(size,isHorizontal,left,top),並計算在任何給定單元格中是否存在飛船,並相應地繪制該單元格。 目前,每個單元格的顏色與我尚未編寫的方法相同。
ships[0] = new ShipData(3, true, 1, 4);
所以我需要檢查(1,4)(2,4)(3,4)並從cellStatus返回0,但是我不知道如何編寫cellStatus。 cellStatus的參數是(列,行),並且在游戲類中是public int。
為了闡明這一點,我的cellStatus方法需要檢查任何給定坐標處是否存在飛船阻擋,大概是通過讀取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返回船的每個坐標的期望值,我只是不知道如何使用startLeft,startTop和direction等來迭代這些值
這是一種方法:
在此示例中(x≡col和y≡行)
將此方法添加到您的Ship
類:
/** @return true if this Ship contains the point */
public boolean contains(int x, int y) {
int myX = left; int myY = top;
int xInc = (isHorizontal ? 1 : 0);
int yInc = ((xInc+1)%2);
for (int z = 0; z < size; z++) {
if (myX == x && myY == y) {
return true;
}
myX += xInc; myY += yInc;
}
return false;
}
您的cellStatus
方法變為:
public static int cellStatus (int column, int row) {
for (int shipIdx = 0; shipIdx < 5; shipIdx++) {
if (ships[shipIdx].contains(column, row)) {
return shipIdx;
}
}
return -1;
}
所以這個測試用例:
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();
}
生產:
- - - - - - - - - -
- - - - - 3 3 3 3 -
- - - - - - - - - -
- - - - - - - - - -
- 0 0 0 - - - - - -
- - - - - - - 4 - -
- 1 - - - - - 4 - -
- 1 - - - - - 4 - -
- - - - - - - 4 - -
2 2 - - - - - 4 - -
如果您不能修改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) {...}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.