郭笑笑

导航

我的个人作业

 

 

 

 

题目     五子棋运算设计程序    

https://github.com/guoxiaox/test/tree/master

 

 

 

 

学校:齐鲁工业大学   

学院:信息学院       

班级:计科(高职)13-3

学号: 201303014100  

姓名:郭笑笑         

 

 

 

 

 

 

 

 

 

 

 

 

五子棋运算设计程序

项目设计方案

1界面棋盘设计

在对棋盘界面设计方面要考虑简洁友好,符合游戏者需求。棋子的设计方面系统中设置了两种棋子颜色,white或者black, 游戏者可自行选择。棋子怎样画出来,怎样使棋子按我们所想的方式来绘制出来是设计的主要难题。运行时要求当每次点击鼠标的时候就在点击鼠标的地方画一个棋子,所以得定义一个棋子的类使点击鼠标时系统把棋子绘制出来。这样主界面里的棋子就确定了,而且也确定了几个所需要的类。可以先定义好这些类了。

有了以上的准备就可以把棋盘绘制出来了,下棋时每次在适当位置点击鼠标的时候就在所点击的位置上画出你这个角色的颜色,然后计算机会自动的下棋,计算机自动下棋要有一个控制变量来控制能不能下棋。人可以随意的下子,可是计算机就要计算在哪个位置下子了。这里由计算机下子的算法来确定,人人对战的话就不象单机游戏一样,要计算机来判断人下子之后计算机下那一个地方了,人人对战只要在双方下子的时候判断赢棋情况,当然还要同步的控制两个人下棋的顺序,这样的话就要用到线程了,各自都继承或实现线程类或接口,以便各自能随时单独控制接发消息。

2. 总体设计

为实现五子棋运算测试程序设计需求,需要实现的FiveChessAppletDemo类、ChessFrame类、ChessModel类、MainPanel类、ChessWindowEvent类等功能。

3.类组合结构图

 

给出各个类之间的整体组合结构

 

 

 
   

 

 

 

 

 

 

 

 


                           

图1所示

核心算法详细设计

各个类的简要描述(在整个软件中的地位及实现功能的简要描述,其继承层次关系及所实现的接口等);详细设计每个类的成员变量、构造方法与方法;并根据详细设计给出每个类的实现代码。示例如下:

(1)FiveChessAppletDemo

main方法创建了ChessFrame类的一个实例对象(cf),并启动屏幕显示显示该实例对象。定义了一个名为 ChessFrame 的监听类实现 ActionListener

(2)ChessFrame

       创建五子棋游戏主窗体和菜单

 

(3)ChessModel

    实现了整个五子棋程序算法的核心

    1、规定棋盘的宽度、高度、棋盘的模式

       private int width,height,modeChess;

    2、规定棋盘方格的横向、纵向坐标

       private int x=0,y=0;

    3、棋盘方格的横向、纵向坐标所对应的棋子颜色,

       数组arrMapShow只有3个值:1,2,3,-1,

       其中1代表该棋盘方格上下的棋子为黑子,

       2代表该棋盘方格上下的棋子为白子,

       3代表为该棋盘方格上没有棋子,

       -1代表该棋盘方格不能够下棋子

       private int[][] arrMapShow;

     4、交换棋手的标识,棋盘方格上是否有棋子的标识符

       private boolean isOdd,isExist;

 

(4)MainPanel

    1、构建一个面板,在该面板上画上棋盘;

    2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)

(5)ChessWindowEvent类

     响应退出窗口

 

源代码

Java编程:五子棋游戏源代码

import java.awt.*;                                                     

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

import java.io.PrintStream;

import javax.swing.JComponent;

import javax.swing.JPanel;

 

/*

*main方法创建了ChessFrame类的一个实例对象(cf),

*并启动屏幕显示显示该实例对象。

**/

public class FiveChessAppletDemo {

public static void main(String args[]){

   ChessFrame cf = new ChessFrame();

   cf.show();

}

}

 

/*

*类ChessFrame主要功能是创建五子棋游戏主窗体和菜单

**/

class ChessFrame extends JFrame implements ActionListener {

private String[] strsize={"20x15","30x20","40x30"};

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(20,15);

   JMenuBar mbar = new JMenuBar();

   this.setJMenuBar(mbar);

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

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

