如题,采用的是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); //死则显示白色}}}}}

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

  1. 软件工程基础 实验4《系统实现》

    实验4<系统实现> 一.实验目的 掌握:系统实现的有关技术及其相关工具. 二.实验内容 试对图3所示的课程管理对象类图,采用Java在Eclipse下编码实现,并用JUnit框架对某Jav ...

  2. 元胞自动机CA+生命游戏代码

    1.元胞自动机 元胞自动机(Cellular Automaton,复数为Cellular Automata,简称CA,也有人译为细胞自动机.点格自动机.分子自动机或单元自动机).是一时间和空间都离散的 ...

  3. 生命游戏代码_生命游戏 the Game of Life

    引言 群居性昆虫是一个生命,鱼群.鸟群是一个生命,社会.城市是一个有机体,人类的语言是活的,人类的集体行为也是活的.这些复杂系统是如何设计出来的?世界上最著名的游戏之一,Game of Life生命游 ...

  4. java swing 代码_java swing编写gui生命游戏代码,新手上路

    项目描述 生命游戏其实是一个零玩家游戏,它包括一个二维矩形世界,这个世界中的每个方格居住着一个活着的或死了的细胞.一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量.如果相邻方格活着 ...

  5. 计算机系统基础实验1---基本工具的使用

    实验准备 安装debian,并完成相关配置 1)更换apt源 2)配置SSH 在virtualBox网络选项卡中设置端口转发,名称SSH,主机端口22,子系统端口22 3)安装aptitude并用其代 ...

  6. 软件工程基础 实验2《需求分析》

    实验2<需求分析> 一.实验目的 了解:软件项目需求分析的基本原理与方法: 掌握:用例建模方法.数据流建模方法和IDEF1X数据建模方法: 掌握:Visio/EA等工具绘制模型图. 二.实 ...

  7. c语言生命游戏代码大全,c++生命游戏源码

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdent ...

  8. [生命科学] 生物基础实验之DNA提取

    生物基础实验之DNA提取实验 基因组DNA的提取通常用于构建基因组文库.Southern杂交(包括RFLP)及PCR分离基因等.利用基因组DNA较长的特性,可以将其与细胞器或质粒等小分子DNA分离. ...

  9. [生命科学] 生物基础实验之PCR验证

    生物基础实验之PCR验证 文章目录 生物基础实验之PCR验证 实验步骤一 实验步骤二 实验步骤三 配胶 实验步骤四 电泳 实验步骤五 跑胶 实验步骤一 在离心管加入7.5μL Master Mix 溶 ...

最新文章

  1. 改变电子商务行业的5种人工智能趋势
  2. jQuery的祖先遍历
  3. Google SVN托管和使用学习笔记
  4. 【毕业答辩】论文答辩过不了?做好这几点,再也不用担心自己被“仍论文”
  5. vue 第八天 小结 作业模拟购物车
  6. 在spring boot中3分钟上手RPC框架Dubbo
  7. Hyperledger Fabric教程(2)-- byfn.sh分析-生成身份证书
  8. 分布式文件存储FastDFS之环境搭建篇
  9. 算法设计与分析(第2版)屈婉玲 刘田 张立昂 王捍贫编著 第四章课后习题答案
  10. spring boot shiro视频
  11. TongLINK/Q7.X 8.x查看队列情况命令
  12. Java服务器处理图片上传
  13. 解决Win10无法安装.Net Framework 3.5,错误代码0x800F081F
  14. 真相(truth)最可怕的敌人不是谎言(lie),而是神话(myth)
  15. 关于时区您了解多少呢?在中国我们使用的是哪个时区?
  16. 整理一下虚拟化与Linux的学习经历
  17. Subsurface Scatting 的简单模拟
  18. mall订单模块的业务学习
  19. Java开源生鲜电商平台-订单抽成模块的设计与架构(源码可下载)
  20. 如何在 Linux 和 Windows 之间共享文件?

热门文章

  1. 批量转换nii文件为nii.gz格式
  2. Redis笔记1-雪崩、击穿和穿透
  3. 金秘书为何那样百度云免费在线观看_迅雷下载
  4. viewport在pc端是否生效_viewport移动端适配
  5. 入门计算机知识,计算机基础知识入门
  6. android 编译 sdl,使用android ndk编译SDL2示例错误r14
  7. 数字测图成果处理——计算开挖量绘制剖面图
  8. 2d平移、缩放、旋转、倾斜、多属性值
  9. 淘淘商城第96讲——单点登录之用户登录
  10. TypeScript基本类型的了解