【软件工程基础实验】使用CheckStyle工具对生命游戏代码进行代码审查和修改
生活随笔
收集整理的這篇文章主要介紹了
【软件工程基础实验】使用CheckStyle工具对生命游戏代码进行代码审查和修改
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
如題,采用的是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工具对生命游戏代码进行代码审查和修改的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js 如何将汉字转换成拼音
- 下一篇: 由方波产生正弦波