📄 messageprocess.java
字号:
package client;
public class MessageProcess
{
public static java.util.Vector<step> greenstep;//client
public static java.util.Vector<step> redstep;//server
public static boolean analyse(String message)
{
if(message.startsWith(Passwords.TALK))
{
md_Chat(message.substring(Passwords.cmdlength));
return true;
};//IF TALK
if(message.startsWith(Passwords.MOVE))
{
String a = message.substring(Passwords.cmdlength, Passwords.cmdlength+2);
String b = message.substring(Passwords.cmdlength+2,Passwords.cmdlength+3);
String c = message.substring(Passwords.cmdlength+3,Passwords.cmdlength+4);
System.out.println("a = "+a+" b = "+b+" c = "+c);
int id = Integer.parseInt(a);
int x = Integer.parseInt(b);
int y = Integer.parseInt(c);
md_Move(id, x, y);
return true;
};//
if(message.startsWith(Passwords.END))
{
return false;
};//
if(message.startsWith(Passwords.BACK))
{
//String a = message.substring(Passwords.cmdlength, Passwords.cmdlength+1);
//System.out.println("a = "+a);
md_Back(); // 0 server(red) want back, 1 client(green) want back
return true;
};//
if(message.startsWith(Passwords.AGAIN));
{
md_Again();
return true;
}
//return true;
}// bool analyse()
public static void md_Back()
{
System.out.println("Back Button Event");
System.out.println("ChessBoard.movelog.size():"+ChessBoard.movelog.size());
if(ChessBoard.movelog.isEmpty())
{
System.err.println("the state is ready but hasn't begin to box");
return;
}
step temp = ChessBoard.movelog.remove(ChessBoard.movelog.size() - 1);
temp.OutputStep();
Main.boardthread.log2Ta.append("BACK:\n "+ChessBoard.chess[temp.schess].name+ " move back to ("+temp.prex+" , "+temp.prey+") from ("+temp.sx+" , "+temp.sy+")\n");
ChessBoard.moment[temp.sx][temp.sy] = temp.sprechess;
ChessBoard.moment[temp.prex][temp.prey] = temp.schess;
ChessBoard.chess[temp.schess].x = temp.prex;
ChessBoard.chess[temp.schess].y = temp.prey;
if(temp.sprechess != 0)
{
ChessBoard.chess[temp.sprechess].x = temp.sx;
ChessBoard.chess[temp.sprechess].y = temp.sy;
ChessBoard.chess[temp.sprechess].alive = true;
}
Main.boardthread.repaint();
System.out.println("OK: Back Button Event");
}
public static void md_Again()
{
System.out.println("Agian Button Event");
ChessBoard.initChessman();
Main.boardthread.log2Ta.append("a new box begin\n");
Main.boardthread.log2Ta.append("\n-----------------------------------\n");
Main.boardthread.repaint();
System.out.println("OK: Again Button Event");
}
public static void md_movePath(int id , int x, int y)
{
System.out.println("md_movePath("+id+" , "+x+" , "+y+" )");
if(id == 0) return;
ChessBoard.chess[id].ax = x-ChessBoard.offset;
ChessBoard.chess[id].ay = y-ChessBoard.offset;
Main.boardthread.repaint();
}
public static boolean md_Move(int id, int x, int y)
{
if(ChessBoard.chess[id].alive == false)
{
System.out.println("error found md_move chess["+id+"] is not alive");
return false;
}
if(ChessRules.isLegal(id,ChessBoard.chess[id].x, ChessBoard.chess[id].y, x, y))
{
//System.out.println("chess["+id+"] move from ("+ChessBoard.chess[id].x+" , "+ChessBoard.chess[id].y+" ) to ("+x+" , "+y+" ) ");
ChessBoard.moment[ChessBoard.chess[id].x][ChessBoard.chess[id].y] = 0;
//set preplace to be zero(no chessman)
ChessBoard.movelog.add(new step(id, x, y,ChessBoard.chess[id].x,ChessBoard.chess[id].y,ChessBoard.moment[x][y]));
if(ChessBoard.moment[x][y]==0)
{
Main.boardthread.log2Ta.append(ChessBoard.chess[id].name+" : ("+ChessBoard.chess[id].x+" , "+ChessBoard.chess[id].y+")-->("+x+" , "+y+")\n");
}
else
{
Main.boardthread.log2Ta.append(ChessBoard.chess[id].name+" : ("+ChessBoard.chess[id].x+" , "+ChessBoard.chess[id].y+")-->("+x+" , "+y+") eat "+ChessBoard.chess[ChessBoard.moment[x][y]].name+"\n");
}
ChessBoard.chess[id].x = x;
ChessBoard.chess[id].y = y;
if(ChessBoard.moment[x][y] != 0)
{//there are chess in the preplace
int preid = ChessBoard.moment[x][y];
ChessBoard.chess[preid].x = -2;
ChessBoard.chess[preid].y = -2;
ChessBoard.chess[preid].alive = false;
//a chess was eaten
}//eat enemy's chess
ChessBoard.moment[x][y] = id;
//set now place to be new chess
Main.boardthread.repaint();
ChessBoard.chess[id].ax = -1;
ChessBoard.chess[id].ay = -1;
System.out.println("OK: move"+" chessman["+id+"] to ("+x+" , "+ y+")");
return true;
}//if legal
else //ilegal
{
ChessBoard.chess[id].ax = -1;
ChessBoard.chess[id].ay = -1;
Main.boardthread.repaint();
System.err.println("md_move():move ileagel: can't move"+" chessman["+id+"] from ("+ChessBoard.chess[id].x+" , "+ChessBoard.chess[id].y+") to ("+x+" , "+ y+")");
return false;
}
}//md_move
public static void md_Chat(String sentence)
{
Main.boardthread.logTa.append(sentence);
Main.boardthread.repaint();
}//md_chat()
}
class step
{
int schess = 0;
int sx = -1;
int sy = -1;
int prex = -1;
int prey = -1;
int sprechess = 0; //zero means no eating enemy's chess
public step(int ischess, int isx, int isy, int iprex, int iprey, int isprechess)
{
schess = ischess;
sx = isx;
sy = isy;
prex = iprex;
prey = iprey;
sprechess = isprechess;
}
public step(int ischess, int isx, int isy, int iprex, int iprey)
{
schess = ischess;
sx = isx;
sy = isy;
prex = iprex;
prey = iprey;
}
public void OutputStep()
{
System.out.println("chess["+schess+"] move to ("+sx+" , "+sy+" ) from ("+prex+" , "+prey+" ) , eat enemy's chess["+sprechess+"]");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -