java实验三

京电子科技学院(BESTI)

             

课程:Java程序设计  班级:1352               姓名:王国伊      学号:20135207

成绩:                 指导教师:娄嘉鹏         实验日期:2015.5.8

实验密级:无           预习程度:                 实验时间:15:30~18:00

仪器组次:07          必修/选修:选修            实验序号:03

实验名称:   敏捷开发与XP实践                                                                      

实验目的与要求: 1. 没有Linux基础的同学建议先学习《Linux基础入门(新版)》《Vim编辑器》课程;                                    

2. 完成实验、撰写实验报告,实验报告以博客方式发表在博客园,注意实验报告重点是运行结果,遇到的问题(工具查找,安装,使用,

程序的编辑,调试,运行等)、解决办法(空洞的方法如查网络问同学看书等一律得0分)以及分析(从中可以得到什么启示,

有什么收获,教训等)。报告可以参考范飞龙老师的指导                                                                                                

3. 严禁抄袭,有该行为者实验成绩归零,并附加其他惩罚措施。                                                                                         

实验仪器:

名称

型号

数量

PC机

Lenovo   Z485

1

Linux虚拟机

Ubuntu(32bit)

1

 

实验步骤

一、敏捷开发与XP

1.软件工程

(1)概念:软件工程是把系统的、有序的、可量化的方法应用到软件的开发、运营和维护上的过程。

(2)包括:软件需求分析、软件设计、软件构建、软件测试和软件维护。

(3)公式:软件工程=开发流程+工具。

软件=程序+软件工程,软件企业=软件+商业模式。

(4)常见的开发流程:RUP(Rational Unified Process)

PSP(Personal Software Process )

TSP(Team Software Process )

Agile Process(重点介绍)

2、敏捷开发(Agile Development)

(1)概念:是一种以人为核心、迭代、循序渐进的开发方法。“敏捷流程”是一系列价值观和方法论的集合。

(2)模式:XP,TDD,CI,FDD,BDD,SCRUM,Feature Team,Domain Model,Extract Activities,Refactor,Pair Programming,Feature List/Product Backlog.

(3)极限编程(eXtreme Programming,XP) 是一种全新而快捷的软件开发方法。

(4)XP准则:沟通、简单、反馈、勇气。

(5)XP活动:编码、测试、倾听、设计。

 

二、编码标准

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

2、Eclipse中的快捷键使用(实用性重点掌握)

(1).Ctrl+Shift+F或菜单中的source->Format,使代码按Eclipse规定的规范缩进。                       

3、Java中的一般的命名规则:

(1)要体现各自的含义;

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

(3)方法名用动宾;

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

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

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

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

4、标识符

一般采用英文单词或其组合,切忌使用汉语拼音来命名,可以通过去元音法把变量名缩短。

 

三、结对编程

1、分为:驾驶员(Driver)是控制键盘输入的人。

领航员(Navigator)起到领航、提醒的作用。

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

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

 

四、版本控制

1、版本控制提供项目级的 undo(撤销) 功能。

2、版本控制允许多人在同一代码上工作, 只要遵守一定的控制原则就行。

3、版本控制系统保存了过去所作的修改的历史记录。

4、版本控制系统还支持在主线上开发的同时发布多个软件版本。

5、版本控制也是项目级的时间机器,你可以选择任何一个时间, 精确地查看项目在当时的情况。

6、流行的版本控制工具有CVS,SVN,Git等。

7、实验楼中的git服务:

(1)公开的代码仓库:shiyanlou_cs212

(2)启动实验时,会自动执行git pull,获取课程仓库中的最新代码,存放于/home/shiyanlou/Code目录。

(3)进入到实验环境中修改代码,完成后需要依次执行下述命令即可提交:

$ cd /home/shiyanlou/Code/shiyanlou_cs212

# 修改代码文件

# 添加修改文件

$ git add 所有修改的文件

# 提交到环境中本地代码仓库

$ git commit -m '本次修改的描述'

# push到git.shiyanlou.com,无需输入密码

$ git push

例:

 

(4)克隆其他用户代码仓库只需要知道对方的仓库链接,我们鼓励在别人代码基础上修改:

$ git clone http://git.shiyanlou.com/[对方的专属用户名]/[课程代码仓库名]

