ChessWZQ.java~2~
上传用户:ptnike
上传日期:2007-06-15
资源大小:61k
文件大小:31k
源码类别:

棋牌游戏

开发平台:

Java

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.net.*;
  6. import java.io.*;
  7. /**
  8.  * <p>Title: </p>
  9.  * <p>Description: </p>
  10.  * <p>Copyright: Copyright (c) 2003</p>
  11.  * <p>Company: e-top</p>
  12.  * @author cylix
  13.  * @version 1.0
  14.  */
  15. public class ChessWZQ extends JFrame {
  16.     JPanel contentPane;
  17.     BorderLayout borderLayout1 = new BorderLayout();
  18.     protected Image white = null;
  19.     protected Image black = null;
  20.     protected static int pColor; // people 's color
  21.     protected static int cColor; // computer's color
  22.     static boolean ptocFlag=false;
  23.     boolean pFirst = false;
  24.     private int bestX=0; // computer find the best x coordinate to put chess
  25.     private int bestY=0; // computer find the best y coordinate to put chess
  26.     private int RectX1=0; // rectangle bounds according to (x,y)
  27.     private int RectY1=0; // length is 9
  28.     private int RectX2=0; // size 9*9
  29.     private int RectY2=0; // used to weight to each black position
  30.     private int weightBoard[][];
  31.     static Socket socket;
  32.     private static int PORT;
  33.     static ObjectInputStream in;
  34.     static ObjectOutputStream out;
  35.     String name=null ;
  36.     String serverAddress=null;
  37.     static char send[];
  38.     public static Message message = new Message();
  39.     public static boolean beginFlag = false;
  40.     BoardPanel bpanel = new BoardPanel();
  41.     JPanel jpanel3 = new JPanel();
  42.     JLabel label1 = new JLabel("Player1");
  43.     JLabel label2 = new JLabel(" VS ");
  44.     JLabel label3 = new JLabel("Player2");
  45.     JLabel label4 = new JLabel("Player List ");
  46.     JLabel label5 = new JLabel("Message list... ");
  47.     static JLabel label6 = new JLabel("welcome");
  48.     JLabel label7 = new JLabel("Host");
  49.     JLabel label8 = new JLabel("Player");
  50.     JRadioButton jrbBlack = new JRadioButton("Black");
  51.     JRadioButton jrbWhite = new JRadioButton("White");
  52.     DefaultListModel lItems = new DefaultListModel();
  53.     JList list = new JList(lItems);
  54.     JMenuBar mb = new JMenuBar();
  55.     JMenu create = new JMenu("Create");
  56.     JMenu setting = new JMenu("Setting");
  57.     JMenu quit = new JMenu("Quit");
  58.     JMenu about = new JMenu("About");
  59.     JMenuItem cPtoP = new JMenuItem("Play With people");
  60.     JMenuItem cPtoC = new JMenuItem("Play With Computer");
  61.     JMenuItem load = new JMenuItem("Load game...");
  62.     JMenuItem save = new JMenuItem("Save ...");
  63. //    JMenuItem sLimited = new JMenuItem("禁止禁手");
  64. //    JMenuItem sNoLimited = new JMenuItem("允许禁手");
  65.     public ChessWZQ() {
  66.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  67.         getContentPane().setLayout(null);
  68.         weightBoard = new int [15][15]; // save each position 's weight
  69.         PORT = Server.PORT;//set socket port
  70.         send = new char[60];
  71.         try {
  72.             jbInit();
  73.         }
  74.         catch(Exception e) {
  75.             e.printStackTrace();
  76.         }
  77.         try {
  78.             getContentPane().setLayout(null);
  79.             jrbBlack.addActionListener(new ActionListener(){
  80.                 public void actionPerformed(ActionEvent e){
  81.                     bpanel.setColor(1);
  82.                     cColor=2;
  83.                     jrbBlack.setEnabled(false);
  84.                     jrbWhite.setEnabled(false);
  85.                     drawChess(1);
  86.                     jrbWhite.setSelected(true);
  87.                     if(ptocFlag==true){
  88.                         return;
  89.                     }
  90.                     Message ms = new Message();
  91.                     ms.color=1;
  92.                     ms.type=13;
  93.                     try{
  94.                         out.writeObject(ms);
  95.                     }catch(IOException error){
  96.                         error.printStackTrace();
  97.                     }
  98.                 }
  99.             });
  100.             jrbWhite.addActionListener(new ActionListener(){
  101.                 public void actionPerformed(ActionEvent e){
  102.                     bpanel.setColor(2);
  103.                     cColor=1;
  104.                     jrbWhite.setEnabled(false);
  105.                     jrbBlack.setEnabled(false);
  106.                     drawChess(2);
  107.                     jrbBlack.setSelected(false);
  108.                     if(ptocFlag==true){
  109.                         return;
  110.                     }
  111.                     Message ms = new Message();
  112.                     ms.color=2;
  113.                     ms.type=13;
  114.                     try{
  115.                         out.writeObject(ms);
  116.                     }catch(IOException error){
  117.                         error.printStackTrace();
  118.                     }
  119.                 }
  120.             });
  121.             about.addMouseListener(new MouseAdapter(){
  122.                 public void mouseClicked(MouseEvent e){
  123.                     JOptionPane.showMessageDialog(null,
  124.                                                   "author: cylix nCopyright (c) 2003 e-topnmail:cylix_xtcc@sina.com",
  125.                                                   "五子棋1.0(beta)",JOptionPane.INFORMATION_MESSAGE);
  126.                 }
  127.             });
  128.             quit.addMouseListener(new MouseAdapter(){
  129.                 public void mouseClicked(MouseEvent e){
  130.                     int flag=JOptionPane.showConfirmDialog(null,
  131.                                                   "Quit the Program ?",
  132.                                                   "Are you sure to quit ?",
  133.                                                   JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  134.                     if(flag==0){       //press YES
  135.                         // disconnect to server
  136.                         sendDisconnect();
  137.                         System.exit(0);
  138.                     }
  139.                 }
  140.             });
  141.             setting.addMouseListener(new MouseAdapter(){
  142.                 public void mouseClicked(MouseEvent e){
  143.                     Object selection[] = {"Forbiden","Allow any"};
  144.                     Object set = JOptionPane.showInputDialog(null,
  145.                         "Setting...","would you allow any method?",
  146.                         JOptionPane.QUESTION_MESSAGE,null,selection,selection[0]);
  147.                     if(ptocFlag==true){
  148.                         return;
  149.                     }
  150.                     Message ms = new Message();
  151.                     if(set==null){
  152.                         return;
  153.                     }
  154.                     if(set.toString().equals(selection[0])){
  155.                         ms.setting=false;
  156.                     }
  157.                     else{
  158.                         ms.setting=true;
  159.                     }
  160.                     //System.out.println("setting begin..."+msg.setting);
  161.                     ms.type=12;
  162.                     try{
  163.                         out.writeObject(ms);
  164.                     }catch(IOException error){
  165.                         error.printStackTrace();
  166.                     }
  167.                 }
  168.             });
  169.             cPtoP.addActionListener(new ActionListener(){
  170.                 public void actionPerformed(ActionEvent e){
  171.                     ptocFlag=false;
  172.                     JOptionPane.showMessageDialog(null,
  173.                         "You can choose a player from the listBox on the right","Welcome...",
  174.                         JOptionPane.INFORMATION_MESSAGE);
  175.                 }
  176.             });
  177.             cPtoC.addActionListener(new ActionListener(){
  178.                 public void actionPerformed(ActionEvent e){
  179.                     if(ptocFlag==true){
  180.                         int flag=JOptionPane.showConfirmDialog(null,
  181.                             "You give up... ?","Message",
  182.                             JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
  183.                         if(flag==0){
  184.                             newGame();
  185.                             return;
  186.                         }
  187.                     }
  188.                     label3.setText("Computer");
  189.                     Object selection[] = {"Computer First","You First"};
  190.                     Object set = JOptionPane.showInputDialog(null,
  191.                         "choose who first...","setting...",
  192.                         JOptionPane.QUESTION_MESSAGE,null,
  193.                         selection,selection[0]);
  194.                         ptocFlag = true;
  195.                     if(set.toString().equals(selection[1])){
  196.                         pFirst = true;
  197.                     }else{
  198.                         pFirst = false;
  199.                     }
  200.                     pColor=bpanel.getColor();
  201.                     if(pColor==1){
  202.                         jrbBlack.setSelected(true);
  203.                         jrbWhite.setSelected(false);
  204.                         cColor=2;
  205.                     }
  206.                     else{
  207.                         jrbWhite.setSelected(true);
  208.                         jrbBlack.setSelected(false);
  209.                         cColor=1;
  210.                     }
  211.                     jrbBlack.setEnabled(false);
  212.                     jrbWhite.setEnabled(false);
  213.                     ptoComputer();
  214.                 }
  215.             });
  216.             create.setBounds(5,5,40,20);
  217.             setting.setBounds(45,5,40,20);
  218.             quit.setBounds(85,5,40,20);
  219.             about.setBounds(125,5,40,20);
  220.             create.add(cPtoP);
  221.             create.add(cPtoC);
  222.             create.add(load);
  223.             create.add(save);
  224.             mb.add(create);
  225.             mb.add(setting);
  226.             mb.add(quit);
  227.             mb.add(about);
  228.             this.setJMenuBar(mb);
  229.             bpanel.setBounds(0,0,470,460);
  230.             bpanel.setBorder(BorderFactory.createMatteBorder(0,0,1,1,Color.ORANGE));
  231.             jpanel3.setLayout(null);
  232.             jpanel3.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.DARK_GRAY));
  233.             jpanel3.setBounds(470,0,180,460);
  234.             label7.setBounds(10,10,30,20);
  235.             label7.setBackground(Color.blue);
  236.             label7.setForeground(Color.yellow);
  237.             label8.setBounds(90,10,40,30);
  238.             label8.setBackground(Color.blue);
  239.             label8.setForeground(Color.yellow);
  240.             label1.setBounds(10,40,50,20);
  241.             label2.setForeground(Color.RED);
  242.             label2.setBounds(60,40,50,20);
  243.             label3.setBounds(90,40,70,20);
  244.             label4.setForeground(Color.BLUE);
  245.             label4.setBounds(10,78,70,20);
  246.             label5.setBackground(Color.magenta);
  247.             label5.setForeground(Color.RED);
  248.             label5.setBounds(15,395,160,20);
  249.             label6.setBounds(15,415,180,20);
  250.             //label6.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.yellow));
  251.             jrbBlack.setSelected(true);
  252.             jrbBlack.setBounds(10,380,80,15);
  253.             jrbWhite.setBounds(90,380,80,15);
  254.             list.setBorder(BorderFactory.createMatteBorder(1,1,1,1,Color.black));
  255.             list.setBounds(10,100,150,265);
  256.             list.addMouseListener(new MouseAdapter(){
  257.                 public void mouseClicked(MouseEvent e){
  258.                     // double click to choose a player
  259.                     if(e.getClickCount()==2){
  260.                         String str = list.getSelectedValue().toString();
  261.                         if(str.equals(name)==true){
  262.                             label6.setText("You can't choose yourself");
  263.                             return;
  264.                         }
  265.                         label3.setText(str);
  266.                         list.setEnabled(false);
  267.                         Message ms = new Message();
  268.                         ms.type=3;  //request play with another
  269.                         strToCharArray(str,ms.msg);
  270.                         ms.color = bpanel.getColor();
  271.                         //System.out.println("request for play with "+str);
  272.                         try{
  273.                             out.writeObject(ms);
  274.                         }catch(IOException er){
  275.                             er.printStackTrace();
  276.                         }
  277.                 }
  278.             }
  279.         });
  280.             jpanel3.add(label1);
  281.             jpanel3.add(label2);
  282.             jpanel3.add(label3);
  283.             jpanel3.add(label4);
  284.             jpanel3.add(label5);
  285.             jpanel3.add(label6);
  286.             jpanel3.add(label7);
  287. //            jpanel3.add(label8);
  288.             jpanel3.add(jrbBlack);
  289.             jpanel3.add(jrbWhite);
  290.             jpanel3.add(list);
  291.             // getContentPane().add(jpanel1);
  292.             getContentPane().add(bpanel);
  293.             getContentPane().add(jpanel3);
  294.            // lItems.add(0,"hugh");
  295.            // lItems.add(0,"cylix");
  296.         }
  297.         catch(Exception e) {
  298.             e.printStackTrace();
  299.         }
  300.     }
  301.     //Component initialization
  302.     private void jbInit() throws Exception  {
  303.         contentPane = (JPanel) this.getContentPane();
  304.         contentPane.setLayout(borderLayout1);
  305.         this.setSize(new Dimension(660, 530));
  306.         this.setTitle("五子棋客户端(beta)");
  307.     }
  308.     /**
  309.      * send disconnection to server
  310.      * type = 7
  311.      */
  312.     public void sendDisconnect(){
  313.         Message ms = new Message();
  314.         ms.type=7;
  315.         try{
  316.             out.writeObject(ms);
  317.         }catch(IOException e){
  318.             e.printStackTrace();
  319.         }
  320.     }
  321.     public void drawChess(int col){
  322.         Graphics g = jpanel3.getGraphics();
  323.         if(col==1) g.setColor(Color.black);
  324.         else g.setColor(Color.white);
  325.         g.fillOval(50,10,20,20);
  326.     }
  327.     /**
  328.      * <format> java ChessWZQ < server address> <player name>
  329.      * @param args[0]=server address args[1]=player name
  330.      */
  331.     public static void main(String [] args){
  332.         ChessWZQ wzq = new ChessWZQ();
  333.         wzq.setResizable(false);
  334.         wzq.setVisible(true);
  335.         wzq.drawChess(1);   //default color is black
  336.         Message ms = new Message();
  337. //        wzq.serverAddress = new String(args[0]);
  338. //        wzq.name = new String(args[1]);
  339. //        wzq.strToCharArray(wzq.name,ms.msg);
  340.         try{
  341.             // should get server name by command line
  342.             InetAddress addr = InetAddress.getByName(wzq.serverAddress);
  343.             //System.out.println("address "+ addr.toString()+" port: "+PORT);
  344.             socket = new Socket(addr, PORT);
  345.             //System.out.println("set socket successful...");
  346.             out = new ObjectOutputStream(
  347.                 socket.getOutputStream());
  348.             in = new ObjectInputStream(
  349.                 socket.getInputStream());
  350.             ms.type=1;
  351.             try{
  352.                 out.writeObject(ms);
  353.             }catch(IOException e){
  354.                 e.printStackTrace();
  355.             }
  356.             while(true){
  357.                 try {
  358.                     ms = (Message) in.readObject();
  359.                     //System.out.println("get message from server...type = "+ms.type);
  360.                     wzq.doMessage(ms);
  361.                 }
  362.                 catch (ClassNotFoundException e) {
  363.                     e.printStackTrace();
  364.                 }
  365.             }
  366.             //System.out.println("ms.coordinateX + , + msg.coordinateY");
  367.             //System.out.println(ms.color);
  368.             //System.out.println("close ...");
  369.             //socket.close();
  370.         }catch(IOException e){
  371.             e.printStackTrace();
  372.         }
  373.     }
  374.     /**
  375.      * deal with message get from server
  376.      * @param msg
  377.      * @return
  378.      */
  379.     protected int doMessage(Message msg){
  380.         String str=arrayToString(msg.msg);
  381.         //System.out.println("msg name = "+str+" msg.type = "+msg.type);
  382.         switch(msg.type){
  383.             case 2:{//respones of put chessman
  384.                 putChessman(msg);
  385.                 break;
  386.             }
  387.             case 3:{//request from another player
  388.                 requestAnother(msg);
  389.                 break;
  390.             }
  391.             case 4:{ // B deny to play with A
  392.                 getDeny(msg);
  393.                 break;}
  394.             case 5:{ // B accept A's requestion
  395.                 acceptToPlay(msg);
  396.                 break;}
  397.             case 6:{
  398.                 getVictory(msg);
  399.                 break;}
  400.             case 7:{
  401.                 getDisconnection(msg);
  402.                 break;}
  403.             case 9:{//reply of adding new player to all client
  404.                 ///System.out.println("add new player to list type = 9 name = "+str+"msg[0]= "+msg.msg[0]);
  405.                 lItems.add(0,str);
  406.                 break;
  407.             }
  408.             case 10:{ //respones of connecting
  409.                 //System.out.println("add him self  type = 10 name = "+str+"msg[0]= "+msg.coordinateX+msg.msg[0]);
  410.                 label1.setText(str);
  411.                 label6.setText("welcome "+str);
  412.                 lItems.add(0,str);
  413.                 break;
  414.             }
  415.             case 14:{ //B have accept playing with A,then get msg
  416.                       //to set color and settings
  417.                 break;
  418.             }
  419.             case 15:{
  420.                 lItems.clear();
  421.                 break;
  422.             }
  423.             case 17:{
  424.                 getFailed(msg);
  425.                 break;
  426.             }
  427.             case 20:{
  428.                 ptocWin(msg);
  429.                 break;
  430.             }
  431.         }
  432.         return 0; //end correctly
  433.     }
  434.     private void ptocWin(Message ms){
  435.         String str=this.arrayToString(ms.msg);
  436.         JOptionPane.showMessageDialog(null,str+" win the game!","V the game",JOptionPane.INFORMATION_MESSAGE);
  437.         newGame();
  438.     }
  439.     /**
  440.      * failed the game and have a new one
  441.      * type == 17
  442.      * @param msg
  443.      */
  444.     public void getFailed(Message msg){
  445.         bpanel.drawChess(msg.coordinateX,msg.coordinateY);
  446.         JOptionPane.showMessageDialog(null,
  447.                                       "Sorry,You've failed the game",
  448.                                       "Try Again",
  449.                                       JOptionPane.INFORMATION_MESSAGE);
  450.         label3.setText("Player2");
  451.         // have a new game to continue to play
  452.         newGame();
  453.     }
  454.     /**
  455.      * another player send disconnection msg,here will resolve it
  456.      * type ==7
  457.      * @param msg
  458.      */
  459.     public void getDisconnection(Message msg){
  460.         getVictory(msg);
  461.     }
  462.     /**
  463.      * win the game
  464.      * type ==6
  465.      * @param msg
  466.      */
  467.     public void getVictory(Message msg){
  468.         JOptionPane.showMessageDialog(null,
  469.                                       "You  Win  The  Game",
  470.                                       "Congratulations",
  471.                                       JOptionPane.INFORMATION_MESSAGE);
  472.         // have a new game to continue to play
  473.         label3.setText("Player2");
  474.         newGame();
  475.     }
  476.     /**
  477.      * when people win a game ,then he can start a new one
  478.      * @param msg
  479.      */
  480.     public void newGame(){
  481.         jrbBlack.setEnabled(true);
  482.         jrbWhite.setEnabled(true);
  483.         jrbBlack.setSelected(true);
  484.         jrbWhite.setSelected(false);
  485.         list.setEnabled(true);
  486.         setting.setEnabled(true);
  487.         bpanel.clearBoard();
  488.         drawChess(1);
  489.         pColor=1;cColor=2;
  490.         if(ptocFlag==false){
  491.             Message msg = new Message();
  492.             msg.type = 19;
  493.             strToCharArray(name, msg.msg);
  494.             try {
  495.                 out.writeObject(msg);
  496.             }
  497.             catch (IOException e) {
  498.                 e.printStackTrace();
  499.             }
  500.         }
  501.         ptocFlag=false;
  502.         beginFlag=false;
  503.     }
  504.     /**
  505.      * precondition:get a msg from competitor of puting a chessman
  506.      * fundition : deal with it
  507.      * @param msg
  508.      */
  509.     public void putChessman(Message msg){
  510.         if(ptocFlag==false){
  511.             bpanel.updateBoard(msg.coordinateX,msg.coordinateY);
  512.             bpanel.drawChess(msg.coordinateX,msg.coordinateY);
  513.             beginFlag = true;
  514.             return;
  515.         }else{
  516.             // update native board
  517.             // search position to put chessman
  518.         }
  519.     }
  520.     /**
  521.      * A get a Ok msg from B and seting according with B
  522.      * type= 5 B accept to play with A
  523.      * @param msg = B's name
  524.      */
  525.     public void acceptToPlay(Message msg){
  526.         String str=arrayToString(msg.msg);
  527.         String ss=null;
  528.         if(msg.color==1){
  529.             ss = new String("white");
  530.             bpanel.setColor(2);
  531.         }
  532.         else{
  533.             ss = new String("black");
  534.             bpanel.setColor(1);
  535.         }
  536.         JOptionPane.showMessageDialog(null,
  537.                                       "OK. "+str+" have accepted your requestionnYour color is"+ss,
  538.                                       "Game will to begin...",JOptionPane.ERROR_MESSAGE);
  539.         list.setEnabled(false);
  540.         jrbBlack.setEnabled(false);
  541.         jrbWhite.setEnabled(false);
  542.         setting.setEnabled(false);
  543.         beginFlag=true;
  544.     }
  545.     /**
  546.      * A get a deny msg from B
  547.      * type ==4 deny to play
  548.      * @param msg
  549.      */
  550.     public void getDeny(Message msg){
  551.         String str=arrayToString(msg.msg);
  552.         JOptionPane.showMessageDialog(null,
  553.                                       "I'm sorryn"+str+" denied your requestion",
  554.                                       "Sorry...",JOptionPane.ERROR_MESSAGE);
  555.         list.setEnabled(true);
  556.         label3.setText("Player2");
  557.     }
  558.     /**
  559.      * A request play game with B,what below is B done
  560.      * deal with request to paly
  561.      * @param msg = requester 's name
  562.      */
  563.     public void requestAnother(Message msg){
  564.         String str=arrayToString(msg.msg);
  565. //        System.out.print("client requestAnother begin ...");
  566.         int flag =JOptionPane.showConfirmDialog(null,
  567.             "Player "+str+" want to play with younAre you OK?",
  568.             "Play request...",JOptionPane.YES_NO_OPTION,
  569.             JOptionPane.QUESTION_MESSAGE);
  570.         if(flag==0){// press YES
  571.             msg.type=5; //agree with request
  572.             if(msg.color==1){ // msg.color is B 's color ,different with A's
  573.                 drawChess(msg.color);
  574.                 jrbBlack.setSelected(true);
  575.                 jrbWhite.setSelected(false);
  576.                 bpanel.setColor(1);
  577.             }
  578.             else{
  579.                 drawChess(msg.color);
  580.                 jrbWhite.setSelected(true);
  581.                 jrbBlack.setSelected(false);
  582.                 bpanel.setColor(2);
  583.             }
  584. //                System.out.println("B's color is"+msg.color);
  585.             list.setEnabled(false);
  586.             label3.setText(str);
  587.             setting.setEnabled(false);
  588.             jrbBlack.setEnabled(false);
  589.             jrbWhite.setEnabled(false);
  590.             beginFlag=true;
  591.         }
  592.         if(flag==1){// press NO
  593.             msg.type=4; // deny request
  594.         }
  595.         try{
  596.             out.writeObject(msg);
  597.         }catch(IOException e){
  598.             e.printStackTrace();
  599.         }
  600.     }
  601.     public void paint(Graphics g){
  602.         super.paint(g);
  603.         drawChess(bpanel.getColor());
  604.     }
  605.     /**
  606.      * Overridden so we can exit when window is closed
  607.      */
  608.     protected void processWindowEvent(WindowEvent e) {
  609.         super.processWindowEvent(e);
  610.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  611.             System.exit(0);
  612.         }
  613.     }
  614.     // convert string to array which is end with ''
  615.     public void strToCharArray(String str,char [] arr){
  616.         int i;
  617.         for(i=0;i<str.length()&&i<49;i++){
  618.             arr[i] = str.charAt(i);
  619.         }
  620.         arr[i]='';
  621.     }
  622.     /**
  623.      * filter array 's black space which is end of the array
  624.      * @param arr
  625.      * @param str
  626.      */
  627.     public String arrayToString(char [] arr){
  628.         int i=0,length=0;
  629.         while(arr[i]!='' && i<50){
  630.             i++;
  631.         }
  632.         length=i;
  633.         char [] ss = new char[length];
  634.         for(i=0;i<length;i++){
  635.             ss[i]=arr[i];
  636.         }
  637.         String str = new String(ss);
  638.         return str;
  639.         //System.out.println("arraytoString "+str+"length = "+length);
  640.     }
  641.     /**
  642.      * do people play with computer
  643.      */
  644.     public void ptoComputer(){
  645.         int x=0,y=0;
  646.         int position;
  647.         if(pFirst==false){
  648.             x=7;
  649.             y=7;
  650.             bpanel.updateBoard(x,y);
  651.             bpanel.drawChess(x,y);
  652.             beginFlag=true;
  653.         }else{
  654.             beginFlag=true;
  655.         }
  656.     }
  657.     /**
  658.      * choos a best position to put chessman
  659.      * @param x the newest chessman's x coordinate
  660.      * @param y the newest chessman's y coordinate
  661.      */
  662.     private void cPutChess(int x,int y){
  663.         setRect(x,y);
  664.         setWeight(x,y,pColor);
  665.         getBetter(3);
  666.     }
  667.     /**
  668.      * set a rectangle of 9*9
  669.      * @param x center of rectangle
  670.      * @param y center of rectangle
  671.      */
  672.     private void setRect(int x,int y){
  673.         if(x-4>0)  RectX1=x-4;
  674.         else       RectX1=0;
  675.         if(x+4>14) RectX2=14;
  676.         else       RectX2=x+4;
  677.         if(y-4>0)  RectY1=y-4;
  678.         else       RectY1=0;
  679.         if(y+4>14) RectY2=14;
  680.         else       RectY2=y+4;
  681.         if(RectX1>RectY1) RectX1 = x-(y-RectY1);
  682.         else              RectY1 = y-(x-RectX1);
  683.         if(RectX2>RectY2) RectY2 = y+(RectX2-x);
  684.         else              RectX2 = x+(RectY2-y);
  685.     }
  686.     /**
  687.      * set each black position's weight in the rectangle
  688.      * @param x rectangle center's x coordinate
  689.      * @param y rectangle center's x coordinate
  690.      */
  691.     private void setWeight(int x,int y,int tcolor){
  692.         int i=RectX1,j=RectY1,value=0,k=0,n=0,flag=0;
  693.         // '--' direction
  694.         for(i=RectX1,j=y;i<=RectX2;i++){
  695.             if(BoardPanel.board[i][j]!=0){
  696.                 continue;
  697.             }
  698.             value=0;flag=0;
  699.             for(k=1;i-k>=RectX1 && k<5;k++){
  700.                 if(BoardPanel.board[i-k][j]==tcolor){
  701.                     value++;
  702.                     continue;
  703.                 }
  704.                 if(BoardPanel.board[i-k][j]==0){//black space
  705.                     flag++;
  706.                     break;
  707.                 }
  708.             }
  709.             for(k=1;i+k<RectX2 && k<5;k++){
  710.                 if(BoardPanel.board[i+k][j]==tcolor){
  711.                     value++;
  712.                 }
  713.                 if(BoardPanel.board[i+k][j]==0){
  714.                     flag++;
  715.                     break;
  716.                 }
  717.             }
  718.             n=weight(value,flag);
  719.             if(weightBoard[i][j]<n){
  720.                 weightBoard[i][j]=n;
  721.             }
  722.         }
  723.         // '|' direction
  724.         for(i=x,j=RectY1;j<=RectY2;j++){
  725.             if(BoardPanel.board[i][j]!=0){
  726.                 continue;
  727.             }
  728.             value=0;flag=0;
  729.             for(k=1;j-k>=RectY1 && k<5;k++){
  730.                 if(BoardPanel.board[i][j-k]==tcolor){
  731.                     value++;
  732.                     continue;
  733.                 }
  734.                 if(BoardPanel.board[i][j-k]==0){
  735.                     flag++;
  736.                     break;
  737.                 }
  738.             }
  739.             for(k=1;j+k<RectY2 && k<5;k++){
  740.                 if(BoardPanel.board[i][j+k]==tcolor){
  741.                     value++;
  742.                 }
  743.                 if(BoardPanel.board[i][j+k]==0){
  744.                     flag++;
  745.                     break;
  746.                 }
  747.             }
  748.             n=weight(value,flag);
  749.             if(weightBoard[i][j]<n){
  750.                 weightBoard[i][j]=n;
  751.             }
  752.         }
  753.         // '' direction
  754.         for(i=RectX1,j=RectY1;i<=RectX2;i++,j++){
  755.             if(BoardPanel.board[i][j]!=0){
  756.                 continue;
  757.             }
  758.             value=0;flag=0;
  759.             for(k=1;i-k>=RectX1 && k<5 ;k++){
  760.                 if(BoardPanel.board[i-k][j-k]==tcolor){
  761.                     value++;
  762.                     continue;
  763.                 }
  764.                 if(BoardPanel.board[i-k][j-k]==0){
  765.                     flag++;
  766.                     break;
  767.                 }
  768.             }
  769.             for(k=1;i+k<RectX2 && k<5;k++){
  770.                 if(BoardPanel.board[i+k][j+k]==tcolor){
  771.                     value++;
  772.                 }
  773.                 if(BoardPanel.board[i+k][j+k]==0){
  774.                     flag++;
  775.                     break;
  776.                 }
  777.             }
  778.             n=weight(value,flag);
  779.             if(weightBoard[i][j]<n){
  780.                 weightBoard[i][j]=n;
  781.             }
  782.         }
  783.         // '/' direction
  784.         for(i=RectX2,j=RectY1;i>=RectX1;i--,j++){
  785.             if(BoardPanel.board[i][j]!=0){
  786.                 continue;
  787.             }
  788.             value=0;flag=0;
  789.             for(k=1;i+k<=RectX2 && k<5;k++){
  790.                 if(BoardPanel.board[i+k][j-k]==tcolor){
  791.                     value++;
  792.                     continue;
  793.                 }
  794.                 if(BoardPanel.board[i+k][j-k]==0){
  795.                     flag++;
  796.                     break;
  797.                 }
  798.             }
  799.             for(k=1;i-k>=RectX1 && k<5;k++){
  800.                 if(BoardPanel.board[i-k][j+k]==tcolor){
  801.                     value++;
  802.                 }
  803.                 if(BoardPanel.board[i-k][j+k]==0){
  804.                     flag++;
  805.                     break;
  806.                 }
  807.             }
  808.             n=weight(value,flag);
  809.             if(weightBoard[i][j]<n){
  810.                 weightBoard[i][j]=n;
  811.             }
  812.         }
  813.     }
  814.     /**
  815.      * return weight
  816.      * @param count
  817.      * @param flag
  818.      * @return
  819.      */
  820.     private int weight(int count, int flag)
  821.     {
  822.         int weight=0;
  823.         switch(count){
  824.             case 0:{
  825.                 if(flag>0) weight=200;
  826.                 else weight=0;
  827.                 break;
  828.             }
  829.             case 1:{
  830.                 if(flag>0) weight=1000;
  831.                 else weight=0;
  832.                 break;
  833.             }
  834.             case 2:{
  835.                 if(flag>0) weight=5000;
  836.                 else weight=0;
  837.                 break;
  838.             }
  839.             case 3:{
  840.                 if(flag>0) weight=8000;
  841.                 else weight=0;
  842.                 break;
  843.             }
  844.             case 4:{
  845.                 if(flag>0) weight=10000;
  846.                 else weight=0;
  847.                 break;
  848.             }
  849.         }
  850.         return weight;
  851.     }
  852.     /**
  853.      * search all board and find the better count position
  854.      * @param count better position's number default value is 3
  855.      */
  856.     private void getBetter(int count){
  857.         int [][] better = new int [count][2];
  858.         int [][] tempArray = new int [15][15];
  859.         for(int i=0;i<15;i++){
  860.             for(int j=0;j<15;j++){
  861.                 tempArray[i][j]=weightBoard[i][j];
  862.             }
  863.         }
  864.         for(int i=0;i<count;i++){
  865.             getBiggest(tempArray,better[i][0],better[i][1]);
  866.         }
  867.         bestX=better[0][0];bestY=better[0][1];
  868.     }
  869.     /**
  870.      *
  871.      * @param arr
  872.      * @param x
  873.      * @param y
  874.      */
  875.     private void getBiggest(int [][] arr,int x,int y){
  876.         int [] temp=new int[2];
  877.         int swt=arr[0][0],tmp=0;
  878.         for(int i=0;i<15;i++){
  879.             for(int j=0;j<15;j++){
  880.                 if(arr[i][j]>swt){
  881.                     temp[0]=i;temp[1]=j;
  882.                     swt=arr[i][j];
  883.                 }
  884.             }
  885.         }
  886.         x=temp[0];
  887.         y=temp[1];
  888.         arr[x][y]=0;
  889.     }
  890. }///:-)