Java用下一步覆盖2D数组



下面的代码是Conway在终端中的生命游戏。我想在这里做的是将当前步骤改写为下一步。我在animateChanges((方法中有一个临时的2D数组。同样在主方法中,我将animateChanges((打印三次,这就是我想要更改的。基本上,我想覆盖电路板,而不是打印三次,因为在循环时视觉上很难看。

public class GameOfLifeTerminal {
private int WIDTH;
private int HEIGHT;
private int[][] board;
private int aliveNeighbours = 0;

public GameOfLifeTerminal(int width, int height) {
this.WIDTH = width;
this.HEIGHT = height;
this.board = new int[width][height];
}
/*Getters & Setters*/
public void setAlive(int x, int y) {
this.board[x][y] = 1;
} //true
public void setDead(int x, int y) {
this.board[x][y] = 0;
} // false, currently not being used
//get the rows in the y axis
public int getRows(){ return board.length; }
//get the columns in the x axis
public int getColumns(){ return board[0].length; }
/*Methods and functions*/
public void printBoard() { //prints the board
for (int y = 0; y < HEIGHT; y++) {
String line = "";
for (int x = 0; x < WIDTH; x++) {
if (this.board[x][y] == 0) {
line += "·";
} else {
line += "O";
}
}
System.out.println(line);
}
System.out.println();
}
public int countAliveNeighbours(int x, int y) {    //count the number of alive neighbours
int count = 0;
/*
for(int i = x-1; i < x+1; i++) {
for (int j = y - 1; i < y + 1; j++) {
if (getState(i, j) == 1) count++;
}
}
if(getState(x, y) == 1) count--;
*/
//checks each position one by one and calls the getState() method to be inside the bounds
count += getState(x - 1, y - 1);
count += getState(x, y - 1);
count += getState(x + 1, y - 1);
count += getState(x - 1, y);
count += getState(x + 1, y);
count += getState(x - 1, y + 1);
count += getState(x, y + 1);
count += getState(x + 1, y + 1);
//System.out.println(count);
return count;
}
public int getState(int x, int y) { //get state neighbours in case is out of bounds
if (x < 0 || x >= WIDTH) return 0;
if (y < 0 || y >= HEIGHT) return 0;
return this.board[x][y];
}
public void animateChanges() { //applies the rules and then prints the board with current state of the cells
//temporary board
int[][] newBoard = new int[WIDTH][HEIGHT];
for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) {
aliveNeighbours = countAliveNeighbours(x, y);
//rules
if (getState(x, y) == 1) { //if true
if (aliveNeighbours < 2) { // le ded
newBoard[x][y] = 0;
} else if (aliveNeighbours == 2 || aliveNeighbours == 3) { //alive
newBoard[x][y] = 1;
} else if (aliveNeighbours > 3) { // le ded
newBoard[x][y] = 0;
}
} else {
if (aliveNeighbours == 3) { //create new cells
newBoard[x][y] = 1;
}
}
}
}
this.board = newBoard;

//System.out.println("Alive cells " + aliveNeighbours);
printBoard();
}
//main method
public static void main(String[] args) {
GameOfLifeTerminal simulation = new GameOfLifeTerminal(8, 5);
simulation.setAlive(2, 2);
simulation.setAlive(3, 2);
simulation.setAlive(4, 2);
simulation.animateChanges();
simulation.animateChanges();
simulation.animateChanges();
}
}

你看起来已经是"重写";您的animateChanges()方法内的板使用此调用:this.board = newBoard;为什么不简单地从相同的animateChanges()方法中删除对printBoard();的调用?然后,您可以在何时以及如何进行呼叫printBoard()

最新更新