[英]Counting neighbors with wrap around in 2d array(game of life)
So I'm trying to build the game of life program and I am fairly fresh to java/coding in general and I am having issues wrapping my head around wrap around in 2D arrays. 所以我正在尝试构建生命游戏程序,而且我对java /编码一般都很新鲜,而且我在绕着2D数组环绕时遇到了问题。 I have a constructor and methods that will build me an array and place "cells" where I want them but I dont understand how I can see how many neighbors a cell has. 我有一个构造函数和方法,将构建一个数组并将“单元格”放在我想要的位置,但我不明白我怎么能看到一个单元格有多少个邻居。
To sum it up: 把它们加起来:
I can make a 2D array of whatever type. 我可以制作任何类型的2D数组。
I can place "cells" at different elements in the array 我可以将“单元格”放在数组中的不同元素上
Now how do I see is the spaces next to my cell being checked has neighbors on all side( I use a nested for loop to go through each cell)? 现在我怎么看我被检查的单元格旁边的空格是否有邻居(我使用嵌套的for循环来遍历每个单元格)?
KEEP IN MIND! 记住! Wrap around is in effect here. 环绕在这里生效。
UPDATE: This is what I have but when I test it it returns 1 less neighbor than there should be. 更新:这就是我所拥有的,但是当我测试它时,它会返回比应该存在的少1个邻居。 UPDATE 2: I removed the first if statement because I don't think it makes sense with it. 更新2:我删除了第一个if语句,因为我认为它没有意义。 But now I cant get c to go up 1. 但现在我不能上升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;
}
My test: 我的测试:
@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));
}
} }
If I understand the problem correctly, the code could be something like 如果我正确理解问题,代码可能是这样的
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
}
This would work: 这可行:
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]
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.