[Java]局域网五子棋

提示: 下面给的代码有问题哦,可以自己去调试

可用版 http://pan.baidu.com/s/1i4XvawX(退出程序,端口并没有被关闭,可自行修改代码实现)

img

           


 

src

        Server部分

Server_test.java

package 五子棋;

public class Server_test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Server_frame sf =new Server_frame();
    }

}

Server_frame.java

package 五子棋;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.net.ServerSocket;
import java.net.Socket;

import javax.sound.midi.Receiver;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

public class Server_frame extends JFrame {

    public static  boolean isstart = false;
    public static Socket socket;
    Server_panel_center_qipan qipan;
    Server_panel_west_talk talk;
    public static ServerSocket server;
    public Server_frame() {
        qipan = new Server_panel_center_qipan();
        talk = new Server_panel_west_talk();
        this.setSize(800, 600);
        this.setTitle("五子棋_服务器");
        this.add(qipan, BorderLayout.CENTER);
        this.add(talk, BorderLayout.WEST);
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu gameMenu = new JMenu("游戏(G)"); // 创建游戏菜单
        JMenu serMenu = new JMenu("帮助(H)"); // 创建游戏菜单
        gameMenu.setMnemonic('G');
        // 创建游戏子菜单及监听
        JMenuItem connect_ser = new JMenuItem("开启服务器连接服务器", 'F');
        connect_ser.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
                InputEvent.CTRL_MASK));
        connect_ser.addActionListener(new AbstractAction("开启服务器") {
            public void actionPerformed(ActionEvent event) {
                boolean flag = Serverstart();
                if (flag == false) {
                    JOptionPane.showMessageDialog(null, "开启失败", "错误",
                            JOptionPane.ERROR_MESSAGE);
                    return;
                } else {
                
                    //服务器开启成功后开启线程
                    Recive_thred rth = new Recive_thred(socket,
                            Server_panel_west_talk.chantAear);
                    rth.start();
                }

            }
        });

        JMenuItem exitItem = new JMenuItem("退出", 'E');
        exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
                InputEvent.CTRL_MASK));
        exitItem.addActionListener(new AbstractAction("退出") {
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        gameMenu.add(connect_ser);
        gameMenu.addSeparator();
        gameMenu.add(exitItem);
        menuBar.add(gameMenu);            
            //客户端连接服务器后才打开线程
            Recive_thred rth = new Recive_thred(socket,
                    Server_panel_west_talk.chantAear);
            rth.start();
        this.setVisible(true);
    }
    /*
     * 开启服务器,并且等待客户端连接
     */
    public static boolean Serverstart() {
        try {
            // 开启服务器
            server = new ServerSocket(6666);
            isstart = true; // 表示服务器开启了额
            // 发送客户端用户基本信息(用户名和ip地址)
            JOptionPane.showMessageDialog(null, "服务器开始于:"
                    + server.getInetAddress().getLocalHost() + ":"
                    + server.getLocalPort());
            Server_panel_west_talk.chantAear.append("\n" + "服务器开始于:"
                    + server.getInetAddress().getLocalHost() + ":"
                    + server.getLocalPort() + "\n");
            
            socket=server.accept();    //    等待客户端连接
            
            return isstart;
        } catch (Exception e) {
            isstart = false;// 未连接上
            return false;
        }
    }

}

Server_panel_center_qipan.java

