【软件工程基础实验】使用CheckStyle工具对生命游戏代码进行代码审查和修改

本文介绍了一个基于SunChecks规范的生命游戏GUI实现,通过Cell类封装细胞状态和行为,使用JFrame和JButton创建可视化界面,实现了细胞状态更新、随机初始化、清零等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题,采用的是Sun Checks规范,
最终将代码修改如下:

增加了package-info.java文件

Cell.java

package lifegame;

/**Cell为细胞类,以实现数据封装和实现与细胞有关的方法.*/
public class Cell {
    /**长度.*/
    private int maxLength;
    /**宽度.*/
    private int maxWidth;
    /**当前代数.*/
    private int nowGeneration;
    /**一个数据代表一个细胞,0代表死,1代表活.*/
    private int[][] grid;

    /**
     * Cell类的构造函数.
     * @param maxLen 形参:长度
     * @param maxWid 形参:宽度
     * */
    public Cell(final int maxLen, final int maxWid) {
        maxLength = maxLen;
        maxWidth = maxWid;
        nowGeneration = 0;
        grid = new int[maxLen + 2][maxWid + 2];
        for (int i = 0; i <= maxLen + 1; i++) {
            for (int j = 0; j <= maxWid + 1; j++) {
                grid[i][j] = 0;
            }
        }
    }

    /**
     * 实现数据封装.
     * @param g 形参:细胞
     */
    public void setGrid(final int[][] g) {
        grid = g;
    }

    /**
     * 实现数据封装.
     * @return 细胞
     */
    public int[][] getGrid() {
        return grid;
    }

    /**
     * 实现数据封装.
     * @param nowGen 形参:当前代数
     */
    public void setNowGeneration(final int nowGen) {
        nowGeneration = nowGen;
    }

    /**
     * 实现数据封装.
     * @return 当前代数
     */
    public int getNowGeneration() {
        return nowGeneration;
    }

    /**随机初始化细胞.*/
    public void randomCell() {
        final double p = 0.5; //细胞随机初始化为活的概率
        for (int i = 1; i <= maxLength; i++) {
            for (int j = 1; j <= maxWidth; j++) {
                grid[i][j] = Math.random() > p ? 1 : 0;
            }
        }
    }

    /**细胞清零.*/
    public void deleteAllCell() {
        for (int i = 1; i <= maxLength; i++) {
            for (int j = 1; j <= maxWidth; j++) {
                grid[i][j] = 0;
            }
        }
    }

    /**繁衍.*/
    public void update() {
        int[][] newGrid = new int[maxLength + 2][maxWidth + 2];
        for (int i = 1; i <= maxLength; i++) {
            final int x = 3; //细胞为活的邻居数量
            for (int j = 1; j <= maxWidth; j++) {
                switch (getNeighborCount(i, j)) {
                    case 2:
                        newGrid[i][j] = grid[i][j]; //细胞状态保持不变
                        break;
                    case x:
                        newGrid[i][j] = 1; // 细胞置为活
                        break;
                    default:
                        newGrid[i][j] = 0; // 细胞置为死
                }
            }
        }
        for (int i = 1; i <= maxLength; i++) {
            for (int j = 1; j <= maxWidth; j++) {
                grid[i][j] = newGrid[i][j];
            }
        }
        nowGeneration++;
    }

    /**
     * 获取细胞的邻居数量.
     * @return 细胞邻居数量
     * @param i1  行
     * @param j1  列
     */
    private int getNeighborCount(final int i1, final int j1) {
        int count = 0;
        for (int i = i1 - 1; i <= i1 + 1; i++) {
            for (int j = j1 - 1; j <= j1 + 1; j++) {
                count += grid[i][j]; //如果邻居还活着,邻居数便会+1
            }
        }
        count -= grid[i1][j1]; //每个细胞不是自己的邻居,则如果细胞还活着,邻居数-1.
        return count;
    }
}

GUI.java

package lifegame;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;

/**GUI为细胞类,以调用细胞类的方法和实现生命游戏过程的可视化.*/
public class GUI extends JFrame implements ActionListener {
    /**界面.*/
    private static GUI frame;
    /**细胞.*/
    private Cell cell;
    /**长和宽.*/
    private int maxLength, maxWidth;
    /**一个按钮表示一个细胞.*/
    private JButton[][] nGrid;
    /**按钮是否被选中.*/
    private boolean[][] isSelected;
    /**确定,当前代数,代数清零.*/
    private JButton ok, jbNowGeneration, randomInit, clearGeneration;
    /**下一代,开始繁衍,暂停,退出.*/
    private JButton clearCell, nextGeneration, start, stop, exit;
    /**长宽选择.*/
    private JComboBox lengthList, widthList;
    /**线程.*/
    private Thread thread;
    /**线程是否在运行.*/
    private boolean isRunning;
    /**细胞是否死亡.*/
    private boolean isDead;

    /**
     * 程序入口.
     * @param args 接收命令行参数
     */
    public static void main(final String[] args) {
        frame = new GUI("生命游戏");
    }

    /**
     * 实现数据封装.
     * @return 宽度
     */
    public int getMaxWidth() {
        return maxWidth;
    }

    /**
     * 实现数据封装.
     * @param maxWid 宽度
     */
    public void setMaxWidth(final int maxWid) {
        this.maxWidth = maxWid;
    }

    /**
     * 实现数据封装.
     * @return 长度
     */
    public int getMaxLength() {
        return maxLength;
    }

    /**
     * 实现数据封装.
     * @param maxLen 宽度
     */
    public void setMaxLength(final int maxLen) {
        this.maxLength = maxLen;
    }

    /**初始化界面.*/
    public void initGUI() {
        final int wid = 20; //界面默认的宽度
        final int len = 35; //界面默认的长度
        if (maxWidth == 0) {
            maxWidth = wid;
        }
        if (maxLength == 0) {
            maxLength = len;
        }

        cell = new Cell(maxWidth, maxLength);

        JPanel backPanel, centerPanel, bottomPanel;
        JLabel jWidth, jLength, jNowGeneration;
        backPanel = new JPanel(new BorderLayout());
        centerPanel = new JPanel(new GridLayout(maxWidth, maxLength));
        bottomPanel = new JPanel();
        this.setContentPane(backPanel);
        backPanel.add(centerPanel, "Center");
        backPanel.add(bottomPanel, "South");

        nGrid = new JButton[maxWidth][maxLength];
        isSelected = new boolean[maxWidth][maxLength];
        for (int i = 0; i < maxWidth; i++) {
            for (int j = 0; j < maxLength; j++) {
                nGrid[i][j] = new JButton(""); //按钮内容置空以表示细胞
                nGrid[i][j].setBackground(Color.WHITE); //初始时所有细胞均为死
                centerPanel.add(nGrid[i][j]);
            }
        }
        final int minNum = 3; //长宽最小值
        final int maxNum = 100; //长宽最大值
        jLength = new JLabel("长度:");
        lengthList = new JComboBox();
        for (int i = minNum; i <= maxNum; i++) {
            lengthList.addItem(String.valueOf(i));
        }
        lengthList.setSelectedIndex(maxLength - minNum);

        jWidth = new JLabel("宽度:");
        widthList = new JComboBox();
        for (int i = minNum; i <= maxNum; i++) {
            widthList.addItem(String.valueOf(i));
        }
        widthList.setSelectedIndex(maxWidth - minNum);

        ok = new JButton("确定");
        jNowGeneration = new JLabel(" 当前代数:");
        //button按钮不能直接添加int,故采用此方式
        jbNowGeneration = new JButton("" + cell.getNowGeneration());
        jbNowGeneration.setEnabled(false);
        clearGeneration = new JButton("代数清零");
        randomInit = new JButton("随机初始化");
        clearCell = new JButton("细胞清零");
        start = new JButton("开始繁衍");
        nextGeneration = new JButton("下一代");
        stop = new JButton("暂停");
        exit = new JButton("退出");

        bottomPanel.add(jLength);
        bottomPanel.add(lengthList);
        bottomPanel.add(jWidth);
        bottomPanel.add(widthList);
        bottomPanel.add(ok);
        bottomPanel.add(jNowGeneration);
        bottomPanel.add(jbNowGeneration);
        bottomPanel.add(clearGeneration);
        bottomPanel.add(randomInit);
        bottomPanel.add(clearCell);
        bottomPanel.add(start);
        bottomPanel.add(nextGeneration);
        bottomPanel.add(stop);
        bottomPanel.add(exit);


        // 设置窗口
        final int length = 1000; //界面长度
        final int height = 650; //界面宽度
        this.setSize(length, height);
        this.setResizable(true);
        this.setLocationRelativeTo(null); // 让窗口在屏幕居中
        this.setVisible(true); // 将窗口设置为可见的

        // 注册监听器
        this.addWindowListener(new WindowAdapter() {
            public void windowClosed(final WindowEvent e) {
                System.exit(0);
            }
        });
        ok.addActionListener(this);
        clearGeneration.addActionListener(this);
        randomInit.addActionListener(this);
        clearCell.addActionListener(this);
        nextGeneration.addActionListener(this);
        start.addActionListener(this);
        stop.addActionListener(this);
        exit.addActionListener(this);
        for (int i = 0; i < maxWidth; i++) {
            for (int j = 0; j < maxLength; j++) {
                nGrid[i][j].addActionListener(this);
            }
        }
    }

    /**
     * 新建界面.
     * @param name  界面标题
     */
    public GUI(final String name) {
        super(name);
        initGUI();
    }

    /**
     * 接收操作事件.
     * @param e 操作事件
     */
    public void actionPerformed(final ActionEvent e) {
        final int minNum = 3; //长宽最小值
        final int sleeptime = 500; //线程睡眠的时间数
        if (e.getSource() == ok) { //确定
            frame.setMaxLength(lengthList.getSelectedIndex() + minNum);
            frame.setMaxWidth(widthList.getSelectedIndex() + minNum);
            initGUI();

            cell = new Cell(getMaxWidth(), getMaxLength());

        } else if (e.getSource() == clearGeneration) { //代数清零
            cell.setNowGeneration(0);
            jbNowGeneration.setText("" + cell.getNowGeneration()); //刷新当前代数
            isRunning = false;
            thread = null;
        } else if (e.getSource() == randomInit) { //随机初始化
            cell.randomCell();
            showCell();
            isRunning = false;
            thread = null;
        } else if (e.getSource() == clearCell) { //细胞清零
            cell.deleteAllCell();
            showCell();
            isRunning = false;
            thread = null;
        } else if (e.getSource() == start) { //开始
            isRunning = true;
            thread = new Thread(new Runnable() {
                public void run() {
                    while (isRunning) {
                        makeNextGeneration();
                        try {
                            Thread.sleep(sleeptime);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        isDead = true;
                        for (int row = 1; row <= maxWidth; row++) {
                            for (int col = 1; col <= maxLength; col++) {
                                if (cell.getGrid()[row][col] != 0) {
                                    isDead = false;
                                    break;
                                }
                            }
                            if (!isDead) {
                                break;
                            }
                        }
                        if (isDead) {
                            JOptionPane.showMessageDialog(null, "所有细胞已死亡");
                            isRunning = false;
                            thread = null;
                        }
                    }
                }
            });
            thread.start();
        } else if (e.getSource() == nextGeneration) { //下一代
            makeNextGeneration();
            isRunning = false;
            thread = null;
        } else if (e.getSource() == stop) { //暂停
            isRunning = false;
            thread = null;
        } else if (e.getSource() == exit) { //退出
            frame.dispose();
            System.exit(0);
        } else {
            int[][] grid = cell.getGrid();
            for (int i = 0; i < maxWidth; i++) {
                for (int j = 0; j < maxLength; j++) {
                    if (e.getSource() == nGrid[i][j]) {
                        isSelected[i][j] = !isSelected[i][j];
                        if (isSelected[i][j]) {
                            nGrid[i][j].setBackground(Color.BLACK);
                            grid[i + 1][j + 1] = 1;
                        } else {
                            nGrid[i][j].setBackground(Color.WHITE);
                            grid[i + 1][j + 1] = 0;
                        }
                        break;
                    }
                }
            }
            cell.setGrid(grid);
        }
    }

    /**下一代.*/
    private void makeNextGeneration() {
        cell.update();
        showCell();
        jbNowGeneration.setText("" + cell.getNowGeneration()); //刷新当前代数
    }

    /**将细胞加载到界面上.*/
    public void showCell() {
        int[][] grid = cell.getGrid();
        for (int i = 0; i < maxWidth; i++) {
            for (int j = 0; j < maxLength; j++) {
                if (grid[i + 1][j + 1] == 1) {
                    nGrid[i][j].setBackground(Color.BLACK); //活显示黑色
                } else {
                    nGrid[i][j].setBackground(Color.WHITE); //死则显示白色
                }
            }
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值