    "开局", "棋盘","模式", null, "退出"

    }, this));

   JMenu lookMenu =new JMenu("视图");

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

    "Metal","Motif","Windows"

    },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 * 20+50 , h * 20+100 );

    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 >= 1){

       cm = new ChessModel(modeChess);

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

    }else{

       System.out.println("\u81EA\u5B9A\u4E49");

    }

    }

   

    public void actionPerformed(ActionEvent e){

    String arg=e.getActionCommand();

    try{

       if (arg.equals("Windows"))

        UIManager.setLookAndFeel(

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

       else if(arg.equals("Motif"))

     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("20x15")){

    this.width=20;

    this.height=15;

    cm=new ChessModel(1);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("30x20")){

    this.width=30;

    this.height=20;

    cm=new ChessModel(2);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("40x30")){

    this.width=40;

    this.height=30;

    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("关于"))

    JOptionPane.showMessageDialog(this, "五子棋游戏测试版本", "关于", 0);

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

    System.exit(0);

}

}

 

/*

*类ChessModel实现了整个五子棋程序算法的核心

*/

class ChessModel {

//棋盘的宽度、高度、棋盘的模式(如20×15)

private int width,height,modeChess;

//棋盘方格的横向、纵向坐标

private int x=0,y=0;

//棋盘方格的横向、纵向坐标所对应的棋子颜色,

//数组arrMapShow只有3个值:1,2,3,-5,

//其中1代表该棋盘方格上下的棋子为黑子,

//2代表该棋盘方格上下的棋子为白子,

//3代表为该棋盘方格上没有棋子,

//-5代表该棋盘方格不能够下棋子

private int[][] arrMapShow;

//交换棋手的标识,棋盘方格上是否有棋子的标识符

private boolean isOdd,isExist;

 

public ChessModel() {}

 

//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘

public ChessModel(int modeChess){

   this.isOdd=true;

   if(modeChess == 1){

    PanelInit(20, 15, modeChess);

   }

   if(modeChess == 2){

    PanelInit(30, 20, modeChess);

   }

   if(modeChess == 3){

    PanelInit(40, 30, 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] = -5;

    }

   }

}

 

//获取是否交换棋手的标识符

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;

   //judge right

   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;

   }

   //judge left

   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;

 

   //judge up

   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;

   }

   //judge down

   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;

 

   //judge left_up

   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;

   }

   //judge right_down

   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;

 

   //judge right_up

   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;

   }

   //judge left_down

   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,"你赢了,好厉害!","win",

    JOptionPane.INFORMATION_MESSAGE);

}

 

//输棋后的提示

public void showDefeat(JPanel jp){

   JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost",

    JOptionPane.INFORMATION_MESSAGE);

}

}

 

/*

*类MainPanel主要完成如下功能:

*1、构建一个面板,在该面板上画上棋盘;

*2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)

**/

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();

}

 

//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),

//然后调用draw方法在棋盘上画出相应的棋子

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.white);

    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);

   }

   //画白色棋子

   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);

   }

   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();

}

}

 

class ChessWindowEvent extends WindowAdapter{

public void windowClosing(WindowEvent e){

   System.exit(0);

}

 

ChessWindowEvent()

 

{

}

}

 

                                                    

 

 

                                                     

 

使用说明与运行结果截图

 (1)五子棋游戏的主界面

 

图2程序主界面

 

 

 

图3“游戏”栏中的选项

 

图4视图中的各个选项

 

 

图5帮助栏中的选项

 

(5)单击“帮助”栏中的“关于”选项时弹出的界面

 

 

 

 

图6关于选项弹出的界面

 

(6)五子棋游戏结束界面

 

 

图7游戏结束界面

 

图8游戏结束界面

 

 

实践总结

通过这次课程设计,我进一步加深对基础理论的理解,扩大专业知识面,对收集资料、查阅文献、方案制定等实践方面得到了很好的锻练,促进对所学知识应用能力的提高。同时我渐渐的复习了Java使用方法和编程语法,之后的编程过程也相对得心应手,基本完成了预期计划的要求。在此感谢我的指导老师兼班主任—赵培英老师,她严谨的治学态度深深的影响了我,使我受益匪浅!

 

                                               

 

 

 

 

 

 

 

 

 

题目     五子棋运算设计程序    

https://github.com/guoxiaox/test/tree/master

 

 

 

 

学校:齐鲁工业大学   

学院:信息学院       

班级:计科(高职)13-3

学号: 201303014100  

姓名:郭笑笑         

 

 

 

 

 

 

 

 

 

 

 

 

五子棋运算设计程序

项目设计方案

1界面棋盘设计

在对棋盘界面设计方面要考虑简洁友好,符合游戏者需求。棋子的设计方面系统中设置了两种棋子颜色,white或者black, 游戏者可自行选择。棋子怎样画出来,怎样使棋子按我们所想的方式来绘制出来是设计的主要难题。运行时要求当每次点击鼠标的时候就在点击鼠标的地方画一个棋子,所以得定义一个棋子的类使点击鼠标时系统把棋子绘制出来。这样主界面里的棋子就确定了,而且也确定了几个所需要的类。可以先定义好这些类了。

有了以上的准备就可以把棋盘绘制出来了,下棋时每次在适当位置点击鼠标的时候就在所点击的位置上画出你这个角色的颜色,然后计算机会自动的下棋,计算机自动下棋要有一个控制变量来控制能不能下棋。人可以随意的下子,可是计算机就要计算在哪个位置下子了。这里由计算机下子的算法来确定,人人对战的话就不象单机游戏一样,要计算机来判断人下子之后计算机下那一个地方了,人人对战只要在双方下子的时候判断赢棋情况,当然还要同步的控制两个人下棋的顺序,这样的话就要用到线程了,各自都继承或实现线程类或接口,以便各自能随时单独控制接发消息。

2. 总体设计

为实现五子棋运算测试程序设计需求,需要实现的FiveChessAppletDemo类、ChessFrame类、ChessModel类、MainPanel类、ChessWindowEvent类等功能。

3.类组合结构图

 

给出各个类之间的整体组合结构

 

 

 
   

 

 

 

 

 

 

 

 


                           

图1所示

核心算法详细设计

各个类的简要描述(在整个软件中的地位及实现功能的简要描述,其继承层次关系及所实现的接口等);详细设计每个类的成员变量、构造方法与方法;并根据详细设计给出每个类的实现代码。示例如下:

(1)FiveChessAppletDemo

main方法创建了ChessFrame类的一个实例对象(cf),并启动屏幕显示显示该实例对象。定义了一个名为 ChessFrame 的监听类实现 ActionListener

(2)ChessFrame

       创建五子棋游戏主窗体和菜单

 

(3)ChessModel

    实现了整个五子棋程序算法的核心

    1、规定棋盘的宽度、高度、棋盘的模式

       private int width,height,modeChess;

    2、规定棋盘方格的横向、纵向坐标

       private int x=0,y=0;

    3、棋盘方格的横向、纵向坐标所对应的棋子颜色,

       数组arrMapShow只有3个值:1,2,3,-1,

       其中1代表该棋盘方格上下的棋子为黑子,

       2代表该棋盘方格上下的棋子为白子,

       3代表为该棋盘方格上没有棋子,

       -1代表该棋盘方格不能够下棋子

       private int[][] arrMapShow;

     4、交换棋手的标识,棋盘方格上是否有棋子的标识符

       private boolean isOdd,isExist;

 

(4)MainPanel

    1、构建一个面板,在该面板上画上棋盘;

    2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)

(5)ChessWindowEvent类

     响应退出窗口

 

源代码

Java编程:五子棋游戏源代码

import java.awt.*;                                                     

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

import java.io.PrintStream;

import javax.swing.JComponent;

import javax.swing.JPanel;

 

/*

*main方法创建了ChessFrame类的一个实例对象(cf),

*并启动屏幕显示显示该实例对象。

**/

public class FiveChessAppletDemo {

public static void main(String args[]){

   ChessFrame cf = new ChessFrame();

   cf.show();

}

}

 

/*

*类ChessFrame主要功能是创建五子棋游戏主窗体和菜单

**/

class ChessFrame extends JFrame implements ActionListener {

private String[] strsize={"20x15","30x20","40x30"};

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(20,15);

   JMenuBar mbar = new JMenuBar();

   this.setJMenuBar(mbar);

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

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