(5)输入git status:查看代码状态;

     出现未跟踪的文件,使用git add <file>...:以包含要提交的内容;

     使用git commit:git将依据索引库中的内容来进行文件的提交;

     使用git push:把代码保存到远程托管服务器中。

 

五、重构(Refactor)

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

2、功能:

(1)Rename

可以给类、包、方法、变量改名字。

修改方法:用鼠标单击要改的名字,选择Eclipse中菜单中的Refactor->Rename

例如图所示

(2)封装

修改方法:Refactor->Encapsulate Field...

例:

 

选择需要封装的变量s.name,点击Refactor->Encapsulate Field。并以相同的方法封装age和id。

使用Eclipse中Source->Generate toString(),给Student类产生一个toString方法。

 

4、修改代码的目的:

增加新功能,原有功能有BUG,改善原有程序的结构,优化原有系统的性能。

代码的坏味道及重构方法:

 

5、有Bad Smell的代码需要重构:

重复代码(Duplicated Code),过长方法,过长参数列,条件逻辑过度复杂,分支语句,基本类型迷恋,数据泥团。

6、Eclipse中Refactor菜单中的重构手法的应用时机如下图所示:

 

7、重构的完整流程:

(1)从版本控制系统代码库中Check out code;

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

(3)发现bad smell;

(4)Refactoring;

(5)运行所有的Unit Tests;

(6)往代码库中Check in code;

 

六、实践项目

结对编写一个软件(程序功能从豌豆荚游戏中选择一款用Java实现):黑白棋。

结对同学:20135115 臧文君

Blog网址:www.cnblogs.com/CatherineZang

1、实验分工:臧文君---测试代码,Chess、ChessListener代码的重构。

             王国伊---代码分析,GUI界面的实现。

2、实验步骤:

(1)先查找黑白棋的游戏规则,分析代码需要实现的内容:

a.下棋子时相应的棋子颜色改变;

b.统计黑白双方现有棋子的个数以及每下一步改变棋子的个数;

c.分析棋手还有几种下发。

(2)在网上搜索相关代码,克隆下来。

(3)分析代码中语句的功能,根据TDD思想编写测试代码。

(4)根据测试结果,由臧文君负责代码中变量名称、方法的重构,由王国伊负责GUI界面的实现。

(5)将两个人的代码进行综合,测试结果。

3、黑白棋游戏代码:

package Chess;

import static org.junit.Assert.*;

import Chess.ChessListener;

import org.junit.Test;

 

public class ChessTest {

       @Test

       public void testNormal() {

              ChessListener a = new ChessListener(null, null, null, null, null);

              assertEquals(2,a.makeout());  //1代表黑方获胜,2代表白方获胜,0代表平局

       }

}

 

 

 

package Chess;

import java.awt.Dimension; 

import java.awt.Font; 

import java.awt.Graphics; 

import java.awt.GridLayout; 

import java.awt.Image; 

import java.awt.event.ActionEvent; 

import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 

import javax.swing.JButton; 

import javax.swing.JFrame; 

import javax.swing.JLabel; 

import javax.swing.JPanel; 

@SuppressWarnings("serial")

 

public class Chess extends JFrame { 

    public static void main(String args[]){ 

        Chess ch = new Chess(); 

        ch.showframe(); 

    }

   

    //显示游戏界面 

