<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/ DTD/wml_1.1.xml">

<wml>
<head>
<meta http-equiv="cache-control" content="max-age=180,private" />
</head>
<card title="JAVA畢業設計-五子棋">
<p>
作者:<a href="index.php?action=showuser&amp;userid=1&amp;hash=">way</a><br />时间:2008-12-17 15:42<br />分类:<a href="index.php?action=list&amp;cid=22&amp;hash=">Development</a><br />标签:<a href="index.php?action=tags&amp;item=wayce&amp;hash=">wayce</a>, <a href="index.php?action=tags&amp;item=%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1&amp;hash=">毕业设计</a>, <a href="index.php?action=tags&amp;item=java&amp;hash=">java</a>, <a href="index.php?action=tags&amp;item=%E7%B0%A1%E6%98%93&amp;hash=">簡易</a>, <a href="index.php?action=tags&amp;item=%E4%BA%94%E5%AD%90%E6%A3%8B&amp;hash=">五子棋</a><br />附件:<a href="index.php?action=downfile&amp;id=197&amp;hash=">wzq1.jpg</a>, <a href="index.php?action=downfile&amp;id=198&amp;hash=">wzq2.jpg</a><br />内容:
急于交畢業設計... 比較簡單, 功能也相對貧乏. 不過應該還是有不少人需要吧~


[attach=197]


[attach=198]


獻上完整源代碼：


