简体   繁体   English

生活邻居问题的康威游戏(java)

[英]conway game of life neighbor issue(java)

I'm currrently having issues with the neighbors in my project where i have to modify two diffrent methods for a conway game of life.我目前在我的项目中遇到邻居的问题,我必须修改两种不同的方法来进行康威生活游戏。 My TA says my code looks like it should work but the neightbor count isn't working.我的助教说我的代码看起来应该可以工作,但邻居计数不起作用。 I'v been printing the neightbor code and it works for the first time than it just goes to 0 for the rest of the run.我一直在打印 neightbor 代码,它第一次工作,而不是在剩下的运行中变为 0。 Anyone have an idea where I am messing up?任何人都知道我在哪里搞砸了?

public static void updateLife(Boolean[][] gameCellAlive) {
int size = gameCellAlive.length;
System.out.println("size of temp--->"+size);
Boolean[][] tempCell = new Boolean [size][size];
int row = 0;
int col = 0;
for (row = 0; row<tempCell.length; row++) {
for(col=0; col<tempCell[0].length; col++) {
tempCell[row][col] = gameCellAlive[row][col];
}
}   
for (int i = 0; i<tempCell.length; i++) {
for (int j = 0; j<tempCell[0].length; j++) {
int tempInt = getLifeNeighborCount(gameCellAlive, j, i);
System.out.println("neighbors---->"+tempInt);
if  ((tempInt>3) || (tempInt<2)) {
tempCell[i][j] = false;
}
else if(tempInt == 3) {
tempCell[i][j] = true;
}
else if(tempInt==2) {
tempCell[i][j]=true;
}
/*else {
tempCell[row][col]=gameCellAlive[row][col];
}*/

}//2nd for loop
}//for loop

for (int x = 0; x<tempCell.length; x++) {
for(int y=0; y<tempCell[0].length; y++) {
gameCellAlive[x][y] = tempCell[x][y];
}
}

  // METHOD STUB - This method needs to be implemented!
//if statemeent for requirements.
} // end method updateLife

/**
 *
 * @param gameBoard A 2D boolean array containing the current life status of
 * each cell at each x,y coordinate on the board. true indicates that the
 * cell is alive. false indicates no life in that cell.
 * @param colIndex The x position of the cell in the game board whose
 * neighbors are to be counted.
 * @param rowIndex The y position of the cell in the game board whose
 * neighbors are to be counted.
 * @return the number of cells adjacent to the cell at the specified row and
 * column that contain life. This value ranges between 0 (no adjacent cells
 * contain life) and 8 (all adjacent cells contain life).
 *
 * CS1180 Note: YOU NEED TO IMPLEMENT THIS METHOD
 */
public static int getLifeNeighborCount(Boolean[][] gameBoard, int colIndex, int rowIndex) {
    // METHOD STUB - THIS METHOD NEEDS TO BE IMPLEMENTED
    int neighborCount = 0;

    //check for alive or dead
    for (int i = rowIndex-1; i<=rowIndex+1; i++) {
    for (int j = colIndex-1; j<=rowIndex+1; j++) {
    try {
    if (gameBoard[i][j]==true  && (i !=rowIndex || j!=colIndex)) {
    //System.out.println("hello");
    neighborCount++;
    }//end if
    }//end try

    catch (ArrayIndexOutOfBoundsException e){         
    }//end catch
    }//end second foor loop
    }//end first foor loop
    return neighborCount;
    }// end method getLifeNeighborCount

You are using the wrong variable in the condition in this loop:您在此循环的条件中使用了错误的变量:

for (int j = colIndex-1; j<=rowIndex+1; j++) {

It should be:它应该是:

for (int j = colIndex-1; j<=colIndex+1; j++) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM