Java实验三

(一)敏捷开发与XP:

XP准则:

1.沟通 :XP认为项目成员之间的沟通是项目成功的关键,并把沟通看作项目中间协调与合作的主要推动因素。

2.简单 :XP假定未来不能可靠地预测,在现在考虑它从经济上是不明智的,所以不应该过多考虑未来的问题而是应该集中力量解决燃眉之急。

3.反馈 :XP认为系统本身及其代码是报告系统开发进度和状态的可靠依据。系统开发状态的反馈可以作为一种确定系统开发进度和决定系统下一步开发方向的手段。

4.勇气:代表了XP认为人是软件开发中最重要的一个方面的观点。在一个软件产品的开发中人的参与贯穿其整个生命周期,是人的勇气来排除困境,让团队把局部的最优抛之脑后,达到更重大的目标。表明了XP对“人让项目取得成功”的基本信任态度。

XP软件开发的活动:编码、测试、倾听、设计。其中的编码标准,结对编程,代码集体所有,测试,重构等实践过程较为重要。

(二)编码标准:

编程标准包含:具有说明性的名字、清晰的表达式、直截了当的控制流、可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性。

 

Java中的一般的命名规则有:

(1)要体现各自的含义

(2)包、类、变量用名词

(3)方法名用动宾

(4)包名全部小写,如:io,awt

(5)类名第一个字母要大写,如:HelloWorldApp

(6)变量名第一个字母要小写,如:userName

(7)方法名第一个字母要小写:setName

标识符名字应当直观且可以拼读,可望文知意,不必进行“解码”,一般采用英文单词或其组合,便于记忆和阅读,切忌使用汉语拼音来命名,用词要准确

标识符的长度“min-length && max-information”的原则,比如:maxVal 比 maxValueUntilOverflow要好些,可以通过去元音法把变量名变短,如returnValue->rtnVal ,message->msg;一般全局变量用具有说明性的名字,局部变量用短名字:单字符的名字,常见的如i,j,k等用作局部变量。

(三)结对编程:

驾驶员:写设计文档,进行编码和单元测试等XP开发流程。

领航员:审阅驾驶员的文档、驾驶员对编码等开发流程的执行;考虑单元测试的覆盖率;思考是否需要和如何重构;帮助驾驶员解决具体的技术问题。

驾驶员和领航员不断轮换角色,不要连续工作超过一小时,每工作一小时休息15分钟。领航员要控制时间。

主动参与。任何一个任务都首先是两个人的责任,也是所有人的责任。没有“我的代码”、“你的代码”或“他/她的代码”,只有“我们的代码”。

只有水平上的差距,没有级别上的差异。两人结对,尽管可能大家的级别资历不同,但不管在分析、设计或编码上,双方都拥有平等的决策权利。

团队精神是好多地方都强调的一个精神,最小的团队就是一对一的二人团队了,培养团队精神从结对编程开始吧。社会生活中人与人相处最重要的是诚信,有同理心,互利。结对编程中大家会出现分歧,如何更有效地合作要做到对事不对人

(四)版本控制:

    我的代码库连接::http://git.shiyanlou.com/fdj20135311

(五)重构:

      重构(Refactor),就是在不改变软件外部行为的基础上,改变软件内部的结构,使其更加易于阅读、易于维护和易于变更。

一个完整的重构流程包括:

1.   从版本控制系统代码库中Check out code

2.   读懂代码(包括测试代码)

3.   发现bad smell

4.   Refactoring

5.   运行所有的Unit Tests

6.   往代码库中Check in code

 

(六)实践项目:

结对伙伴:陈巧然20135310

博客地址:http://www.cnblogs.com/20135310cqr/

此次实验中,我们结对的游戏项目是JKJ连连看。

1.代码库地址:

http://git.shiyanlou.com/dky20135310/shiyanlou_cs212/src/master/lianliankan/lianliankan.java

 

