试图写康威';在Java中的生活游戏,一个小错误

试图写康威';在Java中的生活游戏,一个小错误,java,conways-game-of-life,Java,Conways Game Of Life,我明天有一个Java实验室,我刚写完康威的《生活游戏》的代码。然而,输出清楚地告诉我,我的代码中有一个错误,它似乎指向了我的getNeighborCount方法。我这么说是因为有些细胞是活的,它们有三个以上的邻居是活的,而实际上它们应该是死的。请看一下我的代码,如果有人发现任何错误,请告诉我 import java.util.Arrays; import java.util.Scanner; public class GameOfLifeBoard { private char[][] bo

我明天有一个Java实验室,我刚写完康威的《生活游戏》的代码。然而,输出清楚地告诉我,我的代码中有一个错误,它似乎指向了我的getNeighborCount方法。我这么说是因为有些细胞是活的,它们有三个以上的邻居是活的,而实际上它们应该是死的。请看一下我的代码,如果有人发现任何错误,请告诉我

import java.util.Arrays;
import java.util.Scanner;

public class GameOfLifeBoard {

private char[][] board = new char[50][50];
private char[][] nextBoard = new char[50][50];
public static final char LIVE = 'X';
public static final char DEAD = '^';


public GameOfLifeBoard(){
    for (int row = 0; row < 50; row++) {
        for (int col = 0; col < 50; col++) {
            board[row][col] = DEAD;
        }
    }
    for (int row = 0; row < 50; row++) {
        for (int col = 0; col < 50; col++) {
            nextBoard[row][col] = DEAD;
        }
    }



}



public void generateNextStep() {
    for (int i = 0; i < board.length; i++){
        for (int j = 0; j < board[0].length; j++){
            int live = getNeighborCount(i,j);
            if (board[i][j] == DEAD && live == 3){
                nextBoard[i][j] = LIVE;
            }
            else if (board[i][j] == LIVE && live <= 3 && live >= 2){
                nextBoard[i][j] = LIVE;
            }
            else{
                nextBoard[i][j] = DEAD;
            }
        }
    }
    board = nextBoard;
    printBoard();
}

public int getNeighborCount(int row, int col) {
    int count = 0;
    for (int r = row-1; r <= row+1; r++){
        for (int c = col-1; c <= col+1; c++){
            if (r >= 0 && r < board.length && c >= 0 && c < board[0].length && board[r][c] == LIVE){
                count++;
            }
        }
    }
    if (board[row][col] == LIVE){
        count--;
    }
    return count;
}


public void printBoard(){
    for (int row = 0; row < 50; row++){
        for (int col = 0; col < 50; col++){
            System.out.print(this.board[row][col]);
        }
        System.out.println(row);
    }
}


public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    GameOfLifeBoard newGame = new GameOfLifeBoard();
    newGame.board[24][12] = LIVE;
    newGame.board[24][13] = LIVE;
    newGame.board[24][14] = LIVE;
    newGame.board[24][15] = LIVE;
    newGame.board[24][16] = LIVE;
    newGame.board[24][17] = LIVE;
    newGame.board[24][18] = LIVE;
    newGame.board[24][19] = LIVE;
    newGame.board[24][20] = LIVE;
    newGame.board[24][21] = LIVE;
    newGame.printBoard();
    System.out.println("Welcome to The Game of Life. Please enter a 1 to run the game, and a 0 to end the game:");
    int input = keyboard.nextInt();
    while (input == 1){
        newGame.generateNextStep();
        input = keyboard.nextInt();
    }

}

}

您的问题是,在第一步之后,
board
nextBoard
都指向同一个对象

generateNextStep()

固定代码如下所示:

public void generateNextStep() {
    for (int i = 0; i < board.length; i++){
        for (int j = 0; j < board[0].length; j++){
            int live = getNeighborCount(i,j);
            if (board[i][j] == DEAD && live == 3){
                nextBoard[i][j] = LIVE;
            }
            else if (board[i][j] == LIVE && live <= 3 && live >= 2){
                nextBoard[i][j] = LIVE;
            }
            else{
                nextBoard[i][j] = DEAD;
            }
        }
    }
    board = nextBoard;
    nextBoard = new char[50][50];
    printBoard();
}
public void generateNextStep(){
对于(int i=0;i
假设您共享所获得错误的异常或堆栈跟踪。这可能会有所帮助。@RajithPemabandu正如问题中的OP所述,这是一个逻辑错误,而不是会产生堆栈跟踪的异常或错误。