所以我以 GUI 形式编程康威生命游戏。输出不正确,我不知道为什么。这是处理“下一代”的代码。
for(int i=0; i < ROW; i++) {
for(int j=0; j < COL; j++) {
if(i > 0 && i < ROW-1 && j > 0 && j < COL -1) {
if(grid.getButton(i-1, j-1).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i-1, j).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i-1, j+1).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i, j-1).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i, j+1).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i+1, j-1).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i+1, j).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i+1, j+1).getBackground() == Color.BLUE) liveNeighbor++;
if(grid.getButton(i, j).getBackground() == Color.WHITE) {
if(liveNeighbor == 3)
newGrid.getButton(i, j).setBackground(Color.BLUE);
} else {
if(liveNeighbor > 3 || liveNeighbor < 2)
newGrid.getButton(i, j).setBackground(Color.WHITE);
}
liveNeighbor=0;
}
}
}
也许我错过了一些东西,但我很确定这是正确的。有什么建议么?