package 五子棋;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Server_panel_center_qipan extends JPanel{
    Image board, black, white;
    int hgex, hgey; // 棋子坐标的变量
    int bgex, bgey; // 棋子坐标的变量
    int cou = 0; // 表示连着棋子的数量
    public static boolean isrenshu = false; // renshu=true表示自认输了
    public static boolean ishuiqi = false; // huiqi=true表示自己悔棋了
    public static boolean isok = false; // baiqi表示自己准备好了
    public static boolean ischongxinkaishi = false; // 重新开始?
    public static boolean ishei = false; // 黑棋下了???ishei=true表示下了
    boolean isgamestart = false;// true游戏开始
    private MouseEvent xiaqie;
    ArrayList<String> h_list = new ArrayList<String>(); //
    ArrayList<String> b_list = new ArrayList<String>(); //
    public Server_panel_center_qipan(){
        this.setLayout(null); // 居中
        JButton ks = new JButton("开始/暂停");
        ks.setBounds(10, 460, 100, 25);
        this.add(ks);
        JButton rs = new JButton("认输");
        rs.setBounds(115, 460, 80, 25);
        this.add(rs);
        JButton ht = new JButton("后退");
        ht.setBounds(200, 460, 80, 25);
        this.add(ht);
        JButton cxks = new JButton("重新开始");
        cxks.setBounds(290, 460, 100, 25);
        this.add(cxks);

        hgex = 0;
        hgey = 0;
        bgex = 0;
        bgey = 0;
        try {
            board = ImageIO.read(new File("img/board.gif"));
            black = ImageIO.read(new File("img/black.gif"));
            white = ImageIO.read(new File("img/white.gif"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 鼠标监听器,下棋位置
        MouseAdapter mouse = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                xiaqie=e;    //把鼠标点的位置赋值给全局变量

            }
        };
        // 鼠标监听器--------------开始/暂停
        MouseAdapter start = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                JOptionPane.showMessageDialog(null, "dianjile");
                isok=true;
            }
        };
        MouseAdapter renshu = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // System.out.println("renshu鼠标按下去了");
                if (h_list.size() < 5)
                    JOptionPane.showMessageDialog(null,
                            "这都还没走五步就认输?不给,该谁了,请继续!!");
                else {
                    isrenshu = true;
                }
            }
        };
        // 后退
        MouseAdapter houtui = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                if (h_list.size() == 0 || b_list.size() == 0)
                    JOptionPane.showMessageDialog(null, "你妹哦,这都没开始,哪来的让你毁");
                else {
                    ishuiqi = true;
                }
            }
        };
        // 重新开始
        MouseAdapter chongxinkaishi = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                if (h_list.size() == 0 || b_list.size() == 0)
                    JOptionPane.showMessageDialog(null, "你妹哦,这都没开始,怎么会重新有病把");
                else {
                    isrenshu=true;
                    
                }
            }
        };
        this.addMouseListener(mouse);
        cxks.addMouseListener(chongxinkaishi);
        ks.addMouseListener(start); // 开始按钮与鼠标相连
        rs.addMouseListener(renshu); // 认输按钮与鼠标相连
        ht.addMouseListener(houtui); // 后退按钮与鼠标相连
        
    }
    /*
     * 
     * 获取鼠标位置 保存鼠标位置到数集里面
     */
    public void getmouse(MouseEvent e) {
        if (isgamestart ) {// !ishei表示黑棋没下,到白棋
            int x = e.getX();
            int y = e.getY();
            // 减去边距,除以格子的宽度,取整,得到第几格子
            hgex = (x - 18) / 25;
            hgey = (y - 18) / 25;
            if ((x - 18) % 25 > 12)
                hgex = hgex + 1;
            if ((y - 18) % 25 > 12)
                hgey = hgey + 1;
            // 把格子的x坐标和y坐标变成字符串用逗号连接,保存到list里面
            if (!h_list.contains(hgex + "," + hgey)
                    && !b_list.contains(hgex + "," + hgey)) {
                h_list.add(hgex + "," + hgey);
                ishei = true;        //黑棋下了,
            }
            repaint();
            if (h_list.size() >= 5) {
                heipanduan();
            }

        }

    }
    /*
     * 
     * 画棋盘*/
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(board, 0, 0, null);
        // 黑棋获取鼠标点的位置,画黑棋
        for (int i = 0; i < h_list.size(); i++) {
            String s = h_list.get(i); // 获得集合里面的单个字符串
            String[] a = s.split(",");
            hgex = Integer.parseInt(a[0]);
            hgey = Integer.parseInt(a[1]);
            g.drawImage(black, hgex * 25 + 18 - 12, hgey * 25 + 18 - 12, null);
        }
        // 白棋获取鼠标点的位置,画白棋
        for (int i = 0; i < b_list.size(); i++) {
            String s = b_list.get(i); // 获得集合里面的单个字符串
            String[] b = s.split(",");
            bgex = Integer.parseInt(b[0]);
            bgey = Integer.parseInt(b[1]);
            g.drawImage(white, bgex * 25 + 18 - 12, bgey * 25 + 18 - 12, null);
        }
    }

    // 输赢判断的方法,黑判断
    public void heipanduan() {
        // if (h_list.size() > 5) {// 黑棋至少点了五次之后才执行
        // f判断是白棋还是黑棋,true表示黑棋,false表示白棋
        System.out.println("黑棋判断");
        // 黑棋判断输赢
        for (int i = 0; i < h_list.size(); i++) {
            cou = 0; // 在判断每一个棋子的时候,都要给cou赋值为0
            String s = h_list.get(i); // 获得集合里面的单个字符串
            String[] a = s.split(",");
            hgex = Integer.parseInt(a[0]);
            int tempx = hgex;
            hgey = Integer.parseInt(a[1]);
            int tempy = hgey;
            // 横向判断
            // 黑棋的某个点x坐标+1,Y不变,表示横向判断有没有五子相连
            for (int j = 0; j < 4; j++) {
                hgex++;
                if (h_list.contains(hgex + "," + hgey)) {
                    cou++;
                }
            }
            System.out.println("黑棋横向判断有" + cou + "+1个");
            // 纵向判断
            if (cou < 4) {
                hgex = tempx;
                hgey = tempy;
                cou = 0;
                for (int j = 0; j < 4; j++) {
                    hgey++;
                    if (h_list.contains(hgex + "," + hgey))
                        cou++;
                }
            }
            System.out.println("黑棋纵向判断有" + cou + "+1个");
            // 右上方向判断
            if (cou < 4) { // 小余4表示横向没有五子,就把cou值设为0,重新开始
                cou = 0;
                hgex = tempx;
                hgey = tempy;
                for (int j = 0; j < 4; j++) {
                    hgey--;
                    hgex++;
                    if (h_list.contains(hgex + "," + hgey))
                        cou++;
                }
            }
            System.out.println("黑棋右上判断有" + cou + "+1个");
            // 右下判断
            if (cou < 4) {
                cou = 0;
                hgex = tempx;
                hgey = tempy;
                for (int j = 0; j < 4; j++) {
                    hgey++;
                    hgex++;
                    if (h_list.contains(hgex + "," + hgey))
                        cou++;
                }
            }
            System.out.println("黑棋右下判断有" + cou + "+1个");
            if (cou == 4) {
                JOptionPane.showMessageDialog(null, "黑棋赢啦");
                isgamestart = false;
            }
        }
    }
}

Server_panel_west_talk.java

package 五子棋;

import java.awt.TextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Server_panel_west_talk extends JPanel{
    public static String who="服务器说:";
    public static  TextArea chantAear;
    private JTextField txt_message;
    private String message="ok"; // 要发送客户端的内容
    public static Socket client;
    Send_message sendmessage;        //发送的方法
    public Server_panel_west_talk(){
            sendmessage = new Send_message(message, Server_frame.socket);
            JButton jb_send = new JButton("发送");
            txt_message = new JTextField("请输入你要发的内容");
            chantAear = new TextArea("------------------\n", 15, 40);
            MouseAdapter send = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    // TODO Auto-generated method stub
                    super.mouseClicked(e);
                    send(); // 服务在自己的textarea输出信息
                }
            };
            
            jb_send.addMouseListener(send);
            this.add(chantAear);
            this.add(txt_message);
            this.add(jb_send);
    }


    private void send() {
        // TODO Auto-generated method stub
        if (!Server_frame.isstart) {
            JOptionPane.showMessageDialog(null, "服务器还未启动,不能发送消息!", "错误",
                    JOptionPane.ERROR_MESSAGE);
            return;
        } else {
            String message = txt_message.getText();
            if (message == null || message.equals("")) {
                JOptionPane.showMessageDialog(null, "消息不能为空!", "错误",
                        JOptionPane.ERROR_MESSAGE);
                return;
            }
            chantAear.append("\n服务器说:" + txt_message.getText() + "");
            message = "CHANT" + "-" +who+ txt_message.getText() + "\n";
            //System.out.println("服务器要发送的的message为"+message);
            sendmessage.send(message, Server_frame.socket);
        }
    }

}

        Client部分

Client_test.java

package 五子棋;

public class Client_test {
    public static void main(String[] args) {
        Client_frame cf=new Client_frame();
    }
}

Client_frame.java

package 五子棋;

import java.awt.BorderLayout;
import java.awt.Frame;

import javax.swing.JFrame;

public class Client_frame extends JFrame{
    Client_panel_center_qipan qipan;
    Cilent_panel_west_talk talk;
    public Client_frame(){
        qipan=new Client_panel_center_qipan(); 
        talk=new Cilent_panel_west_talk();
        this.setSize(1000, 600);
        //设置关闭
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //设置居中
    //    this.setLocationRelativeTo(null);
        this.setTitle("五子棋_客户端");
        this.add(qipan,BorderLayout.CENTER); 
        this.add(talk,BorderLayout.WEST);
        
        this.setVisible(true);
    }
}

