在康威生命游戏的二维数组中计算(共有8种可能的)邻居。

huangapple go评论62阅读模式
英文:

Counting (8 possible) neighbours in 2D array in conways Game Of Life

问题

我必须计算每个单元格周围有多少个“活着”的邻居(在这种情况下是字符:'O')。每个单元格有8个邻居。(这是康威的“生命游戏”)

“正如您可以注意到的,每个单元格都有八个邻居。我们将宇宙视为周期性的:边界单元格也有八个邻居。例如:来自“正常”单元格的邻居

如果单元格位于右边界,则其右(东)邻居是同一行中最左边的单元格。
如果单元格位于底边界,则其下(南)邻居是同一列中最顶部的单元格。
角落单元格使用两种解决方案。”单元格何时是边界以及何时是顶部角落

这些链接是有关如何在“异常”情况下检查单元格的可视化。

我在互联网上找到了这个代码:

for (int x = -1; x <= 1; x += 1) {
    for (int y = -1; y <= 1; y += 1) {
        int r = i + y;
        int c = j + x;
        if (r >= 0 && r < n && c >= 0 && c < n
                && !(y == 0 && x == 0)
                && currentUniverse[i][j] == 'O') {
            neighbours++;
        }

然而,这似乎不起作用...
我想不出一个整洁而且最重要的是聪明/方便/简短的代码片段来检查一个位置(比如说 currentUniverse[i][j])上的单元格有多少个活着的邻居...

有人有建议、提示或其他帮助吗?

英文:

I have to count how many "alive" (in this case a char: 'O') neigbours each single cell has. Every cell has 8 neighbours. (It is for "The Game Of Life" from Conway)

"As you can notice, each cell has eight neighbors. We consider the universe to be periodic: border cells also have eight neighbors. For example: Neighbours from a "normal" cell

If cell is right-border, its right (east) neighbor is leftmost cell in the same row.
If cell is bottom-border, its bottom (south) neighbor is topmost cell in the same column.
Corner cells use both solutions." When a cell is border and when a cell is a top corner

The links are visualizations to how to check the cells in cases of "exceptions".

I found this on the internet:

for (int x = -1; x &lt;= 1; x += 1) {
    for (int y = -1; y &lt;= 1; y += 1) {
        int r = i + y;
        int c = j + x;
        if (r &gt;= 0 &amp;&amp; r &lt; n &amp;&amp; c &gt;= 0 &amp;&amp; c &lt; n
                &amp;&amp; !(y == 0 &amp;&amp; x == 0)
                &amp;&amp; currentUniverse[i][j] == &#39;O&#39;) {
            neighbours++;
        }

However that did not seem to work...
I can not come up with a tidy and most of all smart/handy/short piece of code to check how many alive neighbours a cell at a position (let's say currentUniverse[i][j]) has...

Has anyone suggestions, tips or some other help?

答案1

得分: 0

给这个一个尝试。我使用 n 作为数组的大小(假设为正方形)。

int n = 4;
System.out.println();
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        int neighbours = 0;
        for (int x = -1; x <= 1; x += 1) {
            for (int y = -1; y <= 1; y += 1) {
                if (!(y == 0 && x == 0)) {
                    int r = i + y;
                    int c = j + x;
                    //normalize
                    if (r < 0) r = n - 1;
                    else if (r == n) r = 0;

                    if (c < 0) c = n - 1;
                    else if (c == n) c = 0;
                    if (currentUniverse[r][c] == 0)
                        neighbours++;
                }
            }
        }
        System.out.print("\t" + neighbours);
    }
    System.out.println();
}
英文:

Give this one a shot. I am using n as the size of the array (assumes in square).

int n = 4;
System.out.println();
for (int i = 0; i &lt; n; i++) {
    for (int j = 0; j &lt; n; j++) {
        int neighbours = 0;
        for (int x = -1; x &lt;= 1; x += 1) {
            for (int y = -1; y &lt;= 1; y += 1) {
                if (!(y == 0 &amp;&amp; x == 0)) {
                    int r = i + y;
                    int c = j + x;
                    //normalize
                    if (r &lt; 0) r = n - 1;
                    else if (r == n) r = 0;

                    if (c &lt; 0) c = n - 1;
                    else if (c == n) c = 0;
                    if (currentUniverse[r][c] == 0)
                        neighbours++;
                }
            }
        }
        System.out.print(&quot;\t&quot; + neighbours);
    }
    System.out.println();
}

huangapple
  • 本文由 发表于 2020年8月21日 01:31:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63510358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定