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