我不知道如果我转向疯狂,但是这是我的问题是什么:
我PROGRAMM生命游戏,并具有计数周边许多领域是如何活着“countalive”的方法。
public int countalive(Board board, int yaxis, int xaxis) { //defekt
int living = board.getfields()[yaxis - 1][xaxis - 1] + board.getfields()[yaxis - 1][xaxis] + board.getfields()[yaxis - 1][xaxis + 1] +
board.getfields()[yaxis][xaxis - 1] + board.getfields()[yaxis][xaxis + 1] + board.getfields()[yaxis + 1][xaxis - 1] +
board.getfields()[yaxis + 1][xaxis] + board.getfields()[yaxis + 1][xaxis + 1];
return living;
}
这种方法似乎是完全正常工作。 但是,当我这样做
public Board evolve(Board board) {
Board tmpboard = board;
System.out.println(countalive(board, 1, 3)); //I test with this. SHOULD AND IS 2!!
int aliveneighbours = 0;
for (int i = 1; i < board.getfields().length - 1; i++) {
for (int j = 1; j < board.getfields()[i].length - 1; j++) {
System.out.print("i = " +i);
System.out.print("j = " +j +" ");
aliveneighbours = countalive(board, i, j);
System.out.println(aliveneighbours);
if (aliveneighbours == 3) {
tmpboard.getfields()[i][j] = 1;
} else if (aliveneighbours < 2 || aliveneighbours > 3) {
tmpboard.getfields()[i][j] = 0;
}
}
System.out.println("");
}
return tmpboard;
}
我得到这个在控制台:
2
i = 1j = 1 1
i = 1j = 2 1
i = 1j = 3 1
i = 1j = 4 1
i = 1j = 5 0
...
即使我= 1和j = 3应该是2,而不是1。正如你所看到的方法countalive(板,1,3)工作,但在for循环它给了我不同的结果。 你能找到我的错误?