java课程设计模版

课程设计(论文)任务书

  学 院 专 业  2     

一、课程设计(论文)题目:     人机对弈五子棋游戏          

二、课程设计(论文)工作:

 2015 6 23 日起至2015 625日止

三、课程设计(论文) 地点:          5-205               

四、课程设计(论文)内容要求:

1.本课程设计的目的

(1)使学生掌握软件开发的基本工作流程;

(2)巩固JAVA程序设计课程所学的内容;

(3)培养学生的计算机思维能力以及合作的精神;

(4)培养学生分析、解决问题的能力;

(5)提高学生的科技论文写作能力。 

2.课程设计的任务及要求

1)基本要求:

(1)研究课程设计任务,并进行系统需求分析;

(2)对系统进行总体设计,分解系统功能模块,进行任务分配,以实现分工合作;

(3)实现各功能模块代码;

(4)系统组装,测试、完善系统。

2)创新要求:

在基本要求达到后,可进行创新设计,如改进界面、增加功能或进行代码优化。

3)课程设计论文编写要求

(1)要按照书稿的规格打印誊写课程设计论文

(2)论文包括封面、设计任务书(含评语)、摘要、目录、设计内容、设计小结

(3)论文装订按学校的统一要求完成

4)参考文献:

(1)丁振凡,《JAVA语言程序设计》,清华大学出版社

(2)丁振凡,《JAVA语言程序设计实验指导与习题解答》,清华大学出版社

(3)http://cai.ecjtu.jx.cn/

5)课程设计进度安排

内容                   天数             地点

系统总体设计            1               实验室

软件设计及调试          2               实验室

答辩及撰写报告          2            实验室、图书馆

 学生签名:             

20##年6月25日

课程设计(论文)评审意见

(1)  课程设计过程(20分):优( )、良( )、中( )、一般( )、差

(  );

(2)  是否完成调试,系统运行效果(30分):优( )、良( )、中( )、

一般(  )、差(  );

(3)回答问题(20分):优( )、良( )、中( )、一般( )、差( );

(4)课程设计报告(30分):优( )、良( )、中( )、一般( )、差

(  );

(5)格式规范性及考勤是否降等级:是( )、否( )

评阅人       职称教授  

 20##年 6月27日


人机对弈五子棋游戏设计

摘要:

近些年来五子棋游戏成为了人们的最爱,游戏的下载量很高可以说一直排在前几位,也是人们日常生活中放松娱乐的最爱,特别是最近IT行业的发展,人工智能的实现,移动客户端的发展和普及为我们五子棋游戏的发展带来了新的生机,新的高潮,人工智能与五子棋的完美结合是五子棋的又一项重要发展,而我们现在开发的人机对弈更是掀起了五子棋发展的新篇章,为五子棋的发展与传播提供了新的机会,但是机会与挑战并存,我们相信通过我们对五子棋程序的不断优化和完善,五子棋一定会越来越受到大家的欢迎。

五子棋是一种受大众广泛喜爱的游戏,其规则简单,变化多端,非常富有趣味性和消遣性。在Windows操作系统下,用VC++实现了这个人机对战的五子棋程序。采用了博弈树的方法,应用了剪枝和最大最小树原理进行搜索发现最好的下子位置。介绍了五子棋程序的数据结构、评分规则、胜负判断方法和搜索算法过程。


                目录

1、项目内容、要求与分组情况总体介绍................. 1

2、概要设计........................................ 1

2.1开发工具及环境................................... 1

2.2 任务及需求分析................................... 1

2.3 工作原理........................................ 1

3、具体实现........................................ 1

3.1 类设计.......................................... 1

3.2 关键技术处理..................................... 2

4、运行调试与分析讨论............................... 3

5、设计体会与小结.................................. 3

6、参考文献........................................ 4

附录:(源程序)................................... 4


1、项目内容、要求与分组情况总体介绍

本次实验的目标是制作一个计算器,可以进行简单的四则运算(加、减、乘、除)。小组的具体分工情况见下表:

2、概要设计

2.1开发工具及环境

文本编辑器:记事本;

运行环境:命令提示符(DOS环境)。

2.2 任务及需求分析

