关于五子棋

来源:互联网 发布:mac win7 虚拟机 编辑:程序博客网 时间:2024/05/17 01:22

刚刚在论坛中看到一个五子棋的源代码,不过不明白,有没有那位高手能帮忙解释下??我正要打算学JAVA ,呵呵,主要是想知道该代码的布局和思路。小女子在此多谢啦!!

下面是代码

import   java.awt.*;
import   java.awt.event.*;
import   javax.swing.*;
import   javax.swing.border.*;
public   class   WUZIQI   extends   JFrame
{
int   i=0;
Font   ft=new   Font( "华文新魏 ",Font.PLAIN,15);
JButton[][]btn=new   JButton[30][30];
int[][]color=new   int[30][30];
boolean[][]a=new   boolean[30][30];
JPanel[][]jpsz=new   JPanel[30][30];
JScrollPane   pane=new   JScrollPane();
public   WUZIQI()
{
Container   con=getContentPane();
setBounds(100,50,798,628);
setTitle( "五子棋 ");
setResizable(false);
con.setBackground(Color.orange);
JMenuBar   a1=new   JMenuBar();
                JMenu   b1=new   JMenu( "游戏 ");
JMenu   b2=new   JMenu( "帮助 ");
a1.add(b1);
a1.add(b2);
JMenuItem   c1=new   JMenuItem( "开局 ");
//c1.addActionListener(new   LL1());
c1.addActionListener(new   nnn12());
JMenuItem   c2=new   JMenuItem( "暂停/继续 ");
c2.addActionListener(new   mm1());
JMenuItem   c3=new   JMenuItem( "重玩 ");
c3.addActionListener(new   nnn12());
JMenuItem   c4=new   JMenuItem( "撤销 ");
JMenuItem   c5=new   JMenuItem( "退出 ");
c5.addActionListener(new   tuichu());
b1.add(c1);
b1.add(c2);
b1.add(c3);
b1.add(c4);
b1.add(c5);
        JMenuItem   c9=new   JMenuItem( "帮助主题 ");
        c9.addActionListener(new   mm12());
JMenuItem   c10=new   JMenuItem( "使用帮助 ");
c10.addActionListener(new   mmm());
JMenuItem   c11=new   JMenuItem( "关于五子棋 ");
c11.addActionListener(new   mm());
b2.add(c9);
b2.add(c10);
b2.add(c11);
con.add(a1,BorderLayout.NORTH);
pane.setBounds(0,10,593,632);
con.add(pane);
JPanel   jp=new   JPanel();
jp.setBounds(1,31,799,590);
jp.setBackground(Color.green);
con.add(jp);
jp.setLayout(new   GridLayout(22,22));
for(int   i=0;i <22;i++)
        {
for(int   j=0;j <22;j++)
{
jpsz[j]=new   JPanel();
}
}
for(int   i=0;i <22;i++)
{
for(int   j=0;j <22;j++)
{
}
}
for(int   i=0;i <22;i++)
{
for(int   j=0;j <22;j++)
{
btn[j]=new   JButton( " ");
color[j]=0;
}
}
for(int   i=0;i <22;i++)
{
for(int   j=0;j <22;j++)
{
btn[j].setSize(8,8);
btn[j].setBorder(BorderFactory.   createEtchedBorder());
jp.add(btn[j]);
btn[j].setMargin(new   Insets(0,0,0,0));
btn[j].addMouseListener(new   mouse());
}
}
con.add(pane);
show();
}
public   static   void   main(String[]args)
{
new   WUZIQI();
}
class   mouse   implements   MouseListener
{
int   j=0;
int   k=0;
int   l=0;
winestimate   kkk=new   winestimate();
public   void     mouseClicked(MouseEvent   e)
      {
((JButton)(e.getSource())).setBorder(BorderFactory.createRaisedBevelBorder());
        i=i+1;

if(a[k][l]==false)
{
if(j!=1)
{
      if(i%2==0)
      {
      i=2;
      ((JButton)(e.getSource())).setBackground(Color.white);

      j=1;
      a[k][l]=true;
      color[k][l]=-1;
kkk.ads();
kkk.asd();
kkk.xiexiang();
}
else
{
i=1;
((JButton)(e.getSource())).setBackground(Color.black);
j=1;
      a[k][l]=true;
      color[k][l]=1;
kkk.asd();
kkk.ads();
kkk.xiexiang();
}
}

}
}
public   void   mouseEntered(MouseEvent   e)
      {
}
public   void     mouseExited(MouseEvent   e)
      {
}
public   void   mousePressed(MouseEvent   e)
      {
for(int   m=0;m <22;m++)
{
for(int   n=0;n <22;n++)
{
if((e.getSource())==btn[m][n])
{
k=m;
l=n;
}
}
}
}
public   void   mouseReleased(MouseEvent   e)
{
}
}
class   winestimate
{
public   winestimate()
{
}
public   void   xiexiang()
{
for(int   k=0;k <=17;k++)
{
for(int   i=0;i <=17;i++)
{
int   w=color[k]+color[k+1][i+1]+color[k+2][i+2]+color[k+3][i+3]+color[k+4][i+4];
if(w> =5)
{
JOptionPane.   showMessageDialog(null,   "黑方获胜! ");
    w=0;
    break;
}
else   if(w <=-5)
{
    JOptionPane.   showMessageDialog(null,   "白方获胜! ");
    w=0;
    break;
}
w=0;
int   o=i;
int   p=k+5;
int   q=color[o][p]+color[o+1][p-1]+color[o+2][p-2]+color[o+3][p-3]+color[o+4][p-4];
if(q> =5)
{
JOptionPane.   showMessageDialog(null,   "黑方获胜! ");
    q=0;
    break;
}
else   if(q <=-5)
{
    JOptionPane.   showMessageDialog(null,   "白方获胜! ");
  q=0;
    break;
}
q=0;
}
}
}
public   void   asd()
{
int   m=0;
int   n=0;
for(int   k=0;k <=17;k++)
{
for(int   i=0;i <=17;i++)
{
for(int   j=i;j <=i+5;j++)
{

    if(color[k][j]==1)
    {
    m++;
    if(m> =5)
    {
  JOptionPane.   showMessageDialog(null,   "黑方获胜! ");
    m=0;
    break;
    }
    }
    else   if(color[k][j]==-1)
    {
    n++;
    if(n> =5)
    {
        JOptionPane.   showMessageDialog(null,   "白方获胜! ");
          n=0;
          break;
    }

    }
}
m=0;
n=0;

}
}
}

public   void   ads()
{
int   n=0;
int   m=0;
for(int   k=0;k <=17;k++)
{
for(int   i=0;i <=17;i++)
{
for(int   j=i;j <=i+5;j++)
{

    if(color[j][k]==-1)
    {
    n++;
    if(n> =5)
{

    JOptionPane.   showMessageDialog(null,   "白方获胜! ");
n=0;
break;
}
    }
    else   if(color[j][k]==1)
    {
    m++;
    if(m> =5)
    {
    JOptionPane.   showMessageDialog(null,   "黑方获胜! ");
    m=0;
    break;
    }
    }
}
m=0;
n=0;

}
}
}
}
}