Client_panel_center_qipan.java

package 五子棋;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Client_panel_center_qipan extends JPanel{

    Image board, black, white;
    int bgex, bgey; // 白棋子坐标的变量
    int hgex, hgey; // 黑棋子坐标的变量
    static boolean xiaqi = true; // xiaqu=true,黑棋下,否则不能下白棋下
    int cou = 0; // 表示连着棋子的数量
    static int c=1;
    static boolean f = true; // 判断是白棋还是黑棋,true表示黑棋,false表示白棋
    static boolean isgamestart = false;// true游戏开始
    private static String message;        //发送的消息
    Send_message sendmessage;        //发送的方法
    public static boolean isrenshu = false; // renshu=true表示自认输了
    public static boolean ishuiqi = false; // huiqi=true表示自己悔棋了
    public static boolean isok = false; // baiqi表示自己准备好了
    public static boolean ischongxinkaishi = false; // 重新开始?
    public static boolean isbai = false; // 白棋下了??isbai=true
    private MouseEvent xiaqie;    //下白棋的时候,白棋点的鼠标位置的全局变量
    ArrayList<String> h_list = new ArrayList<String>(); //
    ArrayList<String> b_list = new ArrayList<String>(); //

    public Client_panel_center_qipan() {
        sendmessage = new Send_message(message, Cilent_panel_west_talk.socket);
        this.setLayout(null); // 居中
        JButton ks = new JButton("开始/暂停");
        ks.setBounds(10, 460, 100, 25);
        this.add(ks);
        JButton rs = new JButton("认输");
        rs.setBounds(115, 460, 80, 25);
        this.add(rs);
        JButton ht = new JButton("后退");
        ht.setBounds(200, 460, 80, 25);
        this.add(ht);
        JButton cxks = new JButton("重新开始");
        cxks.setBounds(290, 460, 100, 25);
        this.add(cxks);

        bgex = 0;
        bgey = 0;
        hgex = 0;
        hgey = 0;
        try {
            board = ImageIO.read(new File("img/board.gif"));
            black = ImageIO.read(new File("img/black.gif"));
            white = ImageIO.read(new File("img/white.gif"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 鼠标监听器,下棋位置
        MouseAdapter mouse = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // System.out.println("xiaqi鼠标按下去了");
                if (isok && Server_panel_center_qipan.isok)
                    xiaqie=e;        //将鼠标点的位置赋值给全局变量
                    getmouse(xiaqie);
            }

        };
        // 鼠标监听器--------------开始/暂停
        MouseAdapter start = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // System.out.println("kaishi鼠标按下去了");
                message="ANNIU"+"-"+"START";
                sendmessage.send(message, Cilent_panel_west_talk.socket);
            }
        };
        MouseAdapter renshu = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // System.out.println("renshu鼠标按下去了");
                if (h_list.size() < 5)
                    JOptionPane.showMessageDialog(null,
                            "这都还没走五步就认输?不给,该谁了,请继续!!");
                else {
                    if (h_list.size() < 5)
                        JOptionPane.showMessageDialog(null,
                                "这都还没走五步就认输?不给,该谁了,请继续!!");
                    else {
                        isrenshu = true;
                    }
                }
            }
        };
        // 后退
        MouseAdapter houtui = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // int x=b_list.size();
                if (h_list.size() == 0 || b_list.size() == 0)
                    JOptionPane.showMessageDialog(null, "你妹哦,这都没开始,哪来的让你毁");
                else {
                    ishuiqi = false;
                }
            }
        };
        // 重新开始
        MouseAdapter chongxinkaishi = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                // int x=b_list.size();
                if (h_list.size() == 0 || b_list.size() == 0)
                    JOptionPane.showMessageDialog(null, "你妹哦,这都没开始,怎么会重新有病把");
                else {
                    isrenshu = true;
                }
            }
        };
        this.addMouseListener(mouse);
        ks.addMouseListener(start); // 开始按钮与鼠标相连
        rs.addMouseListener(renshu); // 认输按钮与鼠标相连
        ht.addMouseListener(houtui); // 后退按钮与鼠标相连
        cxks.addMouseListener(chongxinkaishi);

    }
