ChessWZQ.java~1~
上传用户: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. whhile(true){
  341. Object temp = JOptionPane.showInputDialog(null,
  342. "Enter your name ","Welcome ...",
  343. JOptionPane.INFORMATION_MESSAGE,);
  344. if(temp!=null){
  345. wzq.strToCharArray(set.toString(),ms.msg)
  346. break;
  347.                 }
  348.         }
  349.         try{
  350.             // should get server name by command line
  351.             InetAddress addr = InetAddress.getByName(wzq.serverAddress);
  352.             //System.out.println("address "+ addr.toString()+" port: "+PORT);
  353.             socket = new Socket(addr, PORT);
  354.             //System.out.println("set socket successful...");
  355.             out = new ObjectOutputStream(
  356.                 socket.getOutputStream());
  357.             in = new ObjectInputStream(
  358.                 socket.getInputStream());
  359.             ms.type=1;
  360.             try{
  361.                 out.writeObject(ms);
  362.             }catch(IOException e){
  363.                 e.printStackTrace();
  364.             }
  365.             while(true){
  366.                 try {
  367.                     ms = (Message) in.readObject();
  368.                     //System.out.println("get message from server...type = "+ms.type);
  369.                     wzq.doMessage(ms);
  370.                 }
  371.                 catch (ClassNotFoundException e) {
  372.                     e.printStackTrace();
  373.                 }
  374.             }
  375.             //System.out.println("ms.coordinateX + , + msg.coordinateY");
  376.             //System.out.println(ms.color);
  377.             //System.out.println("close ...");
  378.             //socket.close();
  379.         }catch(IOException e){
  380.             e.printStackTrace();
  381.         }
  382.     }
  383.     /**
  384.      * deal with message get from server
  385.      * @param msg
  386.      * @return
  387.      */
  388.     protected int doMessage(Message msg){
  389.         String str=arrayToString(msg.msg);
  390.         //System.out.println("msg name = "+str+" msg.type = "+msg.type);
  391.         switch(msg.type){
  392.             case 2:{//respones of put chessman
  393.                 putChessman(msg);
  394.                 break;
  395.             }
  396.             case 3:{//request from another player
  397.                 requestAnother(msg);
  398.                 break;
  399.             }
  400.             case 4:{ // B deny to play with A
  401.                 getDeny(msg);
  402.                 break;}
  403.             case 5:{ // B accept A's requestion
  404.                 acceptToPlay(msg);
  405.                 break;}
  406.             case 6:{
  407.                 getVictory(msg);
  408.                 break;}
  409.             case 7:{
  410.                 getDisconnection(msg);
  411.                 break;}
  412.             case 9:{//reply of adding new player to all client
  413.                 ///System.out.println("add new player to list type = 9 name = "+str+"msg[0]= "+msg.msg[0]);
  414.                 lItems.add(0,str);
  415.                 break;
  416.             }
  417.             case 10:{ //respones of connecting
  418.                 //System.out.println("add him self  type = 10 name = "+str+"msg[0]= "+msg.coordinateX+msg.msg[0]);
  419.                 label1.setText(str);
  420.                 label6.setText("welcome "+str);
  421.                 lItems.add(0,str);
  422.                 break;
  423.             }
  424.             case 14:{ //B have accept playing with A,then get msg
  425.                       //to set color and settings
  426.                 break;
  427.             }
  428.             case 15:{
  429.                 lItems.clear();
  430.                 break;
  431.             }
  432.             case 17:{
  433.                 getFailed(msg);
  434.                 break;
  435.             }
  436.             case 20:{
  437.                 ptocWin(msg);
  438.                 break;
  439.             }
  440.         }
  441.         return 0; //end correctly
  442.     }
  443.     private void ptocWin(Message ms){
  444.         String str=this.arrayToString(ms.msg);
  445.         JOptionPane.showMessageDialog(null,str+" win the game!","V the game",JOptionPane.INFORMATION_MESSAGE);
  446.         newGame();
  447.     }
  448.     /**
  449.      * failed the game and have a new one
  450.      * type == 17
  451.      * @param msg
  452.      */
  453.     public void getFailed(Message msg){
  454.         bpanel.drawChess(msg.coordinateX,msg.coordinateY);
  455.         JOptionPane.showMessageDialog(null,
  456.                                       "Sorry,You've failed the game",
  457.                                       "Try Again",
  458.                                       JOptionPane.INFORMATION_MESSAGE);
  459.         label3.setText("Player2");
  460.         // have a new game to continue to play
  461.         newGame();
  462.     }
  463.     /**
  464.      * another player send disconnection msg,here will resolve it
  465.      * type ==7
  466.      * @param msg
  467.      */
  468.     public void getDisconnection(Message msg){
  469.         getVictory(msg);
  470.     }
  471.     /**
  472.      * win the game
  473.      * type ==6
  474.      * @param msg
  475.      */
  476.     public void getVictory(Message msg){
  477.         JOptionPane.showMessageDialog(null,
  478.                                       "You  Win  The  Game",
  479.                                       "Congratulations",
  480.                                       JOptionPane.INFORMATION_MESSAGE);
  481.         // have a new game to continue to play
  482.         label3.setText("Player2");
  483.         newGame();
  484.     }
  485.     /**
  486.      * when people win a game ,then he can start a new one
  487.      * @param msg
  488.      */
  489.     public void newGame(){
  490.         jrbBlack.setEnabled(true);
  491.         jrbWhite.setEnabled(true);
  492.         jrbBlack.setSelected(true);
  493.         jrbWhite.setSelected(false);
  494.         list.setEnabled(true);
  495.         setting.setEnabled(true);
  496.         bpanel.clearBoard();
  497.         drawChess(1);
  498.         pColor=1;cColor=2;
  499.         if(ptocFlag==false){
  500.             Message msg = new Message();
  501.             msg.type = 19;
  502.             strToCharArray(name, msg.msg);
  503.             try {
  504.                 out.writeObject(msg);
  505.             }
  506.             catch (IOException e) {
  507.                 e.printStackTrace();
  508.             }
  509.         }
  510.         ptocFlag=false;
  511.         beginFlag=false;
  512.     }
  513.     /**
  514.      * precondition:get a msg from competitor of puting a chessman
  515.      * fundition : deal with it
  516.      * @param msg
  517.      */
  518.     public void putChessman(Message msg){
  519.         if(ptocFlag==false){
  520.             bpanel.updateBoard(msg.coordinateX,msg.coordinateY);
  521.             bpanel.drawChess(msg.coordinateX,msg.coordinateY);
  522.             beginFlag = true;
  523.             return;
  524.         }else{
  525.             // update native board
  526.             // search position to put chessman
  527.         }
  528.     }
  529.     /**
  530.      * A get a Ok msg from B and seting according with B
  531.      * type= 5 B accept to play with A
  532.      * @param msg = B's name
  533.      */
  534.     public void acceptToPlay(Message msg){
  535.         String str=arrayToString(msg.msg);
  536.         String ss=null;
  537.         if(msg.color==1){
  538.             ss = new String("white");
  539.             bpanel.setColor(2);
  540.         }
  541.         else{
  542.             ss = new String("black");
  543.             bpanel.setColor(1);
  544.         }
  545.         JOptionPane.showMessageDialog(null,
  546.                                       "OK. "+str+" have accepted your requestionnYour color is"+ss,
  547.                                       "Game will to begin...",JOptionPane.ERROR_MESSAGE);
  548.         list.setEnabled(false);
  549.         jrbBlack.setEnabled(false);
  550.         jrbWhite.setEnabled(false);
  551.         setting.setEnabled(false);
  552.         beginFlag=true;
  553.     }
  554.     /**
  555.      * A get a deny msg from B
  556.      * type ==4 deny to play
  557.      * @param msg
  558.      */
  559.     public void getDeny(Message msg){
  560.         String str=arrayToString(msg.msg);
  561.         JOptionPane.showMessageDialog(null,
  562.                                       "I'm sorryn"+str+" denied your requestion",
  563.                                       "Sorry...",JOptionPane.ERROR_MESSAGE);
  564.         list.setEnabled(true);
  565.         label3.setText("Player2");
  566.     }
  567.     /**
  568.      * A request play game with B,what below is B done
  569.      * deal with request to paly
  570.      * @param msg = requester 's name
  571.      */
  572.     public void requestAnother(Message msg){
  573.         String str=arrayToString(msg.msg);
  574. //        System.out.print("client requestAnother begin ...");
  575.         int flag =JOptionPane.showConfirmDialog(null,
  576.             "Player "+str+" want to play with younAre you OK?",
  577.             "Play request...",JOptionPane.YES_NO_OPTION,
  578.             JOptionPane.QUESTION_MESSAGE);
  579.         if(flag==0){// press YES
  580.             msg.type=5; //agree with request
  581.             if(msg.color==1){ // msg.color is B 's color ,different with A's
  582.                 drawChess(msg.color);
  583.                 jrbBlack.setSelected(true);
  584.                 jrbWhite.setSelected(false);
  585.                 bpanel.setColor(1);
  586.             }
  587.             else{
  588.                 drawChess(msg.color);
  589.                 jrbWhite.setSelected(true);
  590.                 jrbBlack.setSelected(false);
  591.                 bpanel.setColor(2);
  592.             }
  593. //                System.out.println("B's color is"+msg.color);
  594.             list.setEnabled(false);
  595.             label3.setText(str);
  596.             setting.setEnabled(false);
  597.             jrbBlack.setEnabled(false);
  598.             jrbWhite.setEnabled(false);
  599.             beginFlag=true;
  600.         }
  601.         if(flag==1){// press NO
  602.             msg.type=4; // deny request
  603.         }
  604.         try{
  605.             out.writeObject(msg);
  606.         }catch(IOException e){
  607.             e.printStackTrace();
  608.         }
  609.     }
  610.     public void paint(Graphics g){
  611.         super.paint(g);
  612.         drawChess(bpanel.getColor());
  613.     }
  614.     /**
  615.      * Overridden so we can exit when window is closed
  616.      */
  617.     protected void processWindowEvent(WindowEvent e) {
  618.         super.processWindowEvent(e);
  619.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  620.             System.exit(0);
  621.         }
  622.     }
  623.     // convert string to array which is end with ''
  624.     public void strToCharArray(String str,char [] arr){
  625.         int i;
  626.         for(i=0;i<str.length()&&i<49;i++){
  627.             arr[i] = str.charAt(i);
  628.         }
  629.         arr[i]='';
  630.     }
  631.     /**
  632.      * filter array 's black space which is end of the array
  633.      * @param arr
  634.      * @param str
  635.      */
  636.     public String arrayToString(char [] arr){
  637.         int i=0,length=0;
  638.         while(arr[i]!='' && i<50){
  639.             i++;
  640.         }
  641.         length=i;
  642.         char [] ss = new char[length];
  643.         for(i=0;i<length;i++){
  644.             ss[i]=arr[i];
  645.         }
  646.         String str = new String(ss);
  647.         return str;
  648.         //System.out.println("arraytoString "+str+"length = "+length);
  649.     }
  650.     /**
  651.      * do people play with computer
  652.      */
  653.     public void ptoComputer(){
  654.         int x=0,y=0;
  655.         int position;
  656.         if(pFirst==false){
  657.             x=7;
  658.             y=7;
  659.             bpanel.updateBoard(x,y);
  660.             bpanel.drawChess(x,y);
  661.             beginFlag=true;
  662.         }else{
  663.             beginFlag=true;
  664.         }
  665.     }
  666.     /**
  667.      * choos a best position to put chessman
  668.      * @param x the newest chessman's x coordinate
  669.      * @param y the newest chessman's y coordinate
  670.      */
  671.     private void cPutChess(int x,int y){
  672.         setRect(x,y);
  673.         setWeight(x,y,pColor);
  674.         getBetter(3);
  675.     }
  676.     /**
  677.      * set a rectangle of 9*9
  678.      * @param x center of rectangle
  679.      * @param y center of rectangle
  680.      */
  681.     private void setRect(int x,int y){
  682.         if(x-4>0)  RectX1=x-4;
  683.         else       RectX1=0;
  684.         if(x+4>14) RectX2=14;
  685.         else       RectX2=x+4;
  686.         if(y-4>0)  RectY1=y-4;
  687.         else       RectY1=0;
  688.         if(y+4>14) RectY2=14;
  689.         else       RectY2=y+4;
  690.         if(RectX1>RectY1) RectX1 = x-(y-RectY1);
  691.         else              RectY1 = y-(x-RectX1);
  692.         if(RectX2>RectY2) RectY2 = y+(RectX2-x);
  693.         else              RectX2 = x+(RectY2-y);
  694.     }
  695.     /**
  696.      * set each black position's weight in the rectangle
  697.      * @param x rectangle center's x coordinate
  698.      * @param y rectangle center's x coordinate
  699.      */
  700.     private void setWeight(int x,int y,int tcolor){
  701.         int i=RectX1,j=RectY1,value=0,k=0,n=0,flag=0;
  702.         // '--' direction
  703.         for(i=RectX1,j=y;i<=RectX2;i++){
  704.             if(BoardPanel.board[i][j]!=0){
  705.                 continue;
  706.             }
  707.             value=0;flag=0;
  708.             for(k=1;i-k>=RectX1 && k<5;k++){
  709.                 if(BoardPanel.board[i-k][j]==tcolor){
  710.                     value++;
  711.                     continue;
  712.                 }
  713.                 if(BoardPanel.board[i-k][j]==0){//black space
  714.                     flag++;
  715.                     break;
  716.                 }
  717.             }
  718.             for(k=1;i+k<RectX2 && k<5;k++){
  719.                 if(BoardPanel.board[i+k][j]==tcolor){
  720.                     value++;
  721.                 }
  722.                 if(BoardPanel.board[i+k][j]==0){
  723.                     flag++;
  724.                     break;
  725.                 }
  726.             }
  727.             n=weight(value,flag);
  728.             if(weightBoard[i][j]<n){
  729.                 weightBoard[i][j]=n;
  730.             }
  731.         }
  732.         // '|' direction
  733.         for(i=x,j=RectY1;j<=RectY2;j++){
  734.             if(BoardPanel.board[i][j]!=0){
  735.                 continue;
  736.             }
  737.             value=0;flag=0;
  738.             for(k=1;j-k>=RectY1 && k<5;k++){
  739.                 if(BoardPanel.board[i][j-k]==tcolor){
  740.                     value++;
  741.                     continue;
  742.                 }
  743.                 if(BoardPanel.board[i][j-k]==0){
  744.                     flag++;
  745.                     break;
  746.                 }
  747.             }
  748.             for(k=1;j+k<RectY2 && k<5;k++){
  749.                 if(BoardPanel.board[i][j+k]==tcolor){
  750.                     value++;
  751.                 }
  752.                 if(BoardPanel.board[i][j+k]==0){
  753.                     flag++;
  754.                     break;
  755.                 }
  756.             }
  757.             n=weight(value,flag);
  758.             if(weightBoard[i][j]<n){
  759.                 weightBoard[i][j]=n;
  760.             }
  761.         }
  762.         // '' direction
  763.         for(i=RectX1,j=RectY1;i<=RectX2;i++,j++){
  764.             if(BoardPanel.board[i][j]!=0){
  765.                 continue;
  766.             }
  767.             value=0;flag=0;
  768.             for(k=1;i-k>=RectX1 && k<5 ;k++){
  769.                 if(BoardPanel.board[i-k][j-k]==tcolor){
  770.                     value++;
  771.                     continue;
  772.                 }
  773.                 if(BoardPanel.board[i-k][j-k]==0){
  774.                     flag++;
  775.                     break;
  776.                 }
  777.             }
  778.             for(k=1;i+k<RectX2 && k<5;k++){
  779.                 if(BoardPanel.board[i+k][j+k]==tcolor){
  780.                     value++;
  781.                 }
  782.                 if(BoardPanel.board[i+k][j+k]==0){
  783.                     flag++;
  784.                     break;
  785.                 }
  786.             }
  787.             n=weight(value,flag);
  788.             if(weightBoard[i][j]<n){
  789.                 weightBoard[i][j]=n;
  790.             }
  791.         }
  792.         // '/' direction
  793.         for(i=RectX2,j=RectY1;i>=RectX1;i--,j++){
  794.             if(BoardPanel.board[i][j]!=0){
  795.                 continue;
  796.             }
  797.             value=0;flag=0;
  798.             for(k=1;i+k<=RectX2 && k<5;k++){
  799.                 if(BoardPanel.board[i+k][j-k]==tcolor){
  800.                     value++;
  801.                     continue;
  802.                 }
  803.                 if(BoardPanel.board[i+k][j-k]==0){
  804.                     flag++;
  805.                     break;
  806.                 }
  807.             }
  808.             for(k=1;i-k>=RectX1 && k<5;k++){
  809.                 if(BoardPanel.board[i-k][j+k]==tcolor){
  810.                     value++;
  811.                 }
  812.                 if(BoardPanel.board[i-k][j+k]==0){
  813.                     flag++;
  814.                     break;
  815.                 }
  816.             }
  817.             n=weight(value,flag);
  818.             if(weightBoard[i][j]<n){
  819.                 weightBoard[i][j]=n;
  820.             }
  821.         }
  822.     }
  823.     /**
  824.      * return weight
  825.      * @param count
  826.      * @param flag
  827.      * @return
  828.      */
  829.     private int weight(int count, int flag)
  830.     {
  831.         int weight=0;
  832.         switch(count){
  833.             case 0:{
  834.                 if(flag>0) weight=200;
  835.                 else weight=0;
  836.                 break;
  837.             }
  838.             case 1:{
  839.                 if(flag>0) weight=1000;
  840.                 else weight=0;
  841.                 break;
  842.             }
  843.             case 2:{
  844.                 if(flag>0) weight=5000;
  845.                 else weight=0;
  846.                 break;
  847.             }
  848.             case 3:{
  849.                 if(flag>0) weight=8000;
  850.                 else weight=0;
  851.                 break;
  852.             }
  853.             case 4:{
  854.                 if(flag>0) weight=10000;
  855.                 else weight=0;
  856.                 break;
  857.             }
  858.         }
  859.         return weight;
  860.     }
  861.     /**
  862.      * search all board and find the better count position
  863.      * @param count better position's number default value is 3
  864.      */
  865.     private void getBetter(int count){
  866.         int [][] better = new int [count][2];
  867.         int [][] tempArray = new int [15][15];
  868.         for(int i=0;i<15;i++){
  869.             for(int j=0;j<15;j++){
  870.                 tempArray[i][j]=weightBoard[i][j];
  871.             }
  872.         }
  873.         for(int i=0;i<count;i++){
  874.             getBiggest(tempArray,better[i][0],better[i][1]);
  875.         }
  876.         bestX=better[0][0];bestY=better[0][1];
  877.     }
  878.     /**
  879.      *
  880.      * @param arr
  881.      * @param x
  882.      * @param y
  883.      */
  884.     private void getBiggest(int [][] arr,int x,int y){
  885.         int [] temp=new int[2];
  886.         int swt=arr[0][0],tmp=0;
  887.         for(int i=0;i<15;i++){
  888.             for(int j=0;j<15;j++){
  889.                 if(arr[i][j]>swt){
  890.                     temp[0]=i;temp[1]=j;
  891.                     swt=arr[i][j];
  892.                 }
  893.             }
  894.         }
  895.         x=temp[0];
  896.         y=temp[1];
  897.         arr[x][y]=0;
  898.     }
  899. }///:-)