📄 analysemsg0000.java~1~
字号:
package chess;
import java.net.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class AnalyseMsg
{
String username, second, third; //解析字符串得到的值
ThreadPool QueueWithComputer; //与机对战的用户队列,人人对战的队列
// ChessRoom Room;
Vector QueueWithMan ;
static int Waiting = 0;
static String[] One = new String[2];
MainFrame mf;
public AnalyseMsg(String s, ThreadPool Queue, MainFrame mf,Vector Q)
{ //s一般格式为:from.to.message--<
//Room = cr;
this.QueueWithMan=Q;
this.mf = mf;
MessageClass mc = new MessageClass(s);
username = mc.first; //第一部分肯定是username
second = mc.second; //根据第二部分判断
third = mc.third;
//System.out.println(username);
//System.out.println(second);
//System.out.println(third);
QueueWithComputer = Queue;
int tag = 0;
if (second.equals("#"))
{
tag = 1; //如果是和电脑下棋
}
else if (second.equals("#come#"))
{
tag = 2; //如果是在某人来了,等待或与一等待的人直接开始
}
else if (second.equals("#leave#"))
{
tag = 3; //如果是离开某桌子
}
else
{ //在否则,second就是另一个客户的id了
tag = 4;
}
switch (tag)
{
case 1: //*人机下:1111.#.3.4 //A在3,4落子
int index = -1;
//mf.log.AddLine("用户名是:" + username);
mf.log.AddLine("用户名是:" + username);
if (QueueWithComputer.capacity() > 0)
{ //如果用户列表不空,作查找
index = QueueWithComputer.getIndex(username); //查找用户所在位置
}
if (index < 0)
{ //如果没找到,则添加
mf.log.AddLine("列表中没有该用户,添加:" + username);
index = QueueWithComputer.addUser(username); //index是刚添加进来的元素的下标
}
//将消息传给指定的线程
//从消息中提取用户的x和y
mf.log.AddLine("用户:" + username + "在列表中的位置是" + index);
//mf.log.AddLine(third + ". ");
MessageClass temp = new MessageClass(third + ". ");
int x = new Integer(temp.first).intValue();
int y = new Integer(temp.second).intValue();
mf.log.AddLine("传入的坐标是:" + x + "," + y);
ChessCompute now = QueueWithComputer.getThreadAt(index); //从线程池里面找到需要的线程
now.setInput(x, y);
mf.log.AddLine("计算开始");
now.run();
mf.log.AddLine("计算完毕");
x = now.getBestX();
y = now.getBestY();
mf.log.AddLine("传出的坐标将是:" + x + "," + y);
SendOneMsg("wzq.#." + username + "." + new Integer(x).toString() + "." +
new Integer(y).toString()); ////给服务器的反馈
break;
case 2: //1111.#come#.
if (0 == QueueWithMan.size() % 2)
{ //用户数是偶数
User u = new User(username);
u.setAction("wait");
QueueWithMan.add(u);
this.SendOneMsg("wzq.wait." + u.User);
}
else
{ //用户数为奇数
User u = new User(username);
u.setAction("play");
int i = QueueWithMan.size() - 1;
User tu = (User) QueueWithMan.elementAt(i);
tu.setOP(username);
tu.setAction("play");
u.setOP(tu.User);
QueueWithMan.add(u);
this.SendOneMsg("wzq.connect." + tu.User + "." + tu.UserWith);
}
// setOne(username);
//setWaiting();
//One[Waiting - 1] = username;
//this.SendOneMsg(new Integer(Waiting).toString());
break;
case 3: //1111.#leave#.3
int ql = QueueWithMan.size();
if (ql > 0)
{
for (int i = 0; i < ql; i++)
{
User iunow = (User) QueueWithMan.elementAt(i);
if (iunow.User.equals(username))
{
QueueWithMan.remove(i); //remove the user
for (int j = 0; j < ql - 1; j++)
{
User junow = (User) QueueWithMan.elementAt(i);
if (iunow.User.equals(junow.User))
{
QueueWithMan.remove(j); //remove the user who play with the user who was removed
}
}
}
}
}
break;
}
}
private static void setOne(String name)
{
One[Waiting] = name;
}
private static void setWaiting()
{
Waiting++;
if (Waiting == 2)
{
SendOneMsg("wzq.connect." + One[0] + "." + One[1]);
One[0] = null;
One[1] = null;
Waiting = 0;
}
}
public static void SendOneMsg(String s)
{
SocketConn sc = new SocketConn("localhost", 6005);
sc.send(s);
sc.close();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -