纯JAVA写的socket局域网斗地主游戏

来源:互联网 发布:创建windows窗口程序 编辑:程序博客网 时间:2024/06/07 13:05

前两天没项目做,就想着写点什么。
就写了这个小游戏没事跟同事玩玩。
5块钱一把,不带开挂的。

先来几张效果图

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

废话不多,开篇纪念。

部分源码

连接服务器类

public class Connect {    public static final String IP_ADDR = "192.168.0.38";// 服务器地址    public static final int PORT = 8866;// 服务器端口号    private static Socket socket;    public static SendMes sendMes;    public static boolean connect(){        try {            socket = new Socket(IP_ADDR, PORT);        } catch (Exception e) {            JOptionPane.showMessageDialog(null, new JLabel("<html><h1><font color='red'>"+e.getMessage()+"</font></h1></html>"), "错误", JOptionPane.ERROR_MESSAGE);         }        new ReadMes(socket).start();        sendMes=new SendMes(socket);        sendMes.setMsg(Fuckland.jTextField.getText().trim());        sendMes.start();        System.out.println("连接成功");        return true;    }}

接受服务器消息线程

public class ReadMes extends Thread {    private Socket socket;    public ReadMes(Socket socket) {        this.socket = socket;    }    public void run() {        DataInputStream objInput = null;        try {            objInput = new DataInputStream(socket.getInputStream());            while (true) {                ......            }        } catch (Exception e) {            e.printStackTrace();            if(e.getMessage()!=null)                JOptionPane.showMessageDialog(null, new JLabel("<html><h1><font color='red'>"+e.getMessage()+"</font></h1></html>"), "错误", JOptionPane.ERROR_MESSAGE);                      } finally {            try {                if(objInput!=null)                    objInput.close();            } catch (IOException e) {            }        }    }}

发送消息给服务器线程

public class SendMes extends Thread {    private Socket socket;    public SendMes(Socket socket) {        this.socket = socket;    }    private String msg;    public void run() {        DataOutputStream objOut=null;        try {            objOut = new DataOutputStream(socket.getOutputStream());            while(true){                if(msg!=null&&msg.length()>0){                    objOut.writeUTF(msg);                    msg=null;                }                Thread.sleep(100);            }        } catch (Exception e) {            JOptionPane.showMessageDialog(null, new JLabel("<html><h1><font color='red'>"+e.getMessage()+"</font></h1></html>"), "错误", JOptionPane.ERROR_MESSAGE);         }finally{            try {                objOut.close();            } catch (IOException e) {            }        }    }    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }}

判断是不是比上家的牌大

public class IsBigger {    public static boolean isBigger(List<Poker> leftList,List<Poker> rightList,List<Poker> choose){        //如果上家不要 或者第一把出  list=null        if(leftList==null||leftList.size()==0){            if(rightList==null||rightList.size()==0){                //两家都不要                return true;            }else{                if(isRealBigger(rightList, choose)){                    return true;                }                return false;            }        }else{            if(isRealBigger(leftList, choose)){                return true;            }            return false;        }    }    public static boolean isRealBigger(List<Poker> leftList,List<Poker> choose){        // 首先判断牌型是不是一样        String paiXing = IsTruePoker.isTruePoker(leftList);        if (paiXing.equals(IsTruePoker.isTruePoker(choose))) {            // 根据牌型来判断大小            if (IsTruePoker.DANZHANG.equals(paiXing)) {                // 单张                if (isBiggerLast(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.WANGZHA.equals(paiXing)) {                // 王炸                // 开挂了            } else if (IsTruePoker.DUIZI.equals(paiXing)) {                // 对子                if (isBiggerLast(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.SANGETOU.equals(paiXing)) {                // 三张                if (isBiggerLast(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.SANDAIYI.equals(paiXing)) {                // 三带一                if (isBiggerSan(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.SANDAIYIDUI.equals(paiXing)) {                // 三带一对                if (isBiggerSan(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.ZHADAN.equals(paiXing)) {                // 炸弹                if (isBiggerLast(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.SHUNZI.equals(paiXing)) {                // 顺子                if (isBiggerLast(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.LIANDUI.equals(paiXing)) {                // 连对                if (isBiggerLast(leftList, choose)) {                    return true;                }                return false;            } else if (IsTruePoker.SHUANGFEI.equals(paiXing)) {                // 双飞                if (isBiggerSan(leftList, choose)) {                    return true;                }                return false;            }         }else if(choose.size()==2){            //判断是不是王炸            if(IsTruePoker.isWangZha(choose)){                return true;            }            return false;        } else if(choose.size()==4){            //判断是不是炸弹            if(IsTruePoker.isSame(choose, 4)){                return true;            }            return false;        }        return false;    }    public static boolean isBiggerLast(List<Poker> list,List<Poker> choose){        if(list.get(list.size()-1).getColor()<choose.get(choose.size()-1).getColor()){            return true;        }        return false;    }    public static boolean isBiggerSan(List<Poker> list,List<Poker> choose){        int a=san(list);        int b=san(choose);        if(a==-1||b==-1){            return false;        }        if(b>a){            return true;        }        return false;    }    public static int san(List<Poker> list){        for(int i=0;i<list.size()-2;i++){            int a=list.get(i).getColor();            int b=list.get(i+1).getColor();            int c=list.get(i+2).getColor();            if(a==b&&a==c){                return a;            }        }        return -1;    }}

感兴趣的朋友可以下源码玩玩 也可以自己修改修改
源码地址:http://download.csdn.net/detail/laigezao/9136609

1 0