出于某种原因,“numAlive”变量显示为 3,但当传递回 numAlivePass 时,它显示为“0”这是康威人生游戏
根据下面的评论编辑
public int countLiveNeighbour(int x, int y){
int numAlive = 0;
if(x >= 3 && y >= 3 && x < HEIGHT-3 && y < WIDTH-3)
{
if(generation[x][y+1] == ALIVE) {numAlive++;}
if(generation[x][y-1] == ALIVE) {numAlive++;}
if(generation[x+1][y] == ALIVE) {numAlive++;}
if(generation[x+1][y+1] == ALIVE) {numAlive++;}
if(generation[x+1][y-1] == ALIVE) {numAlive++;}
if(generation[x-1][y-1] == ALIVE) {numAlive++;}
if(generation[x-1][y+1] == ALIVE) {numAlive++;}
}
if(x ==31 && y==16){
System.out.println("ALIVE INclass: " + numAlive);
}
return numAlive;
}
//cell will be alive in next generation if alive cells = 3 or if already alive then 2 or 3
private int aliveCell(int x, int y){
int numAlivePass = 0;
numAlivePass = countLiveNeighbour(x, y);
if(x ==31 && y==16){
System.out.println(" NumAlivePass(ed) = " +numAlivePass+ " Live Neighbour Call = " + countLiveNeighbour(x, y));
}
int aliveState= 0;
if(x ==31 && y==16){
System.out.println(x + " x val 1, " + y + " x val 1, ALIVE 1: " + numAlivePass + " Alive State 1: " + aliveState);
}
if (numAlivePass > 3 || numAlivePass < 2){
aliveState = DEAD;
}
if(numAlivePass == 3){
aliveState = ALIVE;
System.out.println("Alive");
}
if (generation[x][y] == ALIVE && numAlivePass == 2){
aliveState = ALIVE;
System.out.println("Alive");
}
return aliveState;
}
这是这个类中的所有代码,包括上面的代码: package game_of_life;
import java.awt.Color;
import java.awt.Graphics;
public class CellularAnimation implements Animatable{
public static final int ALIVE = -1;
public static final int DEAD = 0;
public static final int HEIGHT = 100;
public static final int WIDTH = 100;
public static final int CELL_SIZE = 5;
int generation[][] = new int[HEIGHT][WIDTH];
int nextGeneration[][] = new int[HEIGHT][WIDTH];
public int getCellStatus(int x, int y) {
// TODO Auto-generated method stub
return generation[x][y];
}
public void populateNextGen(){
for(int x=0; x<=WIDTH-1; x++){
for(int y=0; y<=HEIGHT-1; y++){
nextGeneration[x][y] = aliveCell(x,y);
}
}
for(int x=0; x<=WIDTH-1; x++){
for(int y=0; y<=HEIGHT-1; y++){
generation[x][y] = nextGeneration[x][y];
}
}
}
public void initialise(){
for(int i = 0;i<HEIGHT-1;i++){
for(int z = 0;z<WIDTH-1;z++){
generation[z][i]= DEAD;
}
}
System.out.println("test1");
generation[30][15]= ALIVE;
generation[30][16]= ALIVE;
generation[30][17]= ALIVE;
}
public int countLiveNeighbour(int x, int y){
int numAlive = 0;
if(x >= 3 && y >= 3 && x < HEIGHT-3 && y < WIDTH-3)
{
if(generation[x][y++] == ALIVE) {numAlive++;}
if(generation[x][y--] == ALIVE) {numAlive++;}
if(generation[x++][y] == ALIVE) {numAlive++;}
if(generation[x++][y++] == ALIVE) {numAlive++;}
if(generation[x++][y--] == ALIVE) {numAlive++;}
if(generation[x--][y--] == ALIVE) {numAlive++;}
if(generation[x--][y++] == ALIVE) {numAlive++;}
}
if(x ==31 && y==16){
System.out.println("ALIVE INclass: " + numAlive);
}
return numAlive;
}
//cell will be alive in next generation if alive cells = 3 or if already alive then 2 or 3
private int aliveCell(int x, int y){
int numAlivePass = 0;
numAlivePass = countLiveNeighbour(x, y);
if(x ==31 && y==16){
System.out.println(" NumAlivePass(ed) = " +numAlivePass+ " Live Neighbour Call = " + countLiveNeighbour(x, y));
}
int aliveState= 0;
if(x ==31 && y==16){
System.out.println(x + " x val 1, " + y + " x val 1, ALIVE 1: " + numAlivePass + " Alive State 1: " + aliveState);
}
if (numAlivePass > 3 || numAlivePass < 2){
aliveState = DEAD;
}
if(numAlivePass == 3){
aliveState = ALIVE;
System.out.println("Alive");
}
if (generation[x][y] == ALIVE && numAlivePass == 2){
aliveState = ALIVE;
System.out.println("Alive");
}
return aliveState;
}
@Override
public void step() {
populateNextGen();
//paint(null);
//implement paint ect here
}
@Override
public void paint(Graphics pGraphics) {
//super.paint(pGraphics);
for(int i = 0; i<HEIGHT; i++){
for(int z = 0; z<WIDTH; z++){
if(generation[z][i] == ALIVE){
pGraphics.setColor(Color.BLACK);
System.out.println("test");
}
else{
pGraphics.setColor(Color.WHITE);
}
pGraphics.fillRect(z*CELL_SIZE,i*CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
}
}
@Override
public int getWidth() {
// TODO Auto-generated method stub
return WIDTH*CELL_SIZE;
}
@Override
public int getHeight() {
// TODO Auto-generated method stub
return HEIGHT*CELL_SIZE;
}
}