Android Java 战舰游戏:在二维网格上绘制船只
Android Java Battleship Game: Drawing ships on a 2D grid
我目前正在编写一个战舰游戏,我使用 2 个 for 循环制作了 10x10 网格。现在我要做的是根据那里是否有船来更改单元格颜色。
目前作为测试用例,我有一艘 3 号船,坐标 (1,4) 水平移动。船舶存储为 ships[i] = ShipData(int size, boolean isHorizontal, 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);
}
}
问题是,我不知道怎么写cellStatus方法,它需要return-1如果它是一个空单元格,或者0到4如果它是某艘船的一部分(5艘船总共
所以我需要从具有4个变量的ShipData class中提取数据,即ships[i] = ShipData(size, isHorizontal, left, top),并计算是否有船块在任何给定的单元格,并相应地绘制单元格。目前每个单元格都是一样的颜色因为我的方法还没有写出来
ships[0] = new ShipData(3, true, 1, 4);
所以我需要检查cellStatus中的(1,4) (2,4) (3,4)和return 0,但是我不知道cellStatus怎么写。 cellStatus 的参数是(列,行),它是我游戏中的一个 public int class.
澄清一下,我的 cellStatus 方法需要检查在任何给定坐标处是否有船块,大概是通过读取 ShipData class 变量类型。 ShipData 有几种方法,例如 getLeft、getTop、getRight 和 getBottom,我不确定如何将列、行的输入转换为每个单元格的实际数据,如上所述。因此,对于空单元格,cellStatus 的输出需要为 -1,对于 5 艘船中的每艘船,需要为 0-4。
对于冗长的问题,我深表歉意,我已尽力做到准确无误。如果您需要更多信息,请告诉我,我会提供。
不幸的是我的老post没有得到任何答案,从那以后我取得了一些进步,
@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 return 成为船舶每个坐标的所需值,我只是不知道如何遍历这些值使用 startLeft、startTop 和方向等
这是一种方法:
在这个例子中(x ≡ 列和 y ≡ 行)
将此方法添加到您的 Ship
class:
/** @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 class,那么只需将上面定义的 contains
方法更改为:
public static boolean contains(Ship ship, int x, int y) {
// modify all field accesses to ship.get...() accesses.
}
并将船循环修改为:
if (contains(ships[shipIdx],column,row) {...}
我目前正在编写一个战舰游戏,我使用 2 个 for 循环制作了 10x10 网格。现在我要做的是根据那里是否有船来更改单元格颜色。
目前作为测试用例,我有一艘 3 号船,坐标 (1,4) 水平移动。船舶存储为 ships[i] = ShipData(int size, boolean isHorizontal, 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);
}
}
问题是,我不知道怎么写cellStatus方法,它需要return-1如果它是一个空单元格,或者0到4如果它是某艘船的一部分(5艘船总共
所以我需要从具有4个变量的ShipData class中提取数据,即ships[i] = ShipData(size, isHorizontal, left, top),并计算是否有船块在任何给定的单元格,并相应地绘制单元格。目前每个单元格都是一样的颜色因为我的方法还没有写出来
ships[0] = new ShipData(3, true, 1, 4);
所以我需要检查cellStatus中的(1,4) (2,4) (3,4)和return 0,但是我不知道cellStatus怎么写。 cellStatus 的参数是(列,行),它是我游戏中的一个 public int class.
澄清一下,我的 cellStatus 方法需要检查在任何给定坐标处是否有船块,大概是通过读取 ShipData class 变量类型。 ShipData 有几种方法,例如 getLeft、getTop、getRight 和 getBottom,我不确定如何将列、行的输入转换为每个单元格的实际数据,如上所述。因此,对于空单元格,cellStatus 的输出需要为 -1,对于 5 艘船中的每艘船,需要为 0-4。
对于冗长的问题,我深表歉意,我已尽力做到准确无误。如果您需要更多信息,请告诉我,我会提供。
不幸的是我的老post没有得到任何答案,从那以后我取得了一些进步,
@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 return 成为船舶每个坐标的所需值,我只是不知道如何遍历这些值使用 startLeft、startTop 和方向等
这是一种方法:
在这个例子中(x ≡ 列和 y ≡ 行)
将此方法添加到您的 Ship
class:
/** @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 class,那么只需将上面定义的 contains
方法更改为:
public static boolean contains(Ship ship, int x, int y) {
// modify all field accesses to ship.get...() accesses.
}
并将船循环修改为:
if (contains(ships[shipIdx],column,row) {...}