设计简易计算器程序,实现简单的运算(加减乘除),具体任务如下:

(1)计算器窗口界面布局设计;

(2)各种计算功能的算法分析;

(3)编程实现具体的计算功能,实现类的封装处理;

(4)运行测试程序,调试纠正运行错误。

2.3 工作原理

(1)工作过程

点击相应的数字键,即会在显示文档中显示该数字。进行相应的运算,首先输入第一个计算数字,然后输入运算符,再输入第二个计算数字,点击“=”按钮即得计算结果。同时,还有归零功能,点击该按钮,显示栏中即可归零。

(2)工作原理

利用图形界面编程实现,利用到的知识点包括:

q  窗体的创建和应用界面布局;

q  事件处理,包括按钮的动作事件处理,窗体事件。

q  接口知识:事件处理中通过接口约束事件监听者必须实现相应接口中定义的方法。

q  流程控制语句:通过条件判断区分不同按钮,作不同操作处理。

3、 具体实现

3.1 类设计

(1) 类名:jisuanqi

作用:实现窗口的布局设定,事件注册。

该类继承WindowAdapter类。

类中含有的方法介绍:

q  frame()方法:实现窗体的界面布局及按钮的事件注册。

q  main()方法:应用程序的入口,完成创建对象并调用frame()方法。

(2) 类名:close

该类为jisuanqi的内嵌类,作用是实现对关闭按钮的动作事件监听处理。

该类实现ActionListener接口。

类中含有的方法介绍:

q   actionPerformed方法:实现关闭按钮的动作事件处理。

(3) 类名:buttonlistener

该类为jisuanqi的内嵌类,作用是实现计算器中数字按钮和各类运算按钮等的动作事件监听处理。

该类实现ActionListener接口。

类中含有的方法介绍:

q  actionPerformed方法:实现各类按钮的动作事件处理。

q  jisuan()方法:实现具体运算操作。

3.2 关键技术处理

(1) 窗体界面设计及注册事件监听

Frame fm = new Frame("简单计算器");

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

       b[i] = new Button(ss[i]);

    }                             

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

    p2.add(b[i]);

    }                               //创建按钮 并添加到P2

    b[16].setBackground(Color.yellow);

    txt = new TextField(15);

    txt.setEditable(false);

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

    b[i].addActionListener(new buttonlistener());

……

 }

:此方法主要设置窗口、面板、各个按键。对各个按键进行定义(定义好各按键该实现什么功能)、排布,将各个按键注册到监听器上。

(2) 按钮事件处理

q  区分事件源:通过事件对象的getSource()方法得到事件源。关键代码如下:

Button btn = (Button) e.getSource();

if (btn.getLabel() == "=") {

……

}

q  改变计算器的结果。更改文本框的显示,先读原有的数据。

txt.setText(txt.getText() + btn.getLabel());

q  清除计算器的显示

    if (btn.getLabel() == "归零")

       txt.setText("");

(3) 窗体关闭

class close implements ActionListener {//退出

       public void actionPerformed(ActionEvent e) {

           System.exit(0);

}

    }

【注】也可以执行窗体的dispose()方法实现窗体关闭。

(4) 记住前一个运算符

     引入一个字符串类型的类变量 s记录前一个运算符,每次按运算符是先执行前一个运算符的结果。

4、运行调试与分析讨论

调试运行4*5运算,运算过程如下所示:

    测试程序,点击数字键可将数据输入到文本框,点击“+”、“-”、“*”、“/”按钮可进行前一个运算,点击“=”可完成整个运算。从而达到能进行加、减、乘、除运算的简易计算器的功能。点击关闭图标可关闭计算器。

5、设计体会与小结

  通过独立完成五子棋对弈系统的后,发现自己在进行软件开发方面提高不少,同时积极利用所学到的新技术用于自己的设计开发过程。另外,在整个开发的过程中,时间也比较仓促。因此,该系统必然会存在一些缺陷和不足。如:没有讨论五子棋禁手的问题。另一个就是电脑按即定的算法去与玩家下子。这种算法有“固定”。不太会变,玩家若是仔细观察,可以掌握其规律。还有就是界面不是很华丽有待改进。尽管本系统存在着很多不足,但其实现了最重要的功能就是有人工智能。这也让我对计算机中的人工智能领域有一定的了解。另一个就在做系统的过程中,我学到了Visual C++的一些基本结构,尤其对于MFC有一定的了解。还有就是对于C++有更深一步的认识。

6、参考文献

[1] 丁振凡,《JAVA语言程序设计》,清华大学出版社.

[2] 丁振凡,《JAVA语言程序设计实验指导与习题解答》,清华大学出版社.

[3] [4] http://cai.ecjtu.jx.cn/

附录:(源程序)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

class ChessFrame extends JFrame implements ActionListener{

private String[] strsize={"标准棋盘","大号棋盘","超大号棋盘"};

private String[] strmode={"人机对弈","人人对战"};

public static boolean iscomputer=true,checkcomputer=true;

private int width,height;

private ChessModel cm;

private MainPanel mp;

public ChessFrame() {

   this.setTitle("五子棋游戏");

   cm=new ChessModel(1);

   mp=new MainPanel(cm);

   Container con=this.getContentPane();

   con.add(mp,"Center");

   this.setResizable(false);

   this.addWindowListener(new ChessWindowEvent());

   MapSize(14,14);

   JMenuBar mbar = new JMenuBar();

   this.setJMenuBar(mbar);

   JMenu gameMenu = new JMenu("游戏");

   mbar.add(makeMenu(gameMenu, new Object[] {

    "再来一局", null, "棋盘",null,"模式", null,"认输", null, "悔棋", null, "退出"}, this));

   JMenu lookMenu =new JMenu("外观");

   mbar.add(makeMenu(lookMenu,new Object[] {"类型一","类型二","类型三"},this));

   JMenu helpMenu = new JMenu("版本");

   mbar.add(makeMenu(helpMenu, new Object[] {"关于"}, this));

}

public JMenu makeMenu(Object parent, Object items[], Object target){

   JMenu m = null;

   if(parent instanceof JMenu)

    m = (JMenu)parent;

   else if(parent instanceof String)

    m = new JMenu((String)parent);

   else

    return null;

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

    if(items[i] == null)

     m.addSeparator();

    else if(items[i] == "棋盘"){

     JMenu jm = new JMenu("棋盘");

     ButtonGroup group=new ButtonGroup();

     JRadioButtonMenuItem rmenu;

     for (int j=0;j<strsize.length;j++){

      rmenu=makeRadioButtonMenuItem(strsize[j],target);

      if (j==0)

       rmenu.setSelected(true);

      jm.add(rmenu);

      group.add(rmenu);

     }

     m.add(jm);

    }else if(items[i] == "模式"){

     JMenu jm = new JMenu("模式");

     ButtonGroup group=new ButtonGroup();

     JRadioButtonMenuItem rmenu;

     for (int h=0;h<strmode.length;h++){

      rmenu=makeRadioButtonMenuItem(strmode[h],target);

      if(h==0)

       rmenu.setSelected(true);

      jm.add(rmenu);

      group.add(rmenu);

     }

     m.add(jm);

    }else

     m.add(makeMenuItem(items[i], target));

   return m;

}

public JMenuItem makeMenuItem(Object item, Object target){

   JMenuItem r = null;

   if(item instanceof String)

    r = new JMenuItem((String)item);

   else if(item instanceof JMenuItem)

    r = (JMenuItem)item;

   else

    return null;

   if(target instanceof ActionListener)

    r.addActionListener((ActionListener)target);

   return r;

}

public JRadioButtonMenuItem makeRadioButtonMenuItem(

    Object item, Object target){

    JRadioButtonMenuItem r = null;

    if(item instanceof String)

       r = new JRadioButtonMenuItem((String)item);

    else if(item instanceof JRadioButtonMenuItem)

       r = (JRadioButtonMenuItem)item;

    else

       return null;

    if(target instanceof ActionListener)

       r.addActionListener((ActionListener)target);

    return r;

    }

    public void MapSize(int w,int h){

    setSize(w * 24, h * 27);

    if(this.checkcomputer)

       this.iscomputer=true;

    else

       this.iscomputer=false;

    mp.setModel(cm);

    mp.repaint();

    }

    public boolean getiscomputer(){

    return this.iscomputer;

    }

    public void restart(){

    int modeChess = cm.getModeChess();

    if(modeChess <= 3 && modeChess >= 0){

       cm = new ChessModel(modeChess);

       MapSize(cm.getWidth(),cm.getHeight());

    }

    }

    public void actionPerformed(ActionEvent e){

    String arg=e.getActionCommand();

    try{

    if (arg.equals("类型三"))

        UIManager.setLookAndFeel(

         "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

       else if(arg.equals("类型二"))

     UIManager.setLookAndFeel(

      "com.sun.java.swing.plaf.motif.MotifLookAndFeel");

    else

     UIManager.setLookAndFeel(

      "javax.swing.plaf.metal.MetalLookAndFeel" );

    SwingUtilities.updateComponentTreeUI(this);

   }catch(Exception ee){}

   if(arg.equals("标准棋盘")){

    this.width=14;

    this.height=14;

    cm=new ChessModel(1);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("大号棋盘")){

    this.width=18;

    this.height=18;

    cm=new ChessModel(2);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("超大号棋盘")){

    this.width=22;

    this.height=22;

    cm=new ChessModel(3);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("人机对弈")){

    this.checkcomputer=true;

    this.iscomputer=true;

    cm=new ChessModel(cm.getModeChess());

    MapSize(cm.getWidth(),cm.getHeight());

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("人人对战")){

    this.checkcomputer=false;

    this.iscomputer=false;

    cm=new ChessModel(cm.getModeChess());

    MapSize(cm.getWidth(),cm.getHeight());

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("再来一局")){

    restart();

   }

   if(arg.equals("悔棋")){

   if(!checkcomputer){

   int result = JOptionPane.showConfirmDialog(this, (FiveChessAppletDemo.isBlack==false?"白方悔棋,黑方是否同意?":"黑方悔棋,白方是否同意?"));

    if (result == 0) {

        JOptionPane.showMessageDialog(this, "双方同意悔棋决定,棋盘清空!请继续...");

         restart();

           }

      }

   }

   if(arg.equals("认输")){

            int result = JOptionPane.showConfirmDialog(this, "是否认输?");

            if (result == 0) {

  JOptionPane.showMessageDialog(this,"游戏结束,"+(FiveChessAppletDemo.isBlack==true?"黑方认输,白方获胜!":"白方认输,黑方获胜!"));

   }

}

   if(arg.equals("关于"))

    JOptionPane.showMessageDialog(null, " 制作者:\n 制作时间:20##年6月25日", "课程设计制作介绍",JOptionPane.PLAIN_MESSAGE );

   if(arg.equals("退出"))

    System.exit(0);

}

}

class ChessModel {

private int width,height,modeChess;

private int x=0,y=0;

private int[][] arrMapShow;

private boolean isOdd,isExist;

public ChessModel() {}

public ChessModel(int modeChess){

   this.isOdd=true;

   if(modeChess == 1){PanelInit(14, 14, modeChess);}

   if(modeChess == 2){PanelInit(18, 18, modeChess);}

   if(modeChess == 3){PanelInit(22, 22, modeChess);}

}

private void PanelInit(int width, int height, int modeChess){

   this.width = width;

   this.height = height;

   this.modeChess = modeChess;

   arrMapShow = new int[width+1][height+1];

   for(int i = 0; i <= width; i++)

for(int j = 0; j <= height; j++)

     arrMapShow[i][j] = -1;

}

public boolean getisOdd(){ return this.isOdd;}

public void setisOdd(boolean isodd){

   if(isodd)

    this.isOdd=true;

   else

    this.isOdd=false;

}

public boolean getisExist(){ return this.isExist;}

public int getWidth(){return this.width;}

public int getHeight(){return this.height;}

public int getModeChess(){return this.modeChess;}

public int[][] getarrMapShow(){ return arrMapShow;}

private boolean badxy(int x, int y){

   if(x >= width+20 || x < 0)

    return true;

   return y >= height+20 || y < 0;

}

public boolean chessExist(int i,int j){

   if(this.arrMapShow[i][j]==1 || this.arrMapShow[i][j]==2)

    return true;

   return false;

}

public void readyplay(int x,int y){

   if(badxy(x,y))

    return;

   if (chessExist(x,y))

    return;

   this.arrMapShow[x][y]=3;

}

public void play(int x,int y){

   if(badxy(x,y))

    return;

   if(chessExist(x,y)){

    this.isExist=true;

    return;

   }else

    this.isExist=false;

   if(getisOdd()){

    setisOdd(false);

   this.arrMapShow[x][y]=1;

   }else{

    setisOdd(true);

    this.arrMapShow[x][y]=2;

   }

}

public void computerDo(int width,int height){

   int max_black,max_white,max_temp,max=0;

   setisOdd(true);

   System.out.println("计算机走棋 ...");

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

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

     if(!chessExist(i,j)){

      max_white=checkMax(i,j,2);

      max_black=checkMax(i,j,1);

      max_temp=Math.max(max_white,max_black);

      if(max_temp>max){

       max=max_temp;

       this.x=i;

       this.y=j;

      }

     }

    }

   }