Java代码


    package five;   

        

    import java.awt.*;   

    import java.awt.event.*;   

    public class Five extends Frame{   

         static Five mainfram; //當前表單的控制碼   

         Bowl bowl = new Bowl();    

         ControlPanel cp = new ControlPanel();   

         static int cheese = 1;//cheese為棋子的顏色,1:黑,2:白   

         static int allCheese[][] = new int[13][13]; //用二維陣列來存儲棋盤上已經下過的棋子   

                                                     //初始化為0,然後1代表黑子,2代表白子   

        //陣列初始化為0(棋盤清空)   

        static{   

            for(int i=0;i&lt;13;i++){   

                for(int j=0;j&lt;13;j++){   

                    allCheese[i][j] = 0;   

                }   

            }   

        }     

      

        public Five() {   

            setLayout(new BorderLayout());   

            add(bowl,BorderLayout.CENTER);   

            add(cp,BorderLayout.EAST);   

            addWindowListener(new FiveWindowLinstener());   

            setTitle(&quot;++WAYCE 五子棋++&quot;);   

            mainfram = this;   

        }   

           

        public static void main(String[] args) {   

            Five f = new Five();   

            f.setSize(500,500);   

            f.show();   

        }   

           

        public class FiveWindowLinstener extends WindowAdapter   

        {   

            public void windowClosing(WindowEvent e){   

                dispose();   

                System.exit(0);   

            }   

        }//end FiveWindowLinstener   

    }//end Five   

      

    /***************************棋盤********************************/  

    class Bowl extends Panel   

    {   

        FivePanel fp[][] = new FivePanel[13][13];//棋盤實際由169個儲存格面板所組成   

           

        Bowl()   

        {    

            setLayout(new GridLayout(13,13));    

            for(int i=0;i&lt;13;i++){   

                for(int j=0;j&lt;13;j++){   

                    fp[i][j] = new FivePanel(i,j);   

                    add(fp[i][j]); //按網格類型填加儲存格面板   

                }     

            }    

        }//end Bowl()   

           

        class FivePanel extends Panel   

        {   

            int row,column;   

            public FivePanel(int row,int column)   

            {   

                this.row = row;   

                this.column = column;   

                setBackground(Color.gray);   

                addMouseListener(new PutCheese());   

            }//end FivePanel     

            public void paint(Graphics g){   

                g.setColor(Color.white);   

                if(row == 0 &amp;&amp; column == 0){//左上角   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth(),getHeight()/2);   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,getHeight());   

                }else if( row == 0 &amp;&amp; column == 12){//右上角   

                    g.drawLine(0,getHeight()/2,getWidth()/2,getHeight()/2);   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,getHeight());   

                }else if(row == 12 &amp;&amp; column == 0){//左下角   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth(),getHeight()/2);   

                    g.drawLine(getWidth()/2,0,getWidth()/2,getHeight()/2);   

                }else if(row ==12 &amp;&amp; column ==12){//左下角   

                    g.drawLine(getWidth()/2,0,getWidth()/2,getHeight()/2);   

                    g.drawLine(0,getHeight()/2,getWidth()/2,getHeight()/2);   

                }else if(row == 0){   

                    g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,getHeight());   

                }else if(row == 12){   

                    g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth()/2,0);   

                }else if(column == 0){   

                    g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());   

                    g.drawLine(getWidth()/2,getHeight()/2,getWidth(),getHeight()/2);   

                }else if(column == 12){   

                    g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());   

                    g.drawLine(getWidth()/2,getHeight()/2,0,getHeight()/2);   

                }else{   

                    g.drawLine(0,getHeight()/2,getWidth(),getHeight()/2);   

                    g.drawLine(getWidth()/2,0,getWidth()/2,getHeight());   

                }   

                if(Five.allCheese[row][column]==1){   

                    g.setColor(Color.orange);   

                    g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);   

                }else if(Five.allCheese[row][column]==2){   

                    g.setColor(Color.white);   

                    g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);   

                }   

                       

            }//end paint   

               

            public class PutCheese extends MouseAdapter{   

                public void mouseClicked(MouseEvent e){   

                    Graphics g = getGraphics();   

                    System.out.println(Five.cheese+&quot;-------------------&quot;);   

                    if(Five.allCheese[row][column] != 0) return; //此處已經下過棋子了   

                       

                    if (Five.cheese == 1){   

                       g.setColor(Color.orange);   

                       g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);   

                       Five.allCheese[row][column] = 1;   

                    }else if(Five.cheese == 2){   

                       g.setColor(Color.white);   

                       g.fillOval(getWidth()/4,getHeight()/4,getWidth()/2,getHeight()/2);   

                       Five.allCheese[row][column] = 2;   

                    }   

                       

                     if(checkWin(Five.cheese,row,column)==1){   

                    //  System.out.println(&quot;win&quot;);   

                        MessageBox dl = new MessageBox((Frame)Five.mainfram,&quot;五子棋&quot;,true,&quot;       黃方勝!      &quot;);   

                     }else if(checkWin(Five.cheese,row,column)==2){   

                        MessageBox dl = new MessageBox((Frame)Five.mainfram,&quot;五子棋&quot;,true,&quot;       白方勝!      &quot;);   

                     }     

                           

                           

                    if(Five.cheese == 1){   

                       Five.cheese = 2;   

                    }else {   

                       Five.cheese = 1;   

                    }   

                       

                    for(int i=0;i&lt;13;i++){   

                        for(int j=0;j&lt;13;j++){   

                            System.out.print(Five.allCheese[i][j]+&quot; &quot;);   

                        }   

                        System.out.println(&quot;&quot;);   

                    }   

                       

                      

                }//end  mouseClicked()   

            }//end PutCheese   

    //*******計算輸贏的函數******************   

            public  int checkWin(int che,int row,int column){   

                int startRow=0;   

                int startColumn=0;   

                int line =0;   

               //左斜線   

               System.out.println(&quot;棋子為:&quot;+che+&quot;,row=&quot;+row+&quot;,column=&quot;+column);   

                  

               startRow = row; startColumn = column;   

               while(true){   

                if((startRow == 0)||(startColumn == 0)) {break;}   

                else {startRow--; startColumn--;}   

               }   

               while(true){   

                  if( (startRow == 12)||(startColumn ==12) ||(line&gt;=5)) break;//遍歷到最後結束   

                  if( Five.allCheese[startRow][startColumn]==che){   

                      line++;   

                      System.out.print(line);   

                  }else{ line = 0; }   

                  startRow++; startColumn++;   

               }   

               System.out.println(&quot;左斜線:&quot;+line);   

               if(line&gt;=5) return che;//五子連線，贏！   

               else line = 0;   

           

               //右斜線   

               startRow = row; startColumn = column;   

               while(true){   

                if((startRow == 0)||(startColumn == 12)) {break;}   

                else {startRow--; startColumn++;}   

               }   

                 

               while(true){   

                   if( (startRow == 12)||(startColumn ==0)  ||(line&gt;=5))break;//遍歷到最後結束   

                   if( Five.allCheese[startRow][startColumn]==che){   

                       line++;   

                       System.out.print(line);   

                   }else{ line = 0; }   

                   startRow++; startColumn--;   

               }   

               System.out.println(&quot;右斜線:&quot;+line);   

               if(line&gt;=5) return che;//五子連線，贏！   

               else line = 0;   

           

               //水平線   

               startRow = row; startColumn = 0;   

               while(true){   

                   if((startColumn ==12) ||(line&gt;=5)) break;//遍歷到最後結束   

                   if( Five.allCheese[startRow][startColumn]==che){   

                        line++;   

                        System.out.print(Five.allCheese[startRow][startColumn]);   

                        System.out.println(&quot;&quot;);   

                        System.out.print(line);   

                   }else{ line = 0; }   

                   startColumn++;   

             }   

             System.out.println(&quot;水平線:&quot;+line);   

             if(line&gt;=5) return che;//五子連線，贏！   

             else   line = 0;   

      

             //垂直線   

             startRow = 0; startColumn = column;   

             while(true){   

                 if((startRow ==12) ||(line&gt;=5))break;//遍歷到最後結束   

                 if( Five.allCheese[startRow][startColumn]==che){   

                     line++;System.out.print(line);   

                 }else{ line = 0; }   

                 startRow++;   

             }   

             System.out.println(&quot;垂直線:&quot;+line);   

              if(line&gt;=5) return che;//五子連線，贏！   

              else  line = 0;   

           

              return 0;   

          }//end checkWin()   

             

          class MessageBox extends Dialog   

          {   

            Label message;   

            Button confirm = new Button(&quot;確定&quot;);   

               

            MessageBox(Frame owner,String title,boolean modal,String msg){   

                super(owner,title,modal);   

                setLayout(new FlowLayout(FlowLayout.CENTER));   

                message = new Label(msg);   

                add(message);   

                add(confirm);   

                confirm.addActionListener(new ActionListener(){   

                    public void actionPerformed(ActionEvent e){   

                        dispose();   

                    }   

                    });   

                setSize(130,100);   

                setResizable(false) ;   

                setBounds(200,200,130,100);   

                   

                show();   

           

            }   

          }   

       }//end FivePanel   

               

    }//end Bowl;   

      

    /*******************控制台****************************/   

    class ControlPanel extends Panel   

    {   

        CheckboxGroup cbg = new CheckboxGroup();   

        

        Checkbox cb1 = new Checkbox(&quot;黃方先&quot;,cbg,true);   

        Checkbox cb2 = new Checkbox(&quot;白方先&quot;,cbg,false);   

        Button b1 = new Button(&quot;開始&quot;);   

        Button b2 = new Button(&quot;重新開始&quot;);   

        Button b3 = new Button(&quot;幫助&quot;);   

        Button b4 = new Button(&quot;退出&quot;);   

        ControlPanel()   

        {   

            setLayout(new GridLayout(14,1,10,5));   

               

            add(cb1);   

            add(cb2);   

            add(new Label());   

            add(new Label());   

            add(new Label());   

            add(b1);   

            add(b2);   

            add(b3);   

            add(b4);   

            setBounds(0,0,200,500);   

        }   

    }   



</p><p>
<a href="index.php?action=comments&amp;articleid=108&amp;hash=">查看评论</a><br />
<a href="index.php?action=login&amp;hash=">立即登陆发表评论</a><br />
</p>
<p><a href="index.php?action=list&amp;hash=">返回日志列表</a><br /><a href="index.php?action=index&amp;hash=">返回主页</a></p>
</card>
</wml>