    public void showframe(){ 

        //--------------------游戏界面---------------------- 

        JPanel gamejp = new JPanel(); 

        //--------------------组件界面---------------------- 

        JPanel buttonjp = new JPanel(); 

        //按钮图标 

        ImageIcon blackchess = new ImageIcon("E:/JavaCode/Test/Test/image/黑子.jpg"); 

        ImageIcon whitechess = new ImageIcon("E:/JavaCode/Test/Test/image/白子.jpg"); 

        ImageIcon black = new ImageIcon("E:/JavaCode/Test/Test/image/黑子.jpg");

        ImageIcon white = new ImageIcon("E:/JavaCode/Test/Test/image/白子.jpg"); 

        JButton blackbutton = new JButton(blackchess); 

        JButton whitebutton = new JButton(whitechess); 

        blackbutton.setRolloverIcon(black); 

        whitebutton.setRolloverIcon(white); 

        blackbutton.setPressedIcon(black); 

        whitebutton.setPressedIcon(white); 

        Dimension di = new Dimension(100,100); 

        blackbutton.setPreferredSize(di); 

        whitebutton.setPreferredSize(di);

       

        //-----------------------当前下棋的人--------------------- 

        final JLabel record = new JLabel(" 黑方下  "); 

        //设置字体 

        Font recordfont = new Font("黑体",Font.BOLD,30); 

        record.setFont(recordfont); 

        //用来记录阿狸与桃子的数目 

        final JLabel blacknum = new JLabel("2");

        final JLabel whitenum = new JLabel("2"); 

        //设置Label的字体和大小 

        Font font = new Font("宋体",Font.BOLD,42); 

        blacknum.setFont(font); 

        whitenum.setFont(font);

       

        //-----------------重新开局的方法------------------ 

        ImageIcon img = new ImageIcon("E:/JavaCode/Test/Test/image/restart.jpg"); 

        JButton bu = new JButton(img); 

        bu.setPreferredSize(new Dimension(100,40)); 

        buttonjp.add(record); 

        buttonjp.add(blackbutton); 

        buttonjp.add(blacknum); 

        buttonjp.add(whitebutton); 

        buttonjp.add(whitenum); 

        buttonjp.add(bu); 

        this.setLayout(new GridLayout(1,2,600,0)); 

        this.add(gamejp); 

        this.add(buttonjp); 

        this.setTitle("黑白棋--双人对战"); 

        this.setSize(1000,650); 

        this.setResizable(false); 

        this.setLocationRelativeTo(null); 

        this.setDefaultCloseOperation(3);

       

        //显示窗口 

        this.setVisible(true);

        

        //获取画布权限 

        Graphics g = this.getGraphics(); 

        chess[3][3]=1; 

        chess[4][4]=1; 

        chess[3][4]=-1; 

        chess[4][3]=-1; 

        ChessListener listener = new ChessListener(g,this,record,blacknum,whitenum); 

        this.addMouseListener(listener);

       

        //按钮监听器----------------重新开始 

        ActionListener buttonlistener = new ActionListener(){ 

            public void actionPerformed(ActionEvent e){ 

                clear(); 

                //重绘的方法 

                repaint(); 

                //绘制初始的四个图片 

                chess[3][3]=1; 

                chess[4][4]=1; 

                chess[3][4]=-1; 

                chess[4][3]=-1; 

                //将状态改为初始状态 

                ChessListener.stata=1; 

                record.setText(" 黑方下  "); 

                blacknum.setText("2"); 

                whitenum.setText("2"); 

            } 

        };

       

        //如果点击黑方则黑方下 

        ActionListener blacklistener = new ActionListener(){ 

            public void actionPerformed(ActionEvent e){ 

                ChessListener.stata=1; 

                record.setText(" 黑方下 "); 

            } 

        }; 

        //如果点击白方则白方下 

        ActionListener whitelistener = new ActionListener(){ 

            public void actionPerformed(ActionEvent e){ 

                ChessListener.stata=-1; 

                record.setText(" 白方下  "); 

            } 

        };

       

        blackbutton.addActionListener(blacklistener); 

        whitebutton.addActionListener(whitelistener); 

        bu.addActionListener(buttonlistener); 

    } 

   

    public void clear(){ 

        for(int i=0;i<chess.length;i++){ 

            for(int j=0;j<chess[i].length;j++){ 

                chess[i][j]=0;  

            } 

        } 

    } 

 

    public void paint(Graphics g){ 

        super.paint(g); 

        Image back = new ImageIcon("E:/JavaCode/Test/Test/image/棋盘.jpg").getImage(); 

        g.drawImage(back, 10,30,600,600, null); 

 

        //绘制棋子 

        for(int i=0;i<rows-1;i++){ 

            for(int j=0;j<cols-1;j++){ 

                int X=x+size/2+size*i;//棋子的横坐标 

                int Y=y+size/2+size*j;//棋子的纵坐标 

                if(chess[i][j]==1){ 

                    //画黑棋 

                    Image b = new ImageIcon("E:/JavaCode/Test/Test/image/黑子.jpg").getImage(); 

                    g.drawImage(b, X-Chess_size/2, Y-Chess_size/2, Chess_size, Chess_size, null);  

                } 

                else if(chess[i][j]==-1){ 

                    //画白棋 

                    Image w = new ImageIcon("E:/JavaCode/Test/Test/image/白子.jpg").getImage(); 

                    g.drawImage(w, X-Chess_size/2, Y-Chess_size/2, Chess_size, Chess_size, null);

                } 

            } 

        } 

    }

   

