JAVA五子棋

时间:2024.4.7

JAVA课程设计

设计题目:五子棋游戏

一.简要的介绍五子棋

1. 五子棋的起源

五子棋,又被称为“连五子、五子连、串珠、五目、五目碰、五格、五石、五法、五联、京棋”。五子棋相传起源于四千多年前的尧帝时期,比围棋的历史还要悠久,可能早在“尧造围棋”之前,民间就已有五子棋游戏。有关早期五子棋的文史资料与围棋有相似之处,因为古代五子棋的棋具与围棋是完全相同的。

 

2.现在五子棋标准棋盘(如图所示)

3.五子棋的棋子

五子棋采用两种颜色棋子,黑色棋子和白色棋子,和围棋相同,

4.五子棋规则

五子棋就是五个棋子连在一起就算赢,黑棋先行,下棋下在棋盘交叉线上,由于黑棋先行,优势太大,所以对黑棋设了禁手,又规定了“三手交换”,

就是黑棋下第 2 手棋,盘面第 3 着棋之后,白方在应白 2 之前,如感觉黑方棋形不利于己方,可出交换,即执白棋一方变为执黑棋一方。和“五手两打法”,就是黑棋在下盘面上关键的第 5 手时,必须下两步棋,让白方在这两步棋中任选一步,然后再续下。不过一般爱好者不需要遵循这么多规则。

二.程序流程

                      

三.代码设计与分析

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

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

publicclass FiveChessAppletDemo {

publicstaticvoid main(String args[]){

      ChessFrame cf = new ChessFrame();

      cf.show();

}

}

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

import java.awt.*;

import java.awt.event.*;

importjava.applet.*;

import javax.swing.*;

importjava.io.PrintStream;

importjavax.swing.JComponent;

importjavax.swing.JPanel;

classChessFrameextends JFrame implements ActionListener {

private String[] strsize={"标准棋盘","改进棋盘","扩大棋盘"};

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

publicstaticbooleaniscomputer=true,checkcomputer=true;

privateint 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, "退出"

    }, 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;

   elseif(parent instanceof String)

    m = new JMenu((String)parent);

   else

    returnnull;

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

    if(items[i] == null)

     m.addSeparator();

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

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

   elseif(item instanceof JMenuItem)

    r = (JMenuItem)item;

   else

    returnnull;

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

    elseif(item instanceof JRadioButtonMenuItem)

       r = (JRadioButtonMenuItem)item;

    else

       returnnull;

    if(target instanceof ActionListener)

       r.addActionListener((ActionListener)target);

    return r;

    }

   

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

    }

   

    publicboolean getiscomputer(){

    returnthis.iscomputer;

    }

   

    publicvoid restart(){

    int modeChess = cm.getModeChess();

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

       cm = new ChessModel(modeChess);

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

    }

    }

   

    publicvoid actionPerformed(ActionEvent e){

    String arg=e.getActionCommand();

    try{

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

        UIManager.setLookAndFeel(

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

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

    JOptionPane.showMessageDialog(null, "第一版", "版本",JOptionPane.PLAIN_MESSAGE );

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

    System.exit(0);

}

}

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

importjava.awt.*;

importjava.awt.event.*;

importjava.applet.*;

import javax.swing.*;

importjava.io.PrintStream;

importjavax.swing.JComponent;

import javax.swing.JPanel;

class ChessModel {

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

privateint width,height,modeChess;

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

privateint x=0,y=0;

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

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

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

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

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

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

privateint[][] arrMapShow;

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

privateboolean isOdd,isExist;

public ChessModel() {}

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

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

   }

}

按照棋盘模式构建棋盘大小

privatevoid PanelInit(int width, int height, int modeChess){

   this.width = width;

   this.height = height;

   this.modeChess = modeChess;

   arrMapShow = newint[width+1][height+1];

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

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

     arrMapShow[i][j] = -1;

    }

   }

}

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

publicboolean getisOdd(){

   returnthis.isOdd;

}

设置交换棋手的标识符

publicvoid setisOdd(boolean isodd){

   if(isodd)

    this.isOdd=true;

   else

    this.isOdd=false;

}

获取某棋盘方格是否有棋子的标识值

publicboolean getisExist(){

   returnthis.isExist;

}

获取棋盘宽度

publicint getWidth(){

   returnthis.width;

}

获取棋盘高度

publicint getHeight(){

   returnthis.height;

}