class   myact   implements   ActionListener
{
int   z=0;
public   void   actionPerformed(ActionEvent   e)
{
z++;
if(z%2==1)
{
    ((JButton)(e.getSource())).setBackground(Color.white);
          }
else
{
            ((JButton)(e.getSource())).setBackground(Color.black);
          }
          ((JButton)(e.getSource())).setEnabled(false);
                  }
        }
class   ss   implements   ActionListener//------------------------------------声音事件
{
public   void   actionPerformed(ActionEvent   ae)
{
Toolkit.getDefaultToolkit().beep();
}
}
class   mmm   implements   ActionListener
{
public   void   actionPerformed(ActionEvent   ae)
{
new   FF();
}
}
class   FF   extends   JFrame//--------------------------------------------------目录子窗体
{
JButton   j=new   JButton( "确定 ");
public   FF()
{
Container   con1=getContentPane();
con1.setLayout(null);
setTitle( "目录 ");
setBounds(310,310,400,250);
setResizable(false);
j.setBounds(160,180,70,30);
JLabel   a8=new   JLabel( "   苯!五子棋还不会下! ");
a8.setFont(new   Font( "宋体 ",Font.BOLD+Font.ITALIC,   24));
a8.setBounds(60,30,270,80);
con1.add(j);
                con1.add(a8);
j.addActionListener(new   nnn());
show();
}
class   nnn   implements   ActionListener
{
public   void   actionPerformed(ActionEvent   ae)
{
hide();
}
}
}
class   tuichu   implements   ActionListener//---------------------------------退出事件
{
public   void   actionPerformed(ActionEvent   ae)
{
System.exit(0);
}
}
class   mm1   implements   ActionListener//------------------------------------菜单开始事件
{
int   k=3;
public   void   actionPerformed(ActionEvent   e)
{
k++;
if(k%2==1)
{
((JButton)(e.getSource())).setEnabled(true);
}
else
{
((JButton)(e.getSource())).setEnabled(false);
}
}
}
class   nnn12   implements   ActionListener//-----------------------------------菜单重玩事件
{
public   void   actionPerformed(ActionEvent   ae)
{

new   WUZIQI();
}
}
class   mm12   implements   ActionListener
{
public   void   actionPerformed(ActionEvent   ae)
{
new   FF3();
}
}
class   FF3   extends   JFrame//--------------------------------------------------使用帮助子窗体
{
JButton   j=new   JButton( "确定 ");
public   FF3()
{
Container   con2=getContentPane();
con2.setLayout(null);
setTitle( "使用帮助 ");
setBounds(310,310,400,250);
setResizable(false);
j.setBounds(160,180,70,30);
JLabel   QQ=new   JLabel( "只要五颗相同的按钮 ");
JLabel   WW=new   JLabel( "   组成一排就胜利! ");
QQ.setFont(new   Font( "宋体 ",Font.BOLD+Font.ITALIC,   24));
WW.setFont(new   Font( "宋体 ",Font.BOLD+Font.ITALIC,   24));
QQ.setBounds(60,30,270,80);
WW.setBounds(60,60,270,80);
con2.add(j);
                con2.add(QQ);
                con2.add(WW);
j.addActionListener(new   LL1());
show();
}//------------------------------------------------------------------------

class   LL1   implements   ActionListener
{
public   void   actionPerformed(ActionEvent   ae)
{
hide();
}
        }
}
class   mm   implements   ActionListener
{
public   void   actionPerformed(ActionEvent   ae)
{
new   FF1();
}
}

class   FF1   extends   JFrame//--------------------------------------------------关于五子棋子窗体
{
JButton   j=new   JButton( "确定 ");
public   FF1()
{
Container   con1=getContentPane();
con1.setLayout(null);
setTitle( "关于五子棋 ");
setBounds(310,310,400,250);
setResizable(false);
j.setBounds(160,180,70,30);
JLabel   a8=new   JLabel( "五子棋1.0版本由longlonger ");
JLabel   a9=new   JLabel( "       模仿开发! ");
a8.setFont(new   Font( "宋体 ",Font.BOLD+Font.ITALIC,   24));
a9.setFont(new   Font( "宋体 ",Font.BOLD+Font.ITALIC,   24));
a8.setBounds(60,30,270,80);
a9.setBounds(60,60,270,80);
con1.add(j);
                con1.add(a8);
                con1.add(a9);
j.addActionListener(new   nnn1());
show();
}

class   nnn1   implements   ActionListener
{
public   void   actionPerformed(ActionEvent   ae)
{
hide();
}
}
  }

 

原创粉丝点击