    "开局", "棋盘","模式", null, "退出"

    }, this));

   JMenu lookMenu =new JMenu("视图");

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

    "Metal","Motif","Windows"

    },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 * 20+50 , h * 20+100 );

    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 >= 1){

       cm = new ChessModel(modeChess);

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

    }else{

       System.out.println("\u81EA\u5B9A\u4E49");

    }

    }

   

    public void actionPerformed(ActionEvent e){

    String arg=e.getActionCommand();

    try{

       if (arg.equals("Windows"))

        UIManager.setLookAndFeel(

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

       else if(arg.equals("Motif"))

     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("20x15")){

    this.width=20;

    this.height=15;

    cm=new ChessModel(1);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("30x20")){

    this.width=30;

    this.height=20;

    cm=new ChessModel(2);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("40x30")){

    this.width=40;

    this.height=30;

    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("关于"))

    JOptionPane.showMessageDialog(this, "五子棋游戏测试版本", "关于", 0);

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

    System.exit(0);

}

}

 

/*

*类ChessModel实现了整个五子棋程序算法的核心

*/

class ChessModel {

//棋盘的宽度、高度、棋盘的模式(如20×15)

private int width,height,modeChess;

//棋盘方格的横向、纵向坐标

private int x=0,y=0;

//棋盘方格的横向、纵向坐标所对应的棋子颜色,

//数组arrMapShow只有3个值:1,2,3,-5,

//其中1代表该棋盘方格上下的棋子为黑子,

//2代表该棋盘方格上下的棋子为白子,

//3代表为该棋盘方格上没有棋子,

//-5代表该棋盘方格不能够下棋子

private int[][] arrMapShow;

//交换棋手的标识,棋盘方格上是否有棋子的标识符

private boolean isOdd,isExist;

 

public ChessModel() {}

 

//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘

public ChessModel(int modeChess){

   this.isOdd=true;

   if(modeChess == 1){

    PanelInit(20, 15, modeChess);

   }

   if(modeChess == 2){

    PanelInit(30, 20, modeChess);

   }

   if(modeChess == 3){

    PanelInit(40, 30, 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] = -5;

    }

   }

}

 

//获取是否交换棋手的标识符

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;

   //judge right

   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;

   }

   //judge left

   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;

 

   //judge up

   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;

   }

   //judge down

   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;

 

   //judge left_up

   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;

   }

   //judge right_down

   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;

 

   //judge right_up

   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;

   }

   //judge left_down

   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,"你赢了,好厉害!","win",

    JOptionPane.INFORMATION_MESSAGE);

}

 

//输棋后的提示

public void showDefeat(JPanel jp){

   JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost",

    JOptionPane.INFORMATION_MESSAGE);

}

}

 

/*

*类MainPanel主要完成如下功能:

*1、构建一个面板,在该面板上画上棋盘;

*2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)

**/

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();

}

 

//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),

//然后调用draw方法在棋盘上画出相应的棋子

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.white);

    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);

   }

   //画白色棋子

   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);

   }

   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();

}

}

 

class ChessWindowEvent extends WindowAdapter{

public void windowClosing(WindowEvent e){

   System.exit(0);

}

 

ChessWindowEvent()

 

{

}

}

 

                                                    

 

 

                                                     

 

使用说明与运行结果截图

 (1)五子棋游戏的主界面

 

 

 

 

 

 

(5)单击“帮助”栏中的“关于”选项时弹出的界面

 

 

 

 

 

(6)五子棋游戏结束界面

 

 

 

 

 

实践总结

通过这次课程设计,我进一步加深对基础理论的理解,扩大专业知识面,对收集资料、查阅文献、方案制定等实践方面得到了很好的锻练,促进对所学知识应用能力的提高。同时我渐渐的复习了Java使用方法和编程语法,之后的编程过程也相对得心应手,基本完成了预期计划的要求。在此感谢我的指导老师兼班主任—赵培英老师,她严谨的治学态度深深的影响了我,使我受益匪浅!

 

                                               

 

 

 

 

 

 

 

 

 

题目     五子棋运算设计程序    

https://github.com/guoxiaox/test/tree/master

 

 

 

 

学校:齐鲁工业大学   

学院:信息学院       

班级:计科(高职)13-3

学号: 201303014100  