获取棋盘模式

publicint getModeChess(){

   returnthis.modeChess;

}

获取棋盘方格上棋子的信息

publicint[][] getarrMapShow(){

   return arrMapShow;

}

判断下子的横向、纵向坐标是否越界

privateboolean badxy(int x, int y){

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

    returntrue;

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

}

计算棋盘上某一方格上八个方向棋子的最大值,

这八个方向分别是:左、右、上、下、左上、左下、右上、右下

publicboolean chessExist(int i,int j){

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

    returntrue;

   returnfalse;

}

判断该坐标位置是否可下棋子

publicvoid readyplay(int x,int y){

   if(badxy(x,y))

    return;

   if (chessExist(x,y))

    return;

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

}

在该坐标位置下棋子

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

   }

}

计算机走棋

说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数,

最后得出棋子数最大值的坐标,下子

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

}

记录电脑下子后的横向坐标

publicvoid setX(int x){

   this.x=x;

}

记录电脑下子后的纵向坐标

publicvoid setY(int y){

   this.y=y;

}

获取电脑下子的横向坐标

publicint getX(){

   returnthis.x;

}

获取电脑下子的纵向坐标

publicint getY(){

   returnthis.y;

}

计算棋盘上某一方格上八个方向棋子的最大值,

这八个方向分别是:左、右、上、下、左上、左下、右上、右下

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

}

判断胜负

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

    returntrue;

   判断上方胜负

   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)

    returntrue;

   判断左上胜负

   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)

    returntrue;

    判断右上胜负

   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)

    returntrue;

   returnfalse;

}

赢棋后的提示

publicvoid showSuccess(JPanel jp){

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

    JOptionPane.INFORMATION_MESSAGE);

}

输棋后的提示

publicvoid showDefeat(JPanel jp){

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

    JOptionPane.INFORMATION_MESSAGE);

}

}

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

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

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

import java.awt.*;

import java.awt.event.*;

importjava.applet.*;

importjavax.swing.*;

importjava.io.PrintStream;

importjavax.swing.JComponent;

import javax.swing.JPanel;

classMainPanelextends JPanel

implements MouseListener,MouseMotionListener{

设定棋盘的宽度和高度

privateint width,height;

private ChessModel cm;

根据棋盘模式设定面板的大小

MainPanel(ChessModel mm){

   cm=mm;

   width=cm.getWidth();

   height=cm.getHeight();

   addMouseListener(this);

}

   

   根据棋盘模式设定棋盘的宽度和高度

publicvoid setModel(ChessModel mm){

   cm = mm;

   width = cm.getWidth();

   height = cm.getHeight();

}

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

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

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

    }

   }

}

根据提供的棋子信息(颜色、坐标)画棋子

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

   }

    画白色棋子

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

   }

}

响应鼠标的点击事件,根据鼠标的点击来下棋,

根据下棋判断胜负等

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

     }

    }

   }

}

publicvoid mouseClicked(MouseEvent evt){}

publicvoid mouseReleased(MouseEvent evt){}

publicvoid mouseEntered(MouseEvent mouseevt){}

publicvoid mouseExited(MouseEvent mouseevent){}

publicvoid mouseDragged(MouseEvent evt){}

     响应鼠标的拖动事件

publicvoid mouseMoved(MouseEvent moveevt){

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

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

   cm.readyplay(x,y);

   repaint();

}

}

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

响应退出窗口

class ChessWindowEvent extends WindowAdapter{

publicvoid windowClosing(WindowEvent e){

   System.exit(0);

}

ChessWindowEvent()

{

}

}

四.程序调试与运行

  

运行:

标准棋盘

改进棋盘:

扩大棋盘:

外观类型二:

外观类型三:

人机对战:

结果:

五.结论

  通过对五子棋游戏的编写,使自己对java语言有了更深的了解。也更加熟悉和了解了java开发工具Eclipse的使用。不过还有很多不足之处,比如没有能设置禁手,没有能设置悔棋,还有很多东西可以扩充完善。

完整源代码:

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;

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, "退出"

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

    JOptionPane.showMessageDialog(null, " 第一版 ", "版本",JOptionPane.PLAIN_MESSAGE );

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

    System.exit(0);

}

}

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;

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

}

}

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

class ChessWindowEvent extends WindowAdapter{

public void windowClosing(WindowEvent e){

   System.exit(0);

}

ChessWindowEvent()

{

}

}

