You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java终端康威生命游戏如何覆盖2D数组替换多次打印操作

康威生命游戏终端实现修改方案

核心问题说明

你现有代码的animateChanges方法本身已经实现了直接覆盖原棋盘状态的逻辑:this.board = newBoard这行就是将计算好的下一状态直接替换原board变量,状态覆盖功能不需要改动,你遇到的多次打印、视觉效果差的问题是因为把打印逻辑和状态更新逻辑绑定在了一起。

修改思路

  • 拆分状态更新与打印逻辑:移除animateChanges方法最后的printBoard()调用,让该方法仅负责计算并更新棋盘状态
  • 按需控制打印时机:可以选择迭代指定步数后仅打印最终结果,也可以配合清屏、延时实现流畅的连续动画效果

修改后的完整代码

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;
    }
    public void setDead(int x, int y) {
        this.board[x][y] = 0;
    }
    public int getRows(){ return board.length; }
    public int getColumns(){ return board[0].length; }

    /*Methods and functions*/
    public void printBoard() {
        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) {
        int count = 0;
        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);
        return count;
    }

    public int getState(int x, int y) {
        if (x < 0 || x >= WIDTH) return 0;
        if (y < 0 || y >= HEIGHT) return 0;
        return this.board[x][y];
    }

    // 仅更新状态,不打印
    public void animateChanges() {
        int[][] newBoard = new int[WIDTH][HEIGHT];
        for (int y = 0; y < HEIGHT; y++) {
            for (int x = 0; x < WIDTH; x++) {
                aliveNeighbours = countAliveNeighbours(x, y);
                if (getState(x, y) == 1) {
                    if (aliveNeighbours < 2) {
                        newBoard[x][y] = 0;
                    } else if (aliveNeighbours == 2 || aliveNeighbours == 3) {
                        newBoard[x][y] = 1;
                    } else if (aliveNeighbours > 3) {
                        newBoard[x][y] = 0;
                    }
                } else {
                    if (aliveNeighbours == 3) {
                        newBoard[x][y] = 1;
                    }
                }
            }
        }
        this.board = newBoard;
    }
    
    // 场景1:仅迭代3次,打印最终结果
    public static void main(String[] args) {
        GameOfLifeTerminal simulation = new GameOfLifeTerminal(8, 5);
        simulation.setAlive(2, 2);
        simulation.setAlive(3, 2);
        simulation.setAlive(4, 2);

        // 迭代3次,无中间打印
        for(int i = 0; i < 3; i++){
            simulation.animateChanges();
        }
        // 仅打印最终状态
        simulation.printBoard();
    }

    /* 场景2:流畅动画效果,替换上面的main方法即可
    public static void main(String[] args) throws InterruptedException {
        GameOfLifeTerminal simulation = new GameOfLifeTerminal(8, 5);
        simulation.setAlive(2, 2);
        simulation.setAlive(3, 2);
        simulation.setAlive(4, 2);

        // 连续迭代10次的动画
        for(int i = 0; i < 10; i++){
            // 清屏指令,Windows系统可替换为 new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
            System.out.print("\033[H\033[2J");
            System.out.flush();
            simulation.printBoard();
            simulation.animateChanges();
            // 每帧间隔500ms,可自行调整速度
            Thread.sleep(500);
        }
    }
    */
}

内容的提问来源于stack exchange,提问作者noobieCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 10:06:03