0

所以我正在嘗試構建生活程序遊戲,並且我對Java /編碼一般都很新鮮,而且我的問題正圍繞着環繞我的頭部在二維數組中。我有一個構造函數和方法,將構建一個數組,並將「單元格」放在我想要的位置,但我不明白我如何能夠看到一個單元格有多少個鄰居。在2d數組(生命遊戲)中計數鄰居

總結一下:

我可以做任何類型的二維數組。

我可以把「細胞」,在不同的元素數組中現在

我怎麼看到的是空間旁邊我被檢查電池在所有側鄰近(我用一個嵌套循環來遍歷每個細胞)?

繼續努力!環繞在這裏是有效的。

更新:這是我的,但是當我測試它時,它會返回比應該有的更少的鄰居。 更新2:我刪除了第一條if語句,因爲我認爲它沒有意義。但現在我不能讓C至上浮1

public int neighborCount(int row, int col) { 
    int count = 0; 
    for (int r = 0; r < society.length; r++) { 
     for (int c = 0; c < society[0].length; c++) { 
       // up and left 
       if ((society[(r - 1 + row) % row][(c - 1 + col) % col]) == cell) { 
        count++; 
       } 
       // up 
       if ((society[(r - 1 + row) % row][c]) == cell) { 
        count++; 
       } 
       // up and right 
       if ((society[(r - 1 + row) % row][(c + 1 + col) % col]) == cell) { 
        count++; 
       } 
       // left 
       if ((society[r][(c - 1 + col) % col]) == cell) { 
        count++; 
       } 
       // right 
       if ((society[r][(c + 1 + col) % col]) == cell) { 
        count++; 
       } 
       // down and left 
       if ((society[(r + 1 + row) % row][(c - 1 + col) % col]) == cell) { 
        count++; 
       } 
       // down 
       if ((society[(r + 1 + row) % row][c]) == cell) { 
        count++; 
       } 
       // down and right 
       if ((society[(r + 1 + row) % row][(c + 1 + col) % col]) == cell) { 
        count++; 
       } 
     } 
    } 
    return count; 
} 

我的測試:

@Test 
public void testNeighborsWrapping() { 
    GameOfLife society = new GameOfLife(10, 16); 
    society.growCellAt(3, 3); 
    society.growCellAt(3, 4); 
    society.growCellAt(3, 5); 
    assertEquals(0, society.neighborCount(2, 1)); 
    assertEquals(1, society.neighborCount(2, 2)); 
    assertEquals(2, society.neighborCount(2, 3)); 
    assertEquals(3, society.neighborCount(2, 4)); 

} 

}

+0

爲什麼不直接手動檢查每個單元? –

+0

好的編號喜歡讓我的方法爲我做,所以我可以說,去吧。 – Tejas

+0

在您的方法中,您手動檢查每個(周圍)單元格。 –

回答

1

如果我理解正確的問題,代碼可能是這樣的

Object[] getNeighbors(int i, int j) { 
    // put code to return the neighbors given an index 
} 

boolean allNeighborsFull(int i, int j) { 
    Object[] neighbors = getNeighbors(i, j); 
    boolean allFull = true; 
    for (Object neighbor : neighbors) { 
     if (!neighbor.full()) { 
      allFull = false; 
      break; 
     } 
    } 
    return allFull; 
} 

boolean allNeighborsSurrounded() { 
    Object[] neighbors = getNeighbors(i, j); 
    // check each one of these using the method above 
} 
+0

我有點看你的方法getneighbors如何工作 – Tejas

+0

哦,就是這一部分。那麼,一個簡單的方法就是對鄰居進行硬編碼。只要檢查一下,如果你正在訪問i-1,那麼i> 0。如果你正在訪問i + 1,那麼我

+0

我不確定我是否真的關注 – Tejas

0

這將工作:

public Cell[] getNeighbours(int i, int j) { 
    int i2 = i - 1; 
    int i3 = i + 1; 
    int j2 = j - 1; 
    int j3 = j + 1; 
    if (i2 == -1) 
     i2 = board.length - 1; 
    if (i3 == board.length) 
     i3 = 0; 
    if (j2 == -1) 
     j2 = board[i].length - 1; 
    if (j3 == board[i].length) 
     j3 = 0; 
    return new Cell[] { 
     board[i2][j2], board[i2][j], board[i2][j3], 
     board[i][j2], board[i][j3], board[i3][j2], 
     board[i3][j], board[i3][j3] 
    }; 
}