    public final static int x=63;   //棋盘初始点横坐标 

    public final static int y=85;   //棋盘初始点纵坐标 

    public final static int rows=9; //棋盘行数 

    public final static int cols=9; //棋盘列数 

    public final static int size=61;//棋盘格子大小 

    public final static int Chess_size=60;//棋子大小 

    public final static int chess[][]=new int[rows-1][cols-1];//定义一个8*8的数组,用来保存棋子 

}

 

 

 

package Chess;

import java.awt.Graphics; 

import java.awt.event.MouseAdapter; 

import java.awt.event.MouseEvent; 

import javax.swing.JLabel; 

import javax.swing.JOptionPane; 

 

public class ChessListener extends MouseAdapter{ 

       private Graphics g;        //将画布对象传过来 

       private int skill;         //记录当前棋子的下法 

       public  static int stata=1;//判断下黑棋还是白棋

       private int x1,y1;         //记录点击处的横坐标和纵坐标 

       private JLabel record;       //当前下棋子的人 

       private JLabel blacknum;      //黑方棋子数 

       private JLabel whitenum;    //白方棋子数 

       private Chess ch;          //用来刷新棋盘 

       private int   special=0;        //特殊情况 

       private int   change;      //记录改变的棋子数目 

       public ChessListener(Graphics g,Chess ch,JLabel record,JLabel blacknum,JLabel whitenum){ 

              this.g=g; 

              this.ch=ch; 

              this.record=record; 

              this.blacknum=blacknum; 

              this.whitenum=whitenum; 

       } 

 

       //点击下棋子 

       public void mouseClicked(MouseEvent e){ 

              int a=1;            //记录所下的子 

              String s = null;        

              x1=e.getX(); 

              y1=e.getY();

              for(int i=0;i<Chess.rows-1;i++){  

                     for(int j=0;j<Chess.cols-1;j++){ 

                            int x2=Chess.x+Chess.size/2+Chess.size*i;//得到棋盘中棋子所在点的横坐标 

                            int y2=Chess.y+Chess.size/2+Chess.size*j;//得到棋盘中棋子所在点的纵坐标 

                            //将棋子下在棋盘格子的正中央 

                            if(Math.abs(x1-x2)<Chess.size/3&&Math.abs(y1-y2)<Chess.size/3){ 

                                   if(Chess.chess[i][j]==0){ 

                                          if(stata==1)  

                                                 stata=-1;

                                          else if(stata==-1)

                                                 stata=1;

                                          Chess.chess[i][j]=-stata;//将棋子存入棋盘 

                                         

                                          //--------------如果8个方向都不能改变棋子则不改变棋盘状态-------------- 

                                          if (hengYou(i, j) == null && hengZuo(i, j) == null && hengShang(i, j) == null && hengXia(i, j) == null && xieyouS(i, j) == null && xieyouX(i, j) == null && xiezuoS(i, j) == null && xiezuoX(i, j) == null) { 

                                                 Chess.chess[i][j]=0; 

                                                 if(stata==1){ 

                                                        stata=-1;  

                                                 } 

                                                 else if(stata==-1){ 

                                                        stata=1; 

                                                 } 

                                                 //刷新棋盘 

                                                 ch.update(g); 

                                                 JOptionPane.showMessageDialog(null, "不能放子"); 

                                          } 

                                          else{ 

                                                 // 改变中间子颜色 

                                                 hengYou1(i, j); 

                                                 hengZuo1(i, j); 

                                                 hengShang1(i, j); 

                                                 hengXia1(i, j); 

                                                 xieyou1(i, j); 

                                                 xieyou2(i, j); 

                                                 xiezuo1(i, j); 

                                                 xiezuo2(i, j); 

                                                 // 以坐标为圆心画圆  

                                                 ch.update(g); 

                                                 //改变棋子记录框里面的数字 

                                                 blacknum.setText(judge()[0]+""); 

                                                 whitenum.setText(judge()[1]+""); 

                                                 System.out.println("改变了"+change+"个子"); 

                                                 change=0; 

                                                 //判断当前下棋的人 

                                                 if(stata==1){ 

                                                        record.setText(" 黑方下  "); 

                                                        a=1; 

                                                        s="黑方"; 

                                                 } 

                                                 else if(stata==-1){ 

                                                        record.setText(" 白方下  "); 

                                                        a=-1; 

                                                        s="白方"; 

                                                 } 

                                                 skill=Check(a);

                                                

                                                 //输出当前下棋的人还有几种下法 

                                                 System.out.println(s+"有"+skill+"种下法"); 

                                                 if(skill==0&&full()==false){//如果不能下子并且棋盘未满 

                                                        special++; 

                                                        JOptionPane.showMessageDialog(null, s+"不能下子,跳过!"); 

                                                        if(stata==1){//如果黑方不能下子则跳过,白方下 

                                                               stata=-1; 

                                                               record.setText(" 白方下  "); 

                                                               a=-1; 

                                                               s="白方"; 

                                                        } 

                                                        else if(stata==-1){//如果白方不能下子则跳过,黑方下 

                                                               stata=1; 

                                                               record.setText(" 黑方下  "); 

                                                               a=1; 

                                                               s="黑方"; 

                                                        } 

                                                        skill=Check(a); 

                                                        System.out.println(s+"有"+skill+"种下法"); 

                                                        if(skill==0){ 

                                                               special++;//如果无子可下,则特殊情况记录加1 

                                                        }else

                                                               special=0; 

                                                 }

                                                

                                                 //----------------胜负判断---------------- 

                                                 makeout();

                                          } 

                                   }

                            } 

                     } 

              } 

       } 

      

       public int makeout() {

              if(judge()[0]==0){//如果黑方没子了则白方获胜 

                     JOptionPane.showMessageDialog(null,"游戏结束,白方获胜");

                     return 2;

              } 

              else if(judge()[1]==0){//如果白方没子了则黑方获胜 

                     JOptionPane.showMessageDialog(null, "游戏结束,黑方获胜");

                     return 1;

              } 

              if(full()){ 

                     if(judge()[0]>judge()[1]){//如果黑方的子较多,则黑方获胜 

                            JOptionPane.showMessageDialog(null, "游戏结束,黑方获胜");

                            return 1;

                     } 

                     else if(judge()[0]<judge()[1]){//如果白方的子较多,则白方获胜 

                            JOptionPane.showMessageDialog(null, "游戏结束,白方获胜");

                            return 2;

                     } 

                     else if(judge()[0]==judge()[1]){ 

                            JOptionPane.showMessageDialog(null, "平局");

                            return 0;

                     } 

              }

              return 3;

       }

      

       /* 判断输赢的方法,如果黑棋多则黑棋获胜,否则白棋获胜

          @return 黑棋和白棋的数目     */

       public int[] judge(){ 

              int count[]=new int[2]; 

              for(int i=0;i<Chess.rows-1;i++){ 

                     for(int j=0;j<Chess.cols-1;j++){ 

                            if(Chess.chess[i][j]==1){ 

                                   count[0]++; 

                            } 

                            else if(Chess.chess[i][j]==-1){ 

                                   count[1]++; 

                            } 

                     } 

              } 

              return count; 

       } 

 

       /* 判断棋盘是否已满的方法   */

       public boolean full(){ 

              if(special==2){      //如果双方都不能下子,则游戏结束 

                     return true; 

              } 

              else{ 

                     for(int i=0;i<Chess.rows-1;i++){ 

                            for(int j=0;j<Chess.cols-1;j++){  

                                   //如果有一个地方是空的则返回false 

                                   if(Chess.chess[i][j]==0){ 

                                          return false; 

                                   } 

                            } 

                     } 

              } 

              return true; 

       } 

 

       /*  判断当前棋子的下法还有多少种

           @param return 返回下法的总数     */

       public int Check(int a){ 

              int n=0; 

              for(int i=0;i<Chess.chess.length;i++){ 

                     for(int j=0;j<Chess.chess[i].length;j++){ 

                            if(Chess.chess[i][j]!=0){ 

                                   continue; 

                            } 

                            else{ 

                                   Chess.chess[i][j]=a; 

                                   if(hengYou(i, j) != null || hengZuo(i, j) != null || hengShang(i, j) != null || hengXia(i, j) != null || xieyouS(i, j)   != null || xieyouX(i, j) != null || xiezuoS(i, j)   != null || xiezuoX(i, j) != null){ 

                                          Chess.chess[i][j]=0; 

                                          n++;                //如果有一个地方可以下子,则n+1 

                                   } 

                                   else{ 

                                          Chess.chess[i][j]=0; 

                                   }

                            }

                     }

              }

              return n;

       }

 

       /*********************************************** 检测相同颜色 *******************************/

       /* 改变两棋子之间的棋子的颜色——直线   */

       public void paintChess(int r1, int c1, int r2, int c2) { 

              // 横向 

              if (c1 == c2) { 

                     for (int k = Math.min(r1, r2) + 1; k < Math.max(r1, r2); k++) { 

                            Chess.chess[k][c1] = Chess.chess[r1][c1]; 

                            change++; 

                     } 

              } 

              // 纵向 

              if (r1 == r2) { 

                     for (int k = Math.min(c1, c2) + 1; k < Math.max(c1, c2); k++) { 

                            Chess.chess[r1][k] = Chess.chess[r1][c1]; 

                            change++; 

                     } 

              } 

       } 

 

       /* 改变两棋子之间的棋子的颜色——斜线——右上    */

       public void paintChess1(int r1, int c1, int r2, int c2) { 

              for (int k = Math.min(r1, r2) + 1, v = Math.max(c1, c2) - 1; k < Math 

                            .max(r1, r2); k++, v--) { 

                     Chess.chess[k][v] = Chess.chess[r1][c1]; 

                     change++; 

              } 

       } 

 

       /* 改变两棋子之间的棋子的颜色——斜线——右下  */

       public void paintChess2(int r1, int c1, int r2, int c2) { 

              for (int k = Math.min(r1, r2) + 1, v = Math.min(c1, c2) + 1; k < Math 

                            .max(r1, r2); k++, v++) { 

                     Chess.chess[k][v] = Chess.chess[r1][c1]; 

                     change++; 

              } 

       } 

 

       /* 改变两棋子之间的棋子的颜色——斜线——左上    */

       public void paintChess3(int r1, int c1, int r2, int c2) { 

              for (int k = Math.max(r1, r2) - 1, v = Math.max(c1, c2) - 1; k > Math 

                            .min(r1, r2); k--, v--) { 

                     Chess.chess[k][v] = Chess.chess[r1][c1]; 

                     change++; 

              } 

       } 

 

       /* 改变两棋子之间的棋子的颜色——斜线——左下    */

       public void paintChess4(int r1, int c1, int r2, int c2) { 

              for (int k = Math.min(r1, r2) + 1, v = Math.max(c1, c2) - 1; k <= Math 

                            .max(r1, r2); k++, v--) { 

                     Chess.chess[k][v] = Chess.chess[r1][c1]; 

                     change++; 

              } 

       } 

 

       /* 向右检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL  */

       public int[] hengYou(int x, int y) { 

              int r = -2; 

              int i; 

              // 向右 

              for (i = x + 1; i < Chess.rows-1; i++) { 

                     if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1)

                            break;

                     if (Chess.chess[i][y] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x + 1][y] != Chess.chess[i][y]) {  

                     return new int[] { r, y }; 

              }

              else