   setX(this.x);

   setY(this.y);

   this.arrMapShow[this.x][this.y]=2;

}

public void setX(int x){ this.x=x;}public void setY(int y){this.y=y;}

public int getX(){return this.x;}

public int getY(){return this.y;}

public int checkMax(int x, int y,int black_or_white){

   int num=0,max_num,max_temp=0;

   int x_temp=x,y_temp=y;

   int x_temp1=x_temp,y_temp1=y_temp;

   for(int i=1;i<5;i++){

    x_temp1+=1;

    if(x_temp1>this.width)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   x_temp1=x_temp;

   for(int i=1;i<5;i++){

    x_temp1-=1;

    if(x_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   if(num<5)

    max_temp=num;

   x_temp1=x_temp;

   y_temp1=y_temp;

   num=0;

   for(int i=1;i<5;i++){

    y_temp1-=1;

    if(y_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   y_temp1=y_temp;

   for(int i=1;i<5;i++){

    y_temp1+=1;

    if(y_temp1>this.height)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   if(num>max_temp&&num<5)

    max_temp=num;

   x_temp1=x_temp;

   y_temp1=y_temp;

   num=0;

   for(int i=1;i<5;i++){

    x_temp1-=1;

    y_temp1-=1;

    if(y_temp1<0 || x_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   x_temp1=x_temp;

   y_temp1=y_temp;

   for(int i=1;i<5;i++){

    x_temp1+=1;

    y_temp1+=1;

    if(y_temp1>this.height || x_temp1>this.width)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   if(num>max_temp&&num<5)

    max_temp=num;

   x_temp1=x_temp;

   y_temp1=y_temp;

   num=0;

   for(int i=1;i<5;i++){

    x_temp1+=1;

    y_temp1-=1;

    if(y_temp1<0 || x_temp1>this.width)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   x_temp1=x_temp;

   y_temp1=y_temp;

   for(int i=1;i<5;i++){

    x_temp1-=1;

    y_temp1+=1;

    if(y_temp1>this.height || x_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)

     num++;

    else

     break;

   }

   if(num>max_temp&&num<5)

    max_temp=num;

   max_num=max_temp;

   return max_num;

}

public boolean judgeSuccess(int x,int y,boolean isodd){

   int num=1;

   int arrvalue;

   int x_temp=x,y_temp=y;

   if(isodd)

    arrvalue=2;

   else

    arrvalue=1;

   int x_temp1=x_temp,y_temp1=y_temp;

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

    x_temp1+=1;

    if(x_temp1>this.width)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   x_temp1=x_temp;

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

    x_temp1-=1;

    if(x_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   if(num==5)

    return true;

   x_temp1=x_temp;

   y_temp1=y_temp;

   num=1;

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

    y_temp1-=1;

    if(y_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   y_temp1=y_temp;

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

    y_temp1+=1;

    if(y_temp1>this.height)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   if(num==5)

    return true;

   x_temp1=x_temp;

   y_temp1=y_temp;

   num=1;

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

    x_temp1-=1;

    y_temp1-=1;

    if(y_temp1<0 || x_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   x_temp1=x_temp;

   y_temp1=y_temp;

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

   x_temp1+=1;

   y_temp1+=1;

   if(y_temp1>this.height || x_temp1>this.width)

    break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   if(num==5)

    return true;

   x_temp1=x_temp;

   y_temp1=y_temp;

   num=1;

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

    x_temp1+=1;

    y_temp1-=1;

    if(y_temp1<0 || x_temp1>this.width)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   x_temp1=x_temp;

   y_temp1=y_temp;

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

    x_temp1-=1;

    y_temp1+=1;

    if(y_temp1>this.height || x_temp1<0)

     break;

    if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)

     num++;

    else

     break;

   }

   if(num==5)

    return true;

   return false;

}

public void showSuccess(JPanel jp){

   JOptionPane.showMessageDialog(jp,"你赢了","结果",

    JOptionPane.INFORMATION_MESSAGE);

}

public void showDefeat(JPanel jp){

   JOptionPane.showMessageDialog(jp,"你输了","结果",

    JOptionPane.INFORMATION_MESSAGE);

}

}

class ChessWindowEvent extends WindowAdapter{

public void windowClosing(WindowEvent e){

   System.exit(0);

}

ChessWindowEvent(){}

}

public class FiveChessAppletDemo {

       public static Boolean isBlack = true;

       public static void main(String args[]){

              ChessFrame cf = new ChessFrame();

              cf.show();

}

}

class MainPanel extends JPanel implements MouseListener,MouseMotionListener{

private int width,height;

private ChessModel cm;

MainPanel(ChessModel mm){

   cm=mm;

   width=cm.getWidth();

   height=cm.getHeight();

   addMouseListener(this);

}  

public void setModel(ChessModel mm){

   cm = mm;

   width = cm.getWidth();

   height = cm.getHeight();

}

public void paintComponent(Graphics g){

   super.paintComponent(g);

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

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

     int v = cm.getarrMapShow()[i][j];

     draw(g, i, j, v);

    }

   }

}

public void draw(Graphics g, int i, int j, int v){

   int x = 20 * i+20;

   int y = 20 * j+20;

   if(i!=width && j!=height){

    g.setColor(Color.darkGray);

    g.drawRect(x,y,20,20);

   }

   if(v == 1 ){

    g.setColor(Color.gray);

    g.drawOval(x-8,y-8,16,16);

    g.setColor(Color.black);

    g.fillOval(x-8,y-8,16,16);

    FiveChessAppletDemo.isBlack = true;

   }

   if(v == 2 ){

    g.setColor(Color.gray);

    g.drawOval(x-8,y-8,16,16);

    g.setColor(Color.white);

    g.fillOval(x-8,y-8,16,16);

    FiveChessAppletDemo.isBlack = false;

   }

   if(v ==3){

    g.setColor(Color.cyan);

    g.drawOval(x-8,y-8,16,16);

   }

}

public void mousePressed(MouseEvent evt){

   int x = (evt.getX()-10) / 20;

   int y = (evt.getY()-10) / 20;

   System.out.println(x+" "+y);

   if (evt.getModifiers()==MouseEvent.BUTTON1_MASK){

    cm.play(x,y);

    System.out.println(cm.getisOdd()+" "+cm.getarrMapShow()[x][y]);

    repaint();

    if(cm.judgeSuccess(x,y,cm.getisOdd())){

     cm.showSuccess(this);

     evt.consume();

     ChessFrame.iscomputer=false;

    }

    if(ChessFrame.iscomputer&&!cm.getisExist()){

     cm.computerDo(cm.getWidth(),cm.getHeight());

     repaint();

    

     if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){

      cm.showDefeat(this);

      evt.consume();

     }

    }

}

}

public void mouseClicked(MouseEvent evt){}

public void mouseReleased(MouseEvent evt){}

public void mouseEntered(MouseEvent mouseevt){}

public void mouseExited(MouseEvent mouseevent){}

public void mouseDragged(MouseEvent evt){}

public void mouseMoved(MouseEvent moveevt){

   int x = (moveevt.getX()-10) / 20;

   int y = (moveevt.getY()-10) / 20;

   cm.readyplay(x,y);

   repaint();

}

}

相关推荐