试图在 Java 中编写康威的生命游戏,一个小错误

Trying to write Conway's Game Of Life in Java, one minor error

我有一个 Java 明天要交的实验,我刚写完康威的生命游戏代码。但是,输出清楚地告诉我我的代码有错误,它似乎指向我的 getNeighborCount 方法。我这样说是因为有些活着的细胞有超过 3 个 保持 存活的邻居,而实际上它们应该是死的。请查看我的代码,如果您发现任何错误,请告诉我!

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();
    }

}

}

输出:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^2
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^3
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^4
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^5
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^6
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^7
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^8
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^9
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^10
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^11
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^12
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^13
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^14
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^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^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^33
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^34
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^35
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^36
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^37
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^38
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^39
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^40
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^41
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^42
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^43
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^44
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^45
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^46
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^47
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^48
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^49

谢谢!

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

generateNextStep()中,将nextBoard赋值给board后,需要为nextBoard重构一个新的char[][]

固定代码如下所示:

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();
}