姓名:郭笑笑         

 

 

 

 

 

 

 

 

 

 

 

 

五子棋运算设计程序

项目设计方案

1界面棋盘设计

在对棋盘界面设计方面要考虑简洁友好,符合游戏者需求。棋子的设计方面系统中设置了两种棋子颜色,white或者black, 游戏者可自行选择。棋子怎样画出来,怎样使棋子按我们所想的方式来绘制出来是设计的主要难题。运行时要求当每次点击鼠标的时候就在点击鼠标的地方画一个棋子,所以得定义一个棋子的类使点击鼠标时系统把棋子绘制出来。这样主界面里的棋子就确定了,而且也确定了几个所需要的类。可以先定义好这些类了。

有了以上的准备就可以把棋盘绘制出来了,下棋时每次在适当位置点击鼠标的时候就在所点击的位置上画出你这个角色的颜色,然后计算机会自动的下棋,计算机自动下棋要有一个控制变量来控制能不能下棋。人可以随意的下子,可是计算机就要计算在哪个位置下子了。这里由计算机下子的算法来确定,人人对战的话就不象单机游戏一样,要计算机来判断人下子之后计算机下那一个地方了,人人对战只要在双方下子的时候判断赢棋情况,当然还要同步的控制两个人下棋的顺序,这样的话就要用到线程了,各自都继承或实现线程类或接口,以便各自能随时单独控制接发消息。

2. 总体设计

为实现五子棋运算测试程序设计需求,需要实现的FiveChessAppletDemo类、ChessFrame类、ChessModel类、MainPanel类、ChessWindowEvent类等功能。

3.类组合结构图

 

给出各个类之间的整体组合结构

 

 

 
   

 

 

 

 

 

 

 

 


                           

图1所示

核心算法详细设计

各个类的简要描述(在整个软件中的地位及实现功能的简要描述,其继承层次关系及所实现的接口等);详细设计每个类的成员变量、构造方法与方法;并根据详细设计给出每个类的实现代码。示例如下:

(1)FiveChessAppletDemo

main方法创建了ChessFrame类的一个实例对象(cf),并启动屏幕显示显示该实例对象。定义了一个名为 ChessFrame 的监听类实现 ActionListener

(2)ChessFrame

       创建五子棋游戏主窗体和菜单

 

(3)ChessModel

    实现了整个五子棋程序算法的核心

    1、规定棋盘的宽度、高度、棋盘的模式

       private int width,height,modeChess;

    2、规定棋盘方格的横向、纵向坐标

       private int x=0,y=0;

    3、棋盘方格的横向、纵向坐标所对应的棋子颜色,

       数组arrMapShow只有3个值:1,2,3,-1,

       其中1代表该棋盘方格上下的棋子为黑子,

       2代表该棋盘方格上下的棋子为白子,

       3代表为该棋盘方格上没有棋子,

       -1代表该棋盘方格不能够下棋子

       private int[][] arrMapShow;

     4、交换棋手的标识,棋盘方格上是否有棋子的标识符

       private boolean isOdd,isExist;

 

(4)MainPanel

    1、构建一个面板,在该面板上画上棋盘;

    2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)

(5)ChessWindowEvent类

     响应退出窗口

 

源代码

Java编程:五子棋游戏源代码

import java.awt.*;                                                     

import java.awt.event.*;

import java.applet.*;

import javax.swing.*;

import java.io.PrintStream;

import javax.swing.JComponent;

import javax.swing.JPanel;

 

/*

*main方法创建了ChessFrame类的一个实例对象(cf),

*并启动屏幕显示显示该实例对象。

**/

public class FiveChessAppletDemo {

public static void main(String args[]){

   ChessFrame cf = new ChessFrame();

   cf.show();

}

}

 

/*

*类ChessFrame主要功能是创建五子棋游戏主窗体和菜单

**/

class ChessFrame extends JFrame implements ActionListener {

private String[] strsize={"20x15","30x20","40x30"};

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(20,15);

   JMenuBar mbar = new JMenuBar();

   this.setJMenuBar(mbar);

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

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