2.游戏界面如下:

3.源代码

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

public class lianliankan implements ActionListener {

      JFrame mainFrame; // 主面板

      Container thisContainer;

      JPanel centerPanel, southPanel, northPanel; // 子面板

      JButton diamondsButton[][] = new JButton[6][5];// 游戏按钮数组

      JButton exitButton, resetButton, newlyButton; // 退出,重列,重新开始按钮

      JLabel fractionLable = new JLabel("0"); // 分数标签

      JButton firstButton, secondButton; // 分别记录两次被选中的按钮

      int grid[][] = new int[8][7];// 储存游戏按钮位置

      static boolean pressInformation = false; // 判断是否有按钮被选中

      int x0 = 0, y0 = 0, x = 0, y = 0, fristMsg = 0, secondMsg = 0, validateLV; // 游戏按钮的位置坐标

      int i, j, k, n;// 消除方法控制

 

      public void init() {

           mainFrame = new JFrame("JKJ连连看");

           thisContainer = mainFrame.getContentPane();

           thisContainer.setLayout(new BorderLayout());

           centerPanel = new JPanel();

           southPanel = new JPanel();

           northPanel = new JPanel();

           thisContainer.add(centerPanel, "Center");

           thisContainer.add(southPanel, "South");

           thisContainer.add(northPanel, "North");

           centerPanel.setLayout(new GridLayout(6, 5));

           for (int cols = 0; cols < 6; cols++) {

                 for (int rows = 0; rows < 5; rows++) {

                      diamondsButton[cols][rows] = new JButton(

                                  String.valueOf(grid[cols + 1][rows + 1]));

                      diamondsButton[cols][rows].addActionListener(this);

                      centerPanel.add(diamondsButton[cols][rows]);

                 }

           }

           exitButton = new JButton("退出");

           exitButton.addActionListener(this);

           resetButton = new JButton("重列");

           resetButton.addActionListener(this);

           newlyButton = new JButton("再来一局");

           newlyButton.addActionListener(this);

           southPanel.add(exitButton);

           southPanel.add(resetButton);

           southPanel.add(newlyButton);

           fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable

                      .getText())));

           northPanel.add(fractionLable);

           mainFrame.setBounds(280, 100, 500, 450);

           mainFrame.setVisible(true);

      }

 

      public void randomBuild() {

           int randoms, cols, rows;

           for (int twins = 1; twins <= 15; twins++) {

                 randoms = (int) (Math.random() * 25 + 1);

                 for (int alike = 1; alike <= 2; alike++) {

                      cols = (int) (Math.random() * 6 + 1);

                      rows = (int) (Math.random() * 5 + 1);

                      while (grid[cols][rows] != 0) {

                            cols = (int) (Math.random() * 6 + 1);

                            rows = (int) (Math.random() * 5 + 1);

                      }

                      this.grid[cols][rows] = randoms;

                 }

           }

      }

 

      public void fraction() {

           fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable

                      .getText()) + 100));

      }

 

      public void reload() {

           int save[] = new int[30];

           int n = 0, cols, rows;

           int grid[][] = new int[8][7];

           for (int i = 0; i <= 6; i++) {

                 for (int j = 0; j <= 5; j++) {

                      if (this.grid[i][j] != 0) {

                            save[n] = this.grid[i][j];

                            n++;

                      }

                 }

           }

           n = n - 1;

           this.grid = grid;

           while (n >= 0) {

                 cols = (int) (Math.random() * 6 + 1);

                 rows = (int) (Math.random() * 5 + 1);

                 while (grid[cols][rows] != 0) {

                      cols = (int) (Math.random() * 6 + 1);

                      rows = (int) (Math.random() * 5 + 1);

                 }

                 this.grid[cols][rows] = save[n];

                 n--;

           }

           mainFrame.setVisible(false);

           pressInformation = false; // 这里一定要将按钮点击信息归为初始

           init();

           for (int i = 0; i < 6; i++) {

                 for (int j = 0; j < 5; j++) {

                      if (grid[i + 1][j + 1] == 0)

                            diamondsButton[i][j].setVisible(false);

                 }

           }

      }

 

      public void estimateEven(int placeX, int placeY, JButton bz) {

           if (pressInformation == false) {

                 x = placeX;

                 y = placeY;

                 secondMsg = grid[x][y];

                 secondButton = bz;

                 pressInformation = true;

           } else {

                 x0 = x;

                 y0 = y;

                 fristMsg = secondMsg;

                 firstButton = secondButton;

                 x = placeX;

                 y = placeY;

                 secondMsg = grid[x][y];

                 secondButton = bz;

                 if (fristMsg == secondMsg && secondButton != firstButton) {

                      xiao();

                 }

           }

      }

 

      public void xiao() { // 相同的情况下能不能消去。仔细分析,不一条条注释

           if ((x0 == x && (y0 == y + 1 || y0 == y - 1))

                      || ((x0 == x + 1 || x0 == x - 1) && (y0 == y))) { // 判断是否相邻

                 remove();

           } else {

                 for (j = 0; j < 7; j++) {

                      if (grid[x0][j] == 0) { // 判断第一个按钮同行哪个按钮为空

                            if (y > j) { // 如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边

                                  for (i = y - 1; i >= j; i--) { // 判断第二按钮左侧直到第一按钮中间有没有按钮

                                       if (grid[x][i] != 0) {

                                             k = 0;

                                             break;

                                       } else {

                                             k = 1;

                                       } // K=1说明通过了第一次验证

                                  }

                                  if (k == 1) {

                                       linePassOne();

                                  }

                            }

                            if (y < j) { // 如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边

                                  for (i = y + 1; i <= j; i++) { // 判断第二按钮左侧直到第一按钮中间有没有按钮

                                       if (grid[x][i] != 0) {

                                             k = 0;

                                             break;

                                       } else {

                                             k = 1;

                                       }

                                  }

                                  if (k == 1) {

                                       linePassOne();

                                  }

                            }

                            if (y == j) {

                                  linePassOne();

                            }

                      }

                      if (k == 2) {

                            if (x0 == x) {

                                  remove();

                            }

                            if (x0 < x) {

                                  for (n = x0; n <= x - 1; n++) {

                                       if (grid[n][j] != 0) {

                                             k = 0;

                                             break;

                                       }

                                       if (grid[n][j] == 0 && n == x - 1) {

                                             remove();

                                       }

                                  }

                            }

                            if (x0 > x) {

                                  for (n = x0; n >= x + 1; n--) {

                                       if (grid[n][j] != 0) {

                                             k = 0;

                                             break;

                                       }

                                       if (grid[n][j] == 0 && n == x + 1) {

                                             remove();

                                       }

                                 }

                            }

                      }

                 }

                 for (i = 0; i < 8; i++) { // 列

                      if (grid[i][y0] == 0) {

                            if (x > i) {

                                  for (j = x - 1; j >= i; j--) {

                                       if (grid[j][y] != 0) {

                                             k = 0;

                                             break;

                                       } else {

                                             k = 1;

                                       }

                                  }

                                  if (k == 1) {

                                       rowPassOne();

                                  }

                            }

                            if (x < i) {

                                  for (j = x + 1; j <= i; j++) {

                                       if (grid[j][y] != 0) {

                                             k = 0;

                                             break;

                                       } else {

                                             k = 1;

                                       }

                                  }

                                  if (k == 1) {

                                       rowPassOne();

                                 }

                            }

                            if (x == i) {

                                  rowPassOne();

                            }

                      }

                      if (k == 2) {

                            if (y0 == y) {

                                  remove();

                            }

                            if (y0 < y) {

                                  for (n = y0; n <= y - 1; n++) {

                                       if (grid[i][n] != 0) {

                                             k = 0;

                                             break;

                                       }

                                        if (grid[i][n] == 0 && n == y - 1) {

                                             remove();

                                       }

                                  }

                            }

                            if (y0 > y) {

                                  for (n = y0; n >= y + 1; n--) {

                                       if (grid[i][n] != 0) {

                                             k = 0;

                                             break;

                                       }

                                       if (grid[i][n] == 0 && n == y + 1) {

                                             remove();

                                       }

                                  }

                            }

                      }

                 }

           }

      }

 

      public void linePassOne() {

           if (y0 > j) { // 第一按钮同行空按钮在左边

                 for (i = y0 - 1; i >= j; i--) { // 判断第一按钮同左侧空按钮之间有没按钮

                      if (grid[x0][i] != 0) {

                            k = 0;

                            break;

                      } else {

                            k = 2;

                      } // K=2说明通过了第二次验证

                 }

           }

           if (y0 < j) { // 第一按钮同行空按钮在与第二按钮之间

                 for (i = y0 + 1; i <= j; i++) {

                      if (grid[x0][i] != 0) {

                            k = 0;

                            break;

                      } else {

                            k = 2;

                      }

                 }

           }

      }

 

      public void rowPassOne() {

           if (x0 > i) {

                 for (j = x0 - 1; j >= i; j--) {

                      if (grid[j][y0] != 0) {

                            k = 0;

                            break;

                      } else {

                            k = 2;

                      }

                 }

           }

           if (x0 < i) {

                 for (j = x0 + 1; j <= i; j++) {

                      if (grid[j][y0] != 0) {

                            k = 0;

                            break;

                      } else {

                            k = 2;

                      }

                 }

           }

      }

 

      public void remove() {

           firstButton.setVisible(false);

           secondButton.setVisible(false);

           fraction();

           pressInformation = false;

           k = 0;

           grid[x0][y0] = 0;

           grid[x][y] = 0;

      }

 

      public void actionPerformed(ActionEvent e) {

           if (e.getSource() == newlyButton) {

                 int grid[][] = new int[8][7];

                 this.grid = grid;

                 randomBuild();

                 mainFrame.setVisible(false);

                 pressInformation = false;

                 init();

           }

           if (e.getSource() == exitButton)

                 System.exit(0);

           if (e.getSource() == resetButton)

                 reload();

           for (int cols = 0; cols < 6; cols++) {

                 for (int rows = 0; rows < 5; rows++) {

                      if (e.getSource() == diamondsButton[cols][rows])

                            estimateEven(cols + 1, rows + 1, diamondsButton[cols][rows]);

                 }

           }

      }

 

      public static void main(String[] args) {

           lianliankan llk = new lianliankan();

           llk.randomBuild();

           llk.init();

      }

}

 

4.总结与体会

PSP时间:

     

步骤

耗时

百分比

需求分析

40

11%

设计

50

14%

代码实现

180

50%

测试

60

17%

分析总结

30

8%

 

体会:通过结对项目,我认识到了合作的重要性,紧密的合作能够提高我们的能力。代码测试过程中出现很多错误,但经过互相的合作和探讨,加以改进,便成功运行

通过本次实验,学会了如何使用github来管理代码。如果是开源的项目,通过网站托管方式进行统一管理,当然是非常棒的,并且有很多功能,如果不能开源,仅搭建内部Git服务器进行配置管理,团队管理部分的功能就需要自己开发,相比其他配置工具,在这方面没有太大的优势,当然在各大开源网站上已经有大量这方面的实现共享出来。

同时通过结对项目,我认识到了合作的重要性,紧密的合作能够提高我们的能力。代码测试过程中出现很多错误,但经过互相的合作和探讨,加以改进,便成功运行。

posted @ 2015-06-04 17:53  20135311不是富东京  阅读(254)  评论(0编辑  收藏  举报