/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Gomoku;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author skyADMIN
 */
public class ChessController extends Thread{
    private int status;
    MyPanel panel;
    BufferedReader input;
    PrintWriter output;
    Socket socket;
    private String ip;
    private String port;
    int[][] arr;
    int[][] opponentArr;
    public ChessController(MyPanel panel,int status){
        this.status = status;
        this.panel=panel;
    }
    public void client(){
        try {
            Socket client=new Socket(ip,Integer.parseInt(port));
            input=new BufferedReader(new InputStreamReader(client.getInputStream()));
            output=new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (UnknownHostException ex) {
            Logger.getLogger(ChessController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ChessController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    public void server(){
        try {
            ServerSocket serverSocket = new  ServerSocket(Integer.parseInt(port));
            Socket client=serverSocket.accept();
            JOptionPane.showMessageDialog(null, "有客户端链接到了本机的"+port+"端口,游戏开始,我方先走.");
            panel.setIsMyTurn(1);
            input=new BufferedReader(new InputStreamReader(client.getInputStream()));
            output=new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (IOException ex) {
            Logger.getLogger(ChessController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
     public void run(){
         if (status == 0) {
             this.server();
         }
         if(status == 1){
             this.client();
         }
        while(true){
            try {
                arr=panel.getArr();
                String myResult=arrToString(arr);
                output.println(myResult);
                output.flush();
                opponentArr=stringToArr(input.readLine());
                panel.setOpponentArr(opponentArr);
                panel.repaint();
                Thread.sleep(1000);
            } catch (Exception ex) {
                Logger.getLogger(MyPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public String arrToString(int[][] arr){
        String temp="";
        int x=arr.length;
        for(int i=0;i<arr.length;i++)
            for(int j=0;j<arr.length;j++)
                temp+=String.valueOf(arr[i][j]);
        if (panel.getIsGo()==1) {
            temp+=";1";
            panel.setIsGo(0);
        }else{
            temp+=";0";
        }
        if (panel.getIsWin()==0) {
            temp+=";0";
        }else{
            temp+=";1";
            panel.setIsWin(0);
        }
        return temp;
    }   
    public int[][] stringToArr(String orgrins){
        String[] ss = orgrins.split(";");
        String s = ss[0];
        if (ss[1].equals("1")) {
            //System.out.println("123");
            panel.setIsMyTurn(1);
        }
        if (ss[2].equals("1")) {
            JOptionPane.showMessageDialog(null, "对方已取得胜利");
        }
        int num=(int)Math.sqrt(s.length());
        int[][] temp=new int[num][num];
        for(int i=0;i<arr.length;i++)
            for(int j=0;j<arr.length;j++)
            {
              temp[i][j]=Integer.parseInt(s.substring(i*num+j, i*num+j+1));
            }
        
        return temp;
    }

    /**
     * @return the ip
     */
    public String getIp() {
        return ip;
    }

    /**
     * @param ip the ip to set
     */
    public void setIp(String ip) {
        this.ip = ip;
    }

    /**
     * @return the port
     */
    public String getPort() {
        return port;
    }

    /**
     * @param port the port to set
     */
    public void setPort(String port) {
        this.port = port;
    }
}

@@ -730,24 +730,50 @@
 pponentArr;%0A
+    private int isOppWin;%0A
     public C
@@ -866,24 +866,46 @@
 anel=panel;%0A
+        isOppWin = 0;%0A
     %7D%0A    pu
@@ -2552,24 +2552,50 @@
 .repaint();%0A
+                isOver();%0A
             
@@ -2768,24 +2768,187 @@
     %7D%0A    %7D%0A
+     %0A    public void isOver()%7B%0A        if (isOppWin == 1) %7B%0A            isOppWin = 0;%0A            JOptionPane.showMessageDialog(null, %22%E5%AF%B9%E6%96%B9%E5%B7%B2%E5%8F%96%E5%BE%97%E8%83%9C%E5%88%A9%22);%0A        %7D%0A    %7D%0A
     %0A    pub
@@ -3752,54 +3752,20 @@
     
-JOptionPane.showMessageDialog(null, %22%E5%AF%B9%E6%96%B9%E5%B7%B2%E5%8F%96%E5%BE%97%E8%83%9C%E5%88%A9%22)
+isOppWin = 1
 ;%0A  

fix a little bug