java——棋牌类游戏斗地主(webddz1.0)之二
package com.ddz;
import java.io.ObjectInputStream;
import java.net.DatagramSocket;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 上午11:19:29<br>
* @author daicy
***************************************************************************/
public interface Msg {
/**
* 该你叫地主
*/
public static final int CALL = 1;
/**
* 开始游戏了
*/
public static final int NEWGAME = 2;
/**
* 出牌
*/
public static final int SENDEDPOKER = 3;
/**
* 出牌
*/
public static final int RESTART = 4;
/**
* 地主是
*/
public static final int LORDIS = 5;
/**
* 该你出牌
*/
public static final int YOURTURN = 6;
/**
* 该你出牌
*/
public static final int WIN = 7;
/**
* 发送数据
* @param ds
* @param IP
* @param udpPort
*/
public void send(DatagramSocket ds, String IP, int udpPort);
/**
* 接收并处理数据
* @param dis
*/
public void parse(ObjectInputStream dis);
}
package com.ddz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import com.GamePanel;
import common.NetClient;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 上午11:19:17<br>
* @author daicy
***************************************************************************/
public class LordIsMsg implements Msg {
protected int msgType = Msg.LORDIS;
protected int playerId ;
private GamePanel tc;
private NetClient thread;
/**
* 根据tank的信息构建消息
* @param tank
*/
public LordIsMsg(int playerId) {
this.playerId = playerId;
}
/**
* 根据消息产生的场所构建新的消息
* @param tc
*/
public LordIsMsg(GamePanel tc,NetClient thread) {
this.tc = tc;
this.thread = thread;
}
public int getMsgType() {
return msgType;
}
public void setMsgType(int msgType) {
this.msgType = msgType;
}
public int getPlayerId() {
return playerId;
}
public void setPlayerId(int playerId) {
this.playerId = playerId;
}
public GamePanel getTc() {
return tc;
}
public void setTc(GamePanel tc) {
this.tc = tc;
}
/**
* 发送相关的消息
* @param ds 通过该socket发送数据
* @param IP 数据的目标IP
* @param udpPort 数据的目标端口
*/
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream dos;
try {
dos = new ObjectOutputStream(baos);
dos.writeObject(getMsgType());
dos.writeObject(playerId);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 分析接收到的消息数据
* @param dis 接收到的消息数据的输入流
*/
public void parse(ObjectInputStream dis) {
try {
int id = (Integer) dis.readObject();
thread.setlord(id);
tc.gameState = 2;
if(id==tc.id){
tc.reButtonVisible(true);
}
tc.reButtonName();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ddz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import com.GamePanel;
import com.SocketData;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 上午11:19:36<br>
* @author daicy
***************************************************************************/
public class NewGameMsg implements Msg {
protected int msgType = Msg.NEWGAME;
protected SocketData socketData ;
private GamePanel tc;
/**
* 根据tank的信息构建消息
* @param tank
*/
public NewGameMsg(SocketData socketData) {
this.socketData = socketData;
}
/**
* 根据消息产生的场所构建新的消息
* @param tc
*/
public NewGameMsg(GamePanel tc) {
this.tc = tc;
}
public int getMsgType() {
return msgType;
}
public void setMsgType(int msgType) {
this.msgType = msgType;
}
public GamePanel getTc() {
return tc;
}
public void setTc(GamePanel tc) {
this.tc = tc;
}
/**
* 发送相关的消息
* @param ds 通过该socket发送数据
* @param IP 数据的目标IP
* @param udpPort 数据的目标端口
*/
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream dos ;
try {
dos = new ObjectOutputStream(baos);
dos.writeObject(getMsgType());
dos.writeObject(socketData);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 分析接收到的消息数据
* @param dis 接收到的消息数据的输入流
*/
public void parse(ObjectInputStream dis) {
try {
socketData = (SocketData) dis.readObject();
//socketData.reverse(tc);
socketData.initData(tc);
tc.reStart();
//tc.InitData(socketData.getCards());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ddz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.List;
import com.GamePanel;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 上午11:20:04<br>
* @author daicy
***************************************************************************/
public class SendPorkerMsg implements Msg {
int msgType = Msg.SENDEDPOKER;
int playerId ;
List<String> sendList;
GamePanel tc;
/**
* 根据tank的信息构建消息
* @param tank
*/
public SendPorkerMsg(int playerId,List<String> sendList) {
this.playerId = playerId;
this.sendList = sendList;
}
/**
* 根据消息产生的场所构建新的消息
* @param tc
*/
public SendPorkerMsg(GamePanel tc) {
this.tc = tc;
}
/**
* 发送相关的消息
* @param ds 通过该socket发送数据
* @param IP 数据的目标IP
* @param udpPort 数据的目标端口
*/
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream dos ;
try {
dos = new ObjectOutputStream(baos);
dos.writeObject(msgType);
dos.writeObject(playerId);
dos.writeObject(sendList);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 分析接收到的消息数据
* @param dis 接收到的消息数据的输入流
*/
public void parse(ObjectInputStream dis) {
try {
int id = (Integer) dis.readObject();
sendList = (List<String>) dis.readObject();
this.tc.sendCards(id, sendList);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ddz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import com.GamePanel;
import common.NetClient;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 上午11:19:17<br>
* @author daicy
***************************************************************************/
public class WinMsg implements Msg {
protected int msgType = Msg.WIN;
protected int playerId ;
private GamePanel tc;
private NetClient thread;
/**
* 根据tank的信息构建消息
* @param tank
*/
public WinMsg(int playerId) {
this.playerId = playerId;
}
/**
* 根据消息产生的场所构建新的消息
* @param tc
*/
public WinMsg(GamePanel tc,NetClient thread) {
this.tc = tc;
this.thread = thread;
}
public int getMsgType() {
return msgType;
}
public void setMsgType(int msgType) {
this.msgType = msgType;
}
public int getPlayerId() {
return playerId;
}
public void setPlayerId(int playerId) {
this.playerId = playerId;
}
public GamePanel getTc() {
return tc;
}
public void setTc(GamePanel tc) {
this.tc = tc;
}
/**
* 发送相关的消息
* @param ds 通过该socket发送数据
* @param IP 数据的目标IP
* @param udpPort 数据的目标端口
*/
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream dos;
try {
dos = new ObjectOutputStream(baos);
dos.writeObject(getMsgType());
dos.writeObject(playerId);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 分析接收到的消息数据
* @param dis 接收到的消息数据的输入流
*/
public void parse(ObjectInputStream dis) {
try {
int id = (Integer) dis.readObject();
tc.setWinById(id);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.ddz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import com.GamePanel;
/***************************************************************************
* TODO
* <br>Created on 2013-7-3 上午11:19:44<br>
* @author daicy
***************************************************************************/
public class YourTurnMsg implements Msg {
protected int msgType = Msg.YOURTURN;
protected int playerId ;
private GamePanel tc;
/**
* 根据tank的信息构建消息
* @param tank
*/
public YourTurnMsg(int playerId) {
this.playerId = playerId;
}
/**
* 根据消息产生的场所构建新的消息
* @param tc
*/
public YourTurnMsg(GamePanel tc) {
this.tc = tc;
}
public int getMsgType() {
return msgType;
}
public void setMsgType(int msgType) {
this.msgType = msgType;
}
public int getPlayerId() {
return playerId;
}
public void setPlayerId(int playerId) {
this.playerId = playerId;
}
public GamePanel getTc() {
return tc;
}
public void setTc(GamePanel tc) {
this.tc = tc;
}
/**
* 发送相关的消息
* @param ds 通过该socket发送数据
* @param IP 数据的目标IP
* @param udpPort 数据的目标端口
*/
public void send(DatagramSocket ds, String IP, int udpPort) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream dos;
try {
dos = new ObjectOutputStream(baos);
dos.writeObject(getMsgType());
dos.writeObject(playerId);
} catch (IOException e) {
e.printStackTrace();
}
byte[] buf = baos.toByteArray();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
new InetSocketAddress(IP, udpPort));
ds.send(dp);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 分析接收到的消息数据
* @param dis 接收到的消息数据的输入流
*/
public void parse(ObjectInputStream dis) {
try {
int id = (Integer) dis.readObject();
if (getTc().id == id) {
getTc().turnIndex = id;
getTc().reButtonVisible(true);
if(this.getTc().gameState == 1){
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果你喜欢本文, 请长按二维码,关注公众号 分布式编程.
作者:分布式编程
出处:https://zthinker.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。