【发布时间】:2021-11-16 23:02:04
【问题描述】:
以下代码是康威在终端中的生命游戏。我想在这里做的是将当前步骤覆盖到下一步。我在 animateChanges() 方法中有一个临时二维数组。同样在主要方法中,我将 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();
}
}
【问题讨论】:
-
@HovercraftFullOfEels 我的意思是是的,但最后我想将步骤覆盖到单个板上的下一步中,而不是再次打印以查看更改。
-
如果你想操作控制台输出,你需要一个像
curses这样的控制台操作库。我不知道 Java 在本机使用什么来做到这一点,但System.out.println并不是为了修改现有的控制台文本而构建的。
标签: java