java五子棋初实现

来源:互联网 发布:伦敦金投资软件 编辑:程序博客网 时间:2024/05/21 14:58

        五子棋小游戏作为新人上手来说,十分适合。此次程序相对来说除了人机对弈没有实现外,各方面功能都初步完成。             

1,创建窗体,分为棋盘面板LeftPanel和功能面板RightPanel,并设置基本属性。设置可见。publicvoid ShowUI()

{   init();

    addRightPanel();

    addLeftPanel();

   

    //addListener();

    setVisible(true);

}

public void init()

{

   

    setTitle("五子棋");

    setSize(600, 480);

添加背景图片

    ImageIcon ii = new ImageIcon(getClass().getResource("/tt/1.png"));

    setIconImage(ii.getImage());

   

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    setLocationRelativeTo(null);

    setResizable(false);

   

}

2,左边面板在背景图片,画出棋盘。

 

private void drawQiPan(Graphics g) {

    for(inti=1;i<22;i++){

       g.drawLine(20*i, 20, 20*i, 420);

       g.drawLine(20, 20*i, 420, 20*i);

    }

}

3,右边面板添加图片,并在图片上添加按钮。

public void paintComponent(Graphics g)

{

    super.paintComponent(g);

    ImageIcon i2 = new ImageIcon(getClass().getResource("/tt/1.jpg"));

    g.drawImage(i2.getImage(), 0, 0,null);

}

public RightPanel(MainFramemf)

{

    this.leftPanel =mf.lp;

    addMusic();

    this.mf =mf;//LeftPanel lp=newLeftPanel(mf);

   

    setPreferredSize(new Dimension(150, 0));

 

    setBorder(BorderFactory.createBevelBorder(0, Color.white, Color.gray));

   

    FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 50, 50);

    setLayout(fl);

    JButton jb1=new JButton("开始游戏");

    JButton jb2=new JButton("悔棋");

    JButton jb3=new JButton("关于游戏");

    JButton jb4=new JButton("退出游戏");

    JButton jb5=new JButton("关闭音乐");

    add(jb1);

    add(jb2);

    add(jb3);

    add(jb4);

    add(jb5);

    jb1.addActionListener(new ActionListener(){

4,为左右俩边添加监听,进行下棋判断输赢和按钮功能的实现

左边面板的监听

public void addListener() {

    // TODO Auto-generated method stub

    MouseAdapter as= new MouseAdapter() {

      

       public void mouseMoved(MouseEvent e){

       }

      

       public void mouseClicked(MouseEvent e) {

      

           intx = e.getX()-20;

           inty = e.getY()-20;

           intxIndex = x%20>10?x/20+1:x/20;

           intyIndex = y%20>10?y/20+1:y/20;

          

          

          

           System.out.println(xIndex+","+yIndex);

           System.out.println(xIndex+","+yIndex);

          

           if(flag_begin ==true)

           {

           if(xIndex>20 ||yIndex>20)

           {

              return;

           }

          

           if(allChess[xIndex][yIndex] ==0)

           {

           pia();

           if(chessFlag)

           {

              allChess[xIndex][yIndex] = 1;

           }

           else

           {

              allChess[xIndex][yIndex] = 2;

           }

           Point point = new Point(xIndex,yIndex);

           allCs.add(point);

           chessFlag = !chessFlag;

           mf.repaint();

           win(xIndex,yIndex);

           }   }else{

           JOptionPane.showMessageDialog(mf,"请点击开始,然后游戏!");

       }

       }

 

       private void win(int xIndex, int yIndex) {

           // TODO Auto-generatedmethod stub

          

           int[][][]chess={{{-1,0},{1,0}},{{0,1},{0,-1}},{{1,1},{-1,-1}},{{1,-1},{-1,1}}};

           intcount=1;

           intcurrent=allChess[xIndex][yIndex];

           inttempx=xIndex;

           inttempy=yIndex;

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

              int[][]change=chess[i];

              count=1;

              for(intj=0;j<change.length;j++){

                 

                  tempx=xIndex;

               tempy=yIndex;

                     while(true)

           {

              tempx= tempx+change[j][0];

               tempy= tempy+change[j][1];

               if(xIndex<0||xIndex>21||yIndex<0||yIndex>21)

               {

                   break;

                   }

                   intvalue=allChess[tempx][tempy];

               if(value==current){

                   count++;

               }else{

                   break;

               }

                         }

             

              }

              if(count>=5&&allChess[xIndex][yIndex]==1){

                  JOptionPane.showMessageDialog(null,"白棋胜利!");

                  flag_begin=false;

              }

    if(count>=5&&allChess[xIndex][yIndex]==2){

    JOptionPane.showMessageDialog(null,"黑棋胜利!");

    flag_begin=false;

              }

           }

          

       }

    };

    this.addMouseListener(as);

    this.addMouseMotionListener(as);

}

右边面板的监听

比如开始按钮

jb1.addActionListener(new ActionListener(){

 

       @Override

       public void actionPerformed(ActionEvent e) {

          

           LeftPanel.flag_begin=true;

       LeftPanel. allChess =new int[21][21];

       mf.repaint();

         // lp.addListener();

          

       }

   

});;

5,增加一些特色功能,如音乐。

private void addMusic() {

    // TODO Auto-generated method stub

    try {

       //创建文件输入流

       FileInputStream fis = new FileInputStream("tt/back.wav");

       //将文件流包装成音频流

       as = new AudioStream(fis);

       //AudioPlayer.player开始/暂停音频流

       AudioPlayer.player.start(as);

   

    } catch (Exceptione) {

       // TODO Auto-generatedcatch block

       e.printStackTrace();

    }

   

}}


6,进行布局,排除bug,完善代码。


0 0