public static boolean   clientisok(){
    if(c==2)
        isok=true;
    return isok;
}
    /*
     * 
     * 获取鼠标位置 保存鼠标位置到数集里面
     */
    public void getmouse(MouseEvent e) {
        if (isgamestart && !isbai && Server_panel_center_qipan.ishei) {// !ishei表示黑棋没下,到白棋
            int x = e.getX();
            int y = e.getY();
            // 减去边距,除以格子的宽度,取整,得到第几格子
            bgex = (x - 18) / 25;
            bgey = (y - 18) / 25;
            if ((x - 18) % 25 > 12)
                bgex = bgex + 1;
            if ((y - 18) % 25 > 12)
                bgey = bgey + 1;
            // 把格子的x坐标和y坐标变成字符串用逗号连接,保存到list里面
            if (!b_list.contains(hgex + "," + hgey)
                    && !b_list.contains(hgex + "," + hgey)) {
                b_list.add(bgex + "," + bgey);
                isbai = true;
                Server_panel_center_qipan.ishei = false;
            }
            repaint();
            if (h_list.size() >= 5) {
                baipanduan();
            }

        }

    }

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        g.drawImage(board, 0, 0, null);
        // 画黑棋
        for (int i = 0; i < h_list.size(); i++) {
            String s = h_list.get(i); // 获得集合里面的单个字符串
            String[] a = s.split(",");
            hgex = Integer.parseInt(a[0]);
            hgey = Integer.parseInt(a[1]);
            g.drawImage(black, hgex * 25 + 18 - 12, hgey * 25 + 18 - 12, null);
        }
        // 画白棋
        for (int i = 0; i < b_list.size(); i++) {
            String s = b_list.get(i); // 获得集合里面的单个字符串
            String[] b = s.split(",");
            bgex = Integer.parseInt(b[0]);
            bgey = Integer.parseInt(b[1]);
            g.drawImage(white, bgex * 25 + 18 - 12, bgey * 25 + 18 - 12, null);
        }
    }

    /*
     * 
     * 白棋判断输赢
     */
    public void baipanduan() {
        if (isgamestart == true) {
            while (true) {
                System.out.print("开始判断了");

                System.out.println("f=false,白棋下了");

                // if (h_list.size() > 5) {// 白棋至少点了五次之后才执行
                // f判断是白棋还是黑棋,true表示黑棋,false表示白棋
                System.out.println("白棋判断");
                // 白棋判断输赢
                for (int i = 0; i < b_list.size(); i++) {
                    cou = 0; // 在判断每一个棋子的时候,都要给cou赋值为0
                    String s = b_list.get(i); // 获得集合里面的单个字符串
                    String[] a = s.split(",");
                    bgex = Integer.parseInt(a[0]);
                    int tempx = bgex;
                    bgey = Integer.parseInt(a[1]);
                    int tempy = bgey;
                    // 横向判断
                    // 黑棋的某个点x坐标+1,Y不变,表示横向判断有没有五子相连
                    for (int j = 0; j < 4; j++) {
                        bgex++;
                        if (b_list.contains(bgex + "," + bgey)) {
                            cou++;
                        }
                    }
                    System.out.println("白棋横向判断有" + cou + "+1个");
                    // 纵向判断
                    if (cou < 4) {
                        bgex = tempx;
                        bgey = tempy;
                        cou = 0;
                        for (int j = 0; j < 4; j++) {
                            bgey++;
                            if (b_list.contains(bgex + "," + bgey))
                                cou++;

                        }
                    }
                    System.out.println("白棋纵向判断有" + cou + "+1个");
                    // 右上方向判断
                    if (cou < 4) { // 小余4表示横向没有五子,就把cou值设为0,重新开始
                        cou = 0;
                        bgex = tempx;
                        bgey = tempy;
                        for (int j = 0; j < 4; j++) {
                            bgey--;
                            bgex++;
                            if (b_list.contains(bgex + "," + bgey))
                                cou++;

                        }
                    }
                    System.out.println("白棋右上判断有" + cou + "+1个");
                    // 右下判断
                    // 右下判断
                    if (cou < 4) {
                        cou = 0;
                        bgex = tempx;
                        bgey = tempy;
                        for (int j = 0; j < 4; j++) {
                            bgey++;
                            bgex++;
                            if (b_list.contains(bgex + "," + bgey))
                                cou++;

                        }
                    }
                    System.out.println("白棋右下判断有" + cou + "+1个");
                    if (cou == 4) {
                        JOptionPane.showMessageDialog(null, "白棋赢啦");
                        isgamestart = false;
                        return;
                    }

                }

            }

        }
    }
}