public class FiveChessAppletDemo {

public static void main(String args[]){

   ChessFrame cf = new ChessFrame();

   cf.show();

}

}

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;

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

   }

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

}

}

更多相关推荐:
五子棋总结t Word 文档

二(1)班五子棋兴趣小组活动总结在这个学期里,我们班开展五子棋兴趣小组活动,提高了学生活动兴趣,增长学生棋艺水平,陶冶学生的性情。现将本小组的活动情况总结如下:一、基本情况参加本学期五子棋兴趣小组活动的同学是二…

五子棋总结

小浣熊五子棋活动总结五子棋可以培养学生的爱国情感推广体育文化知识开发逻辑思维能力提升心理素质通过学习必将对学生拥有自信的人生起到积极的积淀作用五子棋能促进学生的品行智力审美及身心等综合素质的和谐发展五子棋起源于...

五子棋兴趣小组活动总结

五子棋兴趣小组活动总结在这个学期里,我们五子棋兴趣小组的活动提高了学生活动兴趣,增长学生棋艺水平,陶冶学生的性情。现将本小组的活动情况总结如下:一、基本情况参加本学期五子棋兴趣小组活动的同学分别来自四年级一、二…

6、五子棋比赛总结

五子棋比赛总结为了丰富我校课余文化和学生们的课余生活,宣传中华传统文化和棋类文化,促进棋类爱好者之间的交流,提高我系学生的综合素质,我们女生部特此举办了五子棋比赛,于20xx年x月x日圆满落下帷幕。这次比赛经过…

五子棋兴趣小组活动年度总结

五子棋兴趣小组活动半年总结时光如梭,转眼一个学期已结束。这是我校组建五子棋兴趣小组的第一个年头。在这个学期里,我们五子棋兴趣小组的活动提高学生活动兴趣,增长学生棋艺水平,陶冶学生的性情。现将本小组的活动情况总结…

五子棋比赛总结

五子棋比赛总结五子棋在校园和社会比较普及,但却始终得不到推广,因为在大多数人心中,五子棋只是简简单单的五颗棋子连在一起,没有太大的技术和挑战而言。但是五子棋的阵法,技巧性,逻辑性绝不比象棋和围棋要少,甚至,五子…

五子棋棋展活动总结

湖北科技职业学院五子棋棋展活动总结20xx年12月3日湖北科技职业学院子间风云棋牌社举行的学院大型五子棋展示活动顺利结束了本次活动以向全院师生展示五子棋的开局布局为目的传承五子棋文化这次活动吸引了大量师生前来参...

院校五子棋比赛工作总结

院校棋牌协会五子棋比赛工作总结春去春来花谢花开伴随着阳春三月的脚步声新的学年开始了我院校棋牌协会在此期间积极开展了五子棋比赛为同学们的课余生活学习提供了一个展示自我的平台是同学们在此活动中陶冶情操增进友谊同时思...

五子棋游戏开发总结(C语言版)

五子棋游戏开发总结一五子棋游戏概述略二游戏功能对弈游戏自动判断游戏胜负采用人机对话模式界面设计美观友好具体玩法采用任意键开始游戏此处采用键盘值WASD控制棋子移动方向空格键SPACE落子ESC退出游戏三系统开发...

朱世鹏五子棋选修课教学总结

20xx至20xx学年第一学期五子棋选修课教学工作总结时光如梭转眼20xx年至20xx年学年度第一学期已过去了在这个学期里我担任五子棋选修课的教学工作五子棋选修课可以提高学生活动兴趣增长学生棋艺水平陶冶学生的性...

五子棋兴趣小组活动小结

五子棋兴趣小组活动总结五子棋兴趣小组开展有一段时间了我们的这个兴趣小组共有七至八年级的12人参加人数不多这得益于五子棋的简单易学这些人中其中有两名是参加过五子棋比赛并获奖的但更多的是对五子棋一知半解却自信得不得...

五子棋比赛总结

航空与旅游学院五子棋比赛总结航空与旅游学院五子棋比赛游戏规则一裁判安排1总裁判长齐广立2裁判长陈雪浩秦娜3裁判员梅志伟张思悦王亚婷刘婷婷李静张敏金晶玉崔心怡4总协调孟帅魏培森张凤棋5计分统计秘书处二比赛规则1分...

五子棋总结(14篇)