                     return null;

       } 

 

       /* 向左检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL  */

       public int[] hengZuo(int x, int y) { 

              int r = -2; 

              int i; 

              // 向左 

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

                     if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1) { 

                            break; 

                     } 

                     if (Chess.chess[i][y] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x - 1][y] != Chess.chess[i][y]) { 

 

                     return new int[] { r, y }; 

              }

              else 

                     return null;

       } 

 

       /* 向上检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL  */

       public int[] hengShang(int x, int y) { 

              int r = -2; 

              int i; 

              // 向上 

              for (i = y - 1; i >= 0; i--) { 

                     if (Chess.chess[x][i] == 0)

                            break;

                     if (Chess.chess[x][i] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x][y - 1] != Chess.chess[x][i]) { 

 

                     return new int[] { x, r }; 

              }

              else

                     return null;

       } 

 

       /* 向上检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL  */

       public int[] hengShang1(int x, int y) { 

              int r = -2; 

              int i; 

              // 向上 

              for (i = y - 1; i >= 0; i--) { 

                     if (Chess.chess[x][i] == 0)

                            break;

                     if (Chess.chess[x][i] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x][y - 1] != Chess.chess[x][i]) { 

                     // 改变中间的子 

                     paintChess(x, y, x, r); 

                     return new int[] { x, r }; 

              }

              else

                     return null;

       } 

 

       /* 向下检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL */

       public int[] hengXia(int x, int y) { 

              int r = -2; 

              int i; 

              // 向下 

              for (i = y + 1; i < Chess.rows-1; i++) { 

                     if (Chess.chess[x][i] == 0)

                            break;

                     if (Chess.chess[x][i] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

 

              if (r != -2 && Chess.chess[x][y + 1] != Chess.chess[x][i]) { 

                     return new int[] { x, r }; 

              }

              else

                     return null;

       } 

 

       /* 向下检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL */

       public int[] hengXia1(int x, int y) { 

              int r = -2; 

              int i; 

              // 向下 

              for (i = y + 1; i < Chess.rows-1; i++) { 

                     if (Chess.chess[x][i] == 0)

                            break;

                     if (Chess.chess[x][i] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x][y + 1] != Chess.chess[x][i]) { 

                     // 改变中间的子 

                     paintChess(x, y, x, r); 

                     return new int[] { x, r }; 

              }

              else

                     return null;

       } 

 

       /* 斜右上方向    */

       public int[] xieyouS(int x, int y) { 

              // 向上 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x + 1, j = y - 1; i < Chess.rows-1 && j >= 0; i++, j--) { 

                     if (Chess.chess[i][j] == 0)

                            break;  

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x + 1][y - 1] != Chess.chess[i][j]) { 

                     return new int[] { r, s }; 

              }

              else

                     return null;

       } 

 

       /* 斜右下方向   */

       public int[] xieyouX(int x, int y) { 

              // 向下 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x + 1, j = y + 1; i < Chess.rows-1 && j < Chess.cols-1; i++, j++) { 

                     if (Chess.chess[i][j] == 0)

                            break;   

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2

                            && Chess.chess[x + 1][y + 1] != Chess.chess[i][j]) { 

                     return new int[] { r, s }; 

              }

              else 

                     return null; 

       } 

 

       /* 斜左上方向    */

       public int[] xiezuoS(int x, int y) { 

              // 向上 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { 

                     if (Chess.chess[i][j] == 0)

                            break; 

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x - 1][y - 1] != Chess.chess[i][j]) { 

                     return new int[] { r, s }; 

              }

              else

                     return null; 

       } 

 

       /* 斜左下方向    */

       public int[] xiezuoX(int x, int y) { 

              // 向下 

              int r = -2, s = -2;  

              int i, j; 

              for (i = x - 1, j = y + 1; i >= 0 && j < Chess.cols-1; i--, j++) { 

                     if (Chess.chess[i][j] == 0)

                            break; 

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x - 1][y + 1] != Chess.chess[i][j]) { 

                     return new int[] { r, s }; 

              }

              else

                     return null;

       }

 

       /* 向右检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL  */

       public int[] hengYou1(int x, int y) { 

              int r = -2; 

              int i; 

              // 向右 

              for (i = x + 1; i < Chess.cols-1; i++) { 

                     if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1)

                            break;

                     if (Chess.chess[i][y] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x + 1][y] != Chess.chess[i][y]) { 

                     // 改变中间的子 

                     paintChess(x, y, r, y); 

                     return new int[] { r, y }; 

              }

              else

                     return null; 

       } 

 

       /* 向左检测是否有相同颜色棋子,如果有且不相邻,改变中间棋子颜色,否则返回NULL */

       public int[] hengZuo1(int x, int y) { 

              int r = -2; 

              int i; 

              // 向左 

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

                     if (Chess.chess[i][y] != 1 && Chess.chess[i][y] != -1)

                            break;

                     if (Chess.chess[i][y] == Chess.chess[x][y]) { 

                            r = i; 

                            break; 

                     } 

              } 

              if (r != -2 && Chess.chess[x - 1][y] != Chess.chess[i][y]) { 

                     // 改变中间的子 

                     paintChess(x, y, r, y); 

                     return new int[] { r, y }; 

              }

              else

                     return null; 

       } 

 

       /* 斜右上方向    */

       public int[] xieyou1(int x, int y) { 

              // 向上 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x + 1, j = y - 1; i < Chess.rows-1 && j >= 0; i++, j--) { 

                     if (Chess.chess[i][j] == 0)

                            break;

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x + 1][y - 1] != Chess.chess[i][j]) { 

                     // 改变中间的子 

                     paintChess1(x, y, i, j); 

                     return new int[] { r, s }; 

              }

              else

                     return null;

       } 

 

       /*  斜右下方向    */

       public int[] xieyou2(int x, int y) { 

              // 向下 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x + 1, j = y + 1; i < Chess.rows-1 && j < Chess.cols-1; i++, j++) { 

                     if (Chess.chess[i][j] == 0)

                            break;

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x + 1][y + 1] != Chess.chess[i][j]) { 

                     // 改变中间的子 

                     paintChess2(x, y, i, j); 

                     return new int[] { r, s }; 

              }

              else 

                     return null;

       } 

 

       /* 斜左上方向     */

       public int[] xiezuo1(int x, int y) { 

              // 向上 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { 

                     if (Chess.chess[i][j] == 0)

                            break;

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x - 1][y - 1] != Chess.chess[i][j]) { 

                     // 改变中间的子 

                     paintChess3(x, y, i, j); 

                     return new int[] { r, s }; 

              }

              else

                     return null; 

       } 

 

       /* 斜左下方向    */

       public int[] xiezuo2(int x, int y) { 

              // 向下 

              int r = -2, s = -2; 

              int i, j; 

              for (i = x - 1, j = y + 1; i >= 0 && j < Chess.cols-1; i--, j++) { 

                     if (Chess.chess[i][j] == 0)

                            break;  

                     if (Chess.chess[i][j] == Chess.chess[x][y]) { 

                            r = i; 

                            s = j; 

                            break; 

                     } 

              } 

              if (r != -2 && s != -2 && Chess.chess[x - 1][y + 1] != Chess.chess[i][j]) { 

                     // 改变中间的子 

                     paintChess4(x, y, i, j); 

                     return new int[] { r, s }; 

              }

              else

                     return null;

       } 

}

 