    "开局", "棋盘","模式", null, "退出"

    }, this));

   JMenu lookMenu =new JMenu("视图");

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

    "Metal","Motif","Windows"

    },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 * 20+50 , h * 20+100 );

    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 >= 1){

       cm = new ChessModel(modeChess);

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

    }else{

       System.out.println("\u81EA\u5B9A\u4E49");

    }

    }

   

    public void actionPerformed(ActionEvent e){

    String arg=e.getActionCommand();

    try{

       if (arg.equals("Windows"))

        UIManager.setLookAndFeel(

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

       else if(arg.equals("Motif"))

     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("20x15")){

    this.width=20;

    this.height=15;

    cm=new ChessModel(1);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("30x20")){

    this.width=30;

    this.height=20;

    cm=new ChessModel(2);

    MapSize(this.width,this.height);

    SwingUtilities.updateComponentTreeUI(this);

   }

   if(arg.equals("40x30")){

    this.width=40;

    this.height=30;

    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("关于"))

    JOptionPane.showMessageDialog(this, "五子棋游戏测试版本", "关于", 0);

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

    System.exit(0);

}

}

 

/*

*类ChessModel实现了整个五子棋程序算法的核心

*/

class ChessModel {

//棋盘的宽度、高度、棋盘的模式(如20×15)

private int width,height,modeChess;

//棋盘方格的横向、纵向坐标

private int x=0,y=0;

//棋盘方格的横向、纵向坐标所对应的棋子颜色,

//数组arrMapShow只有3个值:1,2,3,-5,

//其中1代表该棋盘方格上下的棋子为黑子,

//2代表该棋盘方格上下的棋子为白子,

//3代表为该棋盘方格上没有棋子,

//-5代表该棋盘方格不能够下棋子

private int[][] arrMapShow;

//交换棋手的标识,棋盘方格上是否有棋子的标识符

private boolean isOdd,isExist;

 

public ChessModel() {}

 

//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘

public ChessModel(int modeChess){

   this.isOdd=true;

   if(modeChess == 1){

    PanelInit(20, 15, modeChess);

   }

   if(modeChess == 2){

    PanelInit(30, 20, modeChess);

   }

   if(modeChess == 3){

    PanelInit(40, 30, 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] = -5;

    }

   }

}

 

//获取是否交换棋手的标识符

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;

   //judge right

   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;

   }

   //judge left

   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;

 

   //judge up

   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;

   }

   //judge down

   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;

 

   //judge left_up

   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;

   }

   //judge right_down

   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;

 

   //judge right_up

   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;

   }

   //judge left_down

   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,"你赢了,好厉害!","win",

    JOptionPane.INFORMATION_MESSAGE);

}

 

//输棋后的提示

public void showDefeat(JPanel jp){

   JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost",

    JOptionPane.INFORMATION_MESSAGE);

}

}

 

/*

*类MainPanel主要完成如下功能:

*1、构建一个面板,在该面板上画上棋盘;

*2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)

**/

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();

}

 

//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),

//然后调用draw方法在棋盘上画出相应的棋子

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.white);

    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);

   }

   //画白色棋子

   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);

   }

   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();

}

}

 

class ChessWindowEvent extends WindowAdapter{

public void windowClosing(WindowEvent e){

   System.exit(0);

}

 

ChessWindowEvent()

 

{

}

}

 

                                                    

 

 

                                                     

 

使用说明与运行结果截图

 (1)五子棋游戏的主界面

 

图2程序主界面

 

 

 

图3“游戏”栏中的选项

 

图4视图中的各个选项

 

 

图5帮助栏中的选项

 

(5)单击“帮助”栏中的“关于”选项时弹出的界面

 

 

 

 

图6关于选项弹出的界面

 

(6)五子棋游戏结束界面

 

 

图7游戏结束界面

 

图8游戏结束界面

 

 

实践总结

通过这次课程设计,我进一步加深对基础理论的理解,扩大专业知识面,对收集资料、查阅文献、方案制定等实践方面得到了很好的锻练,促进对所学知识应用能力的提高。同时我渐渐的复习了Java使用方法和编程语法,之后的编程过程也相对得心应手,基本完成了预期计划的要求。在此感谢我的指导老师兼班主任—赵培英老师,她严谨的治学态度深深的影响了我,使我受益匪浅!

 

                                               

 

 

 

 

 

posted on 2015-04-30 13:55  郭笑笑  阅读(269)  评论(3编辑  收藏  举报