【问题标题】:Java overwrite 2D array with the next step [duplicate]Java用下一步覆盖二维数组[重复]
【发布时间】: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


【解决方案1】:

你似乎已经用这个调用“覆盖”了你的animateChanges() 方法中的板:this.board = newBoard; 为什么不简单地从同一个animateChanges() 方法中删除对printBoard(); 的调用?然后您可以在何时以及如何操作时致电printBoard()

【讨论】:

  • 是的,我想打印它,但是当我再次打印它时,我想用当前打印覆盖最后一个打印。
  • @noobieCoder:那么您可能想要创建该程序的 GUI 版本,并在 GUI 中显示更改。标准 Java 控制台不允许删除文本。
  • 是的,我做了一个更高级的 GUI 版本,但我是独立做这个版本的,这就是为什么我在板打印时询问是否覆盖。这不行吗? Runtime.getRuntime().exec("cls");
  • @noobieCoder:好吧,当你尝试这个时会发生什么?
  • @noobieCoder cls 是 Windows 命令,但对于 Linux / Unix / MacOS 它将是 clear 命令。您可以搜索一个 JCurses 库,它可以让您清除 Windows 和 Linux 的屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多