java - 更快地找到邻居索引的方法

标签 java math arraylist

我正在创建一个扫雷游戏,我确实需要一种快速有效的方法来计算地雷的邻居,实际上我将我的图 block 存储在数组列表中,这样我就可以在 GridView 中使用它们,所以位置是线性的,但渲染将是一个矩阵 n*n。我有办法做到这一点,但我认为有人可以有更有效的方法。

我想要实现的目标:

0 1 1 1
0 1 * 1
0 1 1 1
0 0 0 0

因此,鉴于矩阵具有线性列表中的索引,位置应如下:

1  2  3  4
5  6  7  8
9  10 11 12
13 14 15 16

所以我需要一种有效的方法来获取 2, 3, 4, 6, 8, 10, 11, 12 并给出索引 7。

生成炸弹的代码:

public void plantMines(){
    Random rand = new Random();
    //Used set so we dont get duplicates
    Set<Integer> mineCoords = new LinkedHashSet<>(mDifficulty.mines);
    //First we randomly select all coordenates
    while (mineCoords.size() < mDifficulty.mines){
        Integer coord = rand.nextInt(mListCap) + 1;
        mineCoords.add(coord);
    }
    //Now we can set the mines accordingly
    for (Integer coord: mineCoords){
        mTiles.get(coord).setMine(true);
    }
}

查找邻居的实际代码:

for (int row = 0; row < ROW_SIZE; row++) {
        for (int col = 0; col < COL_SIZE; col++) {
            int neighbourBombSize = 0;

            // TOP ROW
            if ((row-1) >= 0 && (col-1) >= 0) {
                if (getTile(row-1, col-1).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            if ((row-1) >= 0) {
                if (getTile(row-1, col).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            if ((row-1) >= 0 && (col+1) < COL_SIZE) {
                if (getTile(row-1, col+1).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            // SAME ROW
            if ((col-1) >= 0) {
                if (getTile(row, col-1).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            if ((col+1) < COL_SIZE) {
                if (getTile(row, col+1).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            // BOTTOM ROW
            if ((row+1) < ROW_SIZE && (col-1) >= 0) {
                if (getTile(row+1, col-1).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            if ((row+1) < ROW_SIZE) {
                if (getTile(row+1, col).hasBomb()) {
                    neighbourBombSize++;
                }
            }

            if ((row+1) < ROW_SIZE && (col+1) < COL_SIZE) {
                if (getTile(row+1, col+1).hasBomb()) {
                    neighbourBombSize++;
                }
            } 

            getTile(row, col).setNeighbourBombSize(neighbourBombSize);
        }
    }

我们将不胜感激,谢谢。

最佳答案

警告:我以你的代码为起点,但你的索引从1开始,但在java数组中索引从0开始,所以它可能不起作用。

我会做类似的事情:

int neighbourBombSize = 0;
// Compute currentCell row / col
int currentCellCol = ((currentCellIndex - 1) % COL_SIZE) + 1;
int currentCellRow = ((currentCellIndex - 1) / COL_SIZE) + 1;
System.out.println("Neighbors of " + currentCellIndex + " (" + currentCellRow + ", " + currentCellCol + ")");
for (int x = -1; x <= 1; x++) {
    for (int y = -1; y <= 1; y++) {
        if (x == 0 && y == 0) {
            continue; // Current cell index
        }
        int neighborCol = currentCellCol + y;
        int neighborRow = currentCellRow + x;
        if (neighborCol > 0 && neighborRow > 0 && neighborCol <= COL_SIZE && neighborRow <= ROW_SIZE ) {
            int computedNeighborIndex = neighborCol + ((neighborRow - 1) * COL_SIZE);
            if (getTile(neighborRow , neighborCol ).hasBomb()) {
                neighbourBombSize++;
            }
        }
    }
}

您可以在此处查看正在运行的示例(计算所有情况下的邻居索引):Running example

关于java - 更快地找到邻居索引的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29788518/

相关文章:

java - 将列表复制到另一个列表(带或不带 'new')之间的区别 - JAVA

java - 如何在 Eclipse 中抑制 SQL Scrapbook "save resource"对话框

java - Android 中的颜色

iphone - GPS 坐标(以度为单位)来计算距离

java - 使用泛型类读取 Gson 中的匿名类数组

java - Android 中带有平方根的表达式计算器

database - 什么是检查一个数字是否存在于多个集合中而不搜索所有集合的好算法?

java - 对整数数组列表的数组列表进行排序

java - 如何从充气编辑文本中读取值并将其放入 ArrayList 中

在 C 中实现 round() 的简洁方法?