三、实验体会

       此次实验是这几次试验中挑战最大的一次,不同于以往的各种编程语言的实验。老师大胆的将实践融入到试验中来让我们根据已有的小游戏程序体验建议的软件开发过程。真正结合到了对PC所能理解的程度上来。

       在基础部分,由于在各种网络下尝试了两天都没有打开实验楼的虚拟机的图形界面,所以git部分最后决定才用的同伴的样板提交实验报告。不过其余部分均为自己独立完成。

       在编程过程中,由于是第一次接触到实践编一个游戏出来,刚开始并不是很适应。而且从原来的一个人思考程序问题,语言结构的思想转变为两人共同完成也需要一定的时间来转变自己的思想理念。

       此次实验最大的收获个人觉得并不在于编出程序是的欣喜。而是对于eclipse软件中重构这一功能的掌握。对于初学者的我们来说,是一个很方便的功能,既可以保证编写代码的效率,有降低了编写时的出错率。同时也可以使自己的代码更加规范。最重要的一点是:可以提高代码的复用性,真正意义上避免DRY的产生。对将以后的java语言的掌握会有很大帮助。

       最后感谢老师的教导。

 

 

附:

        

  

步骤

  
  

耗时

  
  

百分比

  

需求分析

2h

20%

设计

0.5h

5%

代码实现

4h

40%

测试

2.5h

25%

分析总结

1h

10%

 

posted @ 2015-06-08 23:06  王国伊  阅读(293)  评论(0编辑  收藏  举报