手机网络象棋开发(3):公用类介绍

共用类主要包括Player,Queue,Message

这些类相对简单。

查看有关代码就可以理解。

后续重点介绍Desk, Umpire类
package net.tuolian.newserver;

import java.io.DataOutputStream;
import java.io.OutputStream;


/**
 * player对象
 * 1. 构造对象
 * 2. 获得桌子getDesk
 * 3. init, 初始化消息队列data.clear(), start = true
 * 4. getDataInputStream, getDataOutputStream 输入输出流
 * 5. reset 复位:start = false, 
 * 
 * @author sean
 *
 */
public class Player {
	private int ID = 1;
	private String IP = "";
	private int PORT = 9999;
	private Desk desk;
	public Queue data;
	private String Color = "";
	public boolean start = false;
	/**
	 * 
	 */
	public Player(){
		desk = null;
		data = new Queue();
	}
	
	/**
	 * 
	 * @param ip
	 * @param p
	 */
	public Player(String ip, int p){
		IP = ip;
		PORT = p;
		desk = null;
		data = new Queue();
	}
	
	/**
	 * 构造方法
	 * @param userName
	 * @param password
	 */
	public Player(String userName, String password) {
		// TODO Auto-generated constructor stub
		data = new Queue();
	}
	
	
	public void setDesk(Desk d) {
		desk = d;
	}
	public Desk getDesk() {
		return desk;
	}
	public boolean equals(String ip) {
		if (IP.equals(ip))
			return true;
		else
			return false;
	}
	public boolean equals(Player p) {
		if (IP.equals(p.getIP()) && PORT == p.getPort())
			return true;
		else
			return false;
	}
	
	public String getIP() {
		return IP;
	}
	
	public int getPort() {
		return PORT;
	}
	
	public void setID(int id) {
		ID = id;
	}
	
	public int getID() {
		return ID;
	}
	
	public boolean isStart() {
		return start;
	}
	
	public void init() {
		start = false;
		data.clear();
		IP="";
		PORT=0;
	}
	public void reset()	{
		Color = "";
		start = false;
	}

	public void start() {
		start = true;
	}
	
	public void setStart(boolean start) {
		this.start = start;
	}
	
	public String getColor() {
		return Color;
	}
	
	public void setColor(String color) {
		Color = color;
	}

	private OutputStream outputStream;
	/**
	 * 返回数据输出流
	 * @return 
	 */
	public DataOutputStream getOutputStream() {
		// TODO Auto-generated method stub
		return new DataOutputStream(outputStream);
	}

	/**
	 * 设置数据输出流
	 * @param outputStream
	 */
	public void setOutputStream(OutputStream outputStream) {
		// TODO Auto-generated method stub
		this.outputStream = outputStream;
	}
}


package net.tuolian.newserver;

/**
 * 队列类
 * @author sean
 * 1. 队列构造函数
 * 2. push,将对象放入第一个位置
 * 3. pop, 将第一个对象取出
 * 4. clear, 清空对象
 * 5. empty, 判断是否为空
 */
public class Queue extends java.util.Vector {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public class EmptyQueueException extends java.lang.RuntimeException {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public EmptyQueueException() {
			super();
		}
	}

	public Queue() {
		super();
	}

	/**
	 * 添加元素到队列
	 * @param x
	 */
	public synchronized void push(Object x) {
		System.out.println("queue: is ready to add msg : " + ((Message)x).getMessage());
		super.addElement(x);
	}

	/**
	 * 弹出第一个元素
	 * @return
	 */
	public synchronized Object pop() {
		if (this.empty())
			throw new EmptyQueueException();
		Object x = super.elementAt(0);
		System.out.println("queue: is ready to remove msg : " + ((Message)x).getMessage());
		//第一个元素
		super.removeElementAt(0);
		return x;
	}

	/**
	 * 队列第一个元素
	 * @return
	 */
	public synchronized Object front() {
		if (this.empty())
			throw new EmptyQueueException();
		return super.elementAt(0);
	}
	
	/**
	 * 检查队列是否为空
	 * @return
	 */
	public boolean empty() {
		return this.isEmpty();
	}

	/**
	 * 清空队列
	 */
	public synchronized void clear() {
		super.removeAllElements();
	}

	/**
	 * 搜索元素的索引号
	 * @param x
	 * @return
	 */
	public int search(Object x) {
		return super.indexOf(x);
	}

}



package net.tuolian.newserver;

/**
 * 
 * @author sean
 *
 */
public class Message {	
	
	public static final int REGISTER_REQ 	 = 1000;
	public static final int START_REQ 		 = 1001;
	public static final int DESKS_REQ	 	 = 1002;
	public static final int MOVE_REQ 		 = 1003;
	public static final int TAKE_REQ		 = 1004;
	public static final int EXITGAME_REQ     = 1008;
	public static final int EXIT_REQ 		 = 1009;
	
	public static final int DESKS_RESP 	 	 = 1100;
	public static final int UPDATEDESK_RESP  = 1101;
	public static final int GAME_RESP 		 = 1102;	

	
	int cmdType;
	String message;
	int playerId;
	
	/**
	 * 
	 * @param cmdType
	 * @param message
	 */	
	public Message(int cmdType, String message) {
		// TODO Auto-generated constructor stub
		this.cmdType = cmdType;
		this.message = message;
		
	}

	/**
	 * 返回消息内容
	 * @return
	 */
	public String getMessage() {
		// TODO Auto-generated method stub
		return message;
	}

	public void setPlayerId(int id) {
		// TODO Auto-generated method stub
		this.playerId = id;
	}

	public int getCmdType() {
		return cmdType;
	}

	public void setCmdType(int cmdType) {
		this.cmdType = cmdType;
	}

	public int getPlayerId() {
		return playerId;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}




注:很多代码参考了java项目全程实录

你可能感兴趣的:(.net)