目前能够计算总邻居,我只需要活着的邻居

发布于 2025-01-28 16:34:38 字数 1538 浏览 2 评论 0原文

我有一个布尔[,]板,该木板存储,如果细胞还活着或死亡。我提供的代码正确地告诉我每个单元格的总邻居,问题是我只需要计算活着的邻居。我的想法是对这些

   if (WithinBounds (row, col) ) {
      if (board[row, col] == true)
         liveNeighbor++;

是我的两种相关方法来计算总邻居。

        private int TotalLiveNeighbors(int x, int y)
        {
            int liveNeighbors = 0;


            for (int row = x - 1; row <= x + 1; row++)
            {
                for (int col = y - 1; col <= y + 1; col++)
                {
                    if (! (row == x && col == y))
                    {
                        if (WithinBounds(row, col)) {
                            liveNeighbors++;
                        }

                    }
                }
            }

            return liveNeighbors;
        }

        public bool WithinBounds(int x, int y)
        {
            if (x < 0 || y < 0)
                return false;
            if (x >= 3 || y >= 3)
                return false;
            
            return true;
        }

我还在研究一种使用偏移的不同方法:

int[,] neighborLocations = { { -1,-1 }, { -1,0 }, { -1, +1 },
                             { 0, -1},            { 0, +1 },
                             {+1, -1},  {+1, 0 }, { +1, +1 } };

int countAliveNeighbors (int x, int y) {
    int count = 0;
    foreach (int[] offset in neighborLocations) {
        if (board.hasAliveNeighborsAt(x + offset[1], y + offset[0])
            count++;
    }
    return count;
}

不确定这是否比我的第一种方法更有效

I have a bool [,] board that stores if the cell is alive or dead. The code I have provided is correctly telling me the total neighbors for each cell, the problem is I need to only count the alive neighbors. My thought was to have a check in the

   if (WithinBounds (row, col) ) {
      if (board[row, col] == true)
         liveNeighbor++;

These are my two relevant methods for calculating total neighbors.

        private int TotalLiveNeighbors(int x, int y)
        {
            int liveNeighbors = 0;


            for (int row = x - 1; row <= x + 1; row++)
            {
                for (int col = y - 1; col <= y + 1; col++)
                {
                    if (! (row == x && col == y))
                    {
                        if (WithinBounds(row, col)) {
                            liveNeighbors++;
                        }

                    }
                }
            }

            return liveNeighbors;
        }

        public bool WithinBounds(int x, int y)
        {
            if (x < 0 || y < 0)
                return false;
            if (x >= 3 || y >= 3)
                return false;
            
            return true;
        }

I was also working on a different approach that used an offset as so:

int[,] neighborLocations = { { -1,-1 }, { -1,0 }, { -1, +1 },
                             { 0, -1},            { 0, +1 },
                             {+1, -1},  {+1, 0 }, { +1, +1 } };

int countAliveNeighbors (int x, int y) {
    int count = 0;
    foreach (int[] offset in neighborLocations) {
        if (board.hasAliveNeighborsAt(x + offset[1], y + offset[0])
            count++;
    }
    return count;
}

Not really sure if this would be more efficient than my first approach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2025-02-04 16:34:38

假设阵列称为“活着”

 if (alive[row,col] && WithinBounds(row, col)) 

assuming that the array is called 'alive'

 if (alive[row,col] && WithinBounds(row, col)) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文