Cilent_panel_west_talk.java

package 五子棋;

import java.awt.TextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Cilent_panel_west_talk extends JPanel {

    public static boolean connect = false; // 给出连接服务器的boolean,true表示连接成功了
    static TextArea chatAear; // 所有消息内容区域
    public static Socket socket; // 连接服务器的socket = new
                        // Socket(serverip.getText(), 5555);
    private String message="ok"; // 要发送给服务器的内容
    Send_message sendmessage;        //发送的方法
    private String who = "客户端说:";
    private JTextField txt_message; // 将要发送给服务器的内容
    private JTextField serverip; // 服务器的IP地址

    public Cilent_panel_west_talk() {
        sendmessage = new Send_message(message, socket);
        chatAear = new TextArea("----------------------\n", 15, 40);
        txt_message = new JTextField("请输入你要发送的消息");
        JButton jb_connectserver = new JButton("连接服务器");
        serverip = new JTextField("127.0.0.1");
        JButton jb_send = new JButton("发送");
        MouseAdapter connect = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                if (connectserver()) {
                    
                    JOptionPane.showMessageDialog(null, "我成功连接!!!!");
                    message = "CHANT" + "-" + "我是客户端,我已经连接成功了" + "\n";
                    System.out.println("客户端的message为"+message);
                    //客户端连接服务器后才打开线程
                    Recive_thred rth = new Recive_thred(socket,
                            chatAear);
                    rth.start();
                    sendmessage.send(message, socket);
                }
            }

        };
        // 给服务器发送消息按钮
        MouseAdapter send = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseClicked(e);
                chatAear.append("\n客户端说"+txt_message.getText());
                message = "CHANT" + "-" + who + txt_message.getText() + "\n";
                System.out.println("客户端要发送的的message为"+message);
                sendmessage.send(message, socket);
            }
        };
        this.add(serverip);
        jb_connectserver.addMouseListener(connect);
        this.add(jb_connectserver);

        jb_send.addMouseListener(send);
        this.add(chatAear);
        this.add(txt_message);
        this.add(jb_send);

    }

    public boolean connectserver() {
        try {
            // 获取输入的IP地址,并且进行连接
            socket = new Socket(serverip.getText(), 6666);
            connect = true;
            return connect;
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            chatAear.append("\n连接失败");
        }
        return false;
    }
}

      公共部分

Recive_thred.java

package 五子棋;

import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JTextField;

public class Recive_thred extends Thread {
    private Socket socket;
    TextArea chantAear;

    public Recive_thred(Socket socket, TextArea chantAear) {
        this.socket = socket;
        this.chantAear = chantAear;

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        InputStream is;
        try {
            is = socket.getInputStream();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is));
            while (true) {
                String msg = reader.readLine();
                System.out.println("recive线程类接受到的信息" + msg);
                String[] split = msg.split("-");
                System.out.println("split[0]==" + split[0] + "==");
                System.out.println("split[1]==" + split[1] + "==");
                if (split[0] == "ANNIU") {
                    // 按钮控制
                    if (split[1].equals("START")) {
                        // 对方按下开始按钮
                        System.out.println("客户端按下开始了");
                    } else if (split[1].equals("TOUXIANG")) {
                        // 按下认输按钮
                    } else if (split[1].equals("HUIQI")) {
                        // 按下悔棋按钮
                    } else if (split[1].equals("CHONGXINLAI")) {
                        // 按下重新来按钮
                    }
                } 
                if (split[0].equals("CHANT")) {
                    // 聊天控制
                    System.out.println(split[1]);
                    // chantAear.append(split[1]);
                    chantAear.append("\n"+split[1]);
                } 
                if (split[0] == "XIAQI")// a==2
                {
                    // 下棋控制
                } 
            //    System.out.println("recive类接受错误");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

Send_message.java

package 五子棋;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class Send_message {
    Socket socket;
    String message;

    public Send_message(String message, Socket socket) {
        this.message = message;
        this.socket = socket;
    }

    public void send(String message, Socket socket) {
        OutputStream os;
        try {
            os = socket.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
            System.out.println("send类里面要发送给对方的消息是"+message);    
            //用输出流把它给输出去
            bw.write(message);
            //令起一行
            bw.newLine();
            bw.flush(); // 清空缓存
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

posted on 2016-09-11 10:22  losbyday  阅读(3965)  评论(0编辑  收藏  举报

导航