📄 j_fir_client.java
字号:
} else n = -1;
m_board.updateBoard(x,y,n);
SideDecided = false;
while (!SideDecided)
;
if ((n+2)%2==1) {
if (ColorInHand==1) countTurns = 0; else
if (ColorInHand==2) countTurns = 2;
} else {
countTurns = 1;
}
//确定轮到谁下
GameOver = false;
while (!GameOver) {
//循环下棋
if (ColorInHand>countTurns) {
isReady = false;
do {
if (!m_board.updateCompleted) continue;
q = m_board.getCurrentN();
if (q!=-1) {
if (q>=n+1) {
n++;
x[n] = m_board.getCurrentX(n);
y[n] = m_board.getCurrentY(n);
countTurns++;
m_output.writeObject(PLAYING_COMMAND+Integer.toString(x[n]*LINE_NUM+y[n]));
mb_displayAppend("您下了(双方)第"+(n+1)+"步棋。");
if (m_board.isWinner(ColorInHand)) {
m_output.writeObject(WINNER_DECIDED+"Win");
}
isReady = true;
}
} else n = -1;
} while (!isReady);
}
isReady = false;
while (!isReady)
;
}
while (!ReplayOptionDecided)
;
//等待回复是否重下
if (ReplaySelected) {
InvitationAccepted = false;
m_output.writeObject(PLAY_AGAIN);
mb_displayAppend("您已向"+Opponent[0]+"发出了邀请,请等待回应。");
RequestReplied = false;
while (!RequestReplied)
;
if (InvitationAccepted) readyToPlay = true; else readyToPlay = false;
}
}
}
}
}
catch (Exception e)
{
System.err.println("发生异常:"+e);
e.printStackTrace();
mb_displayAppend("发生异常");
}
}
}
class MessageThread extends Thread //消息线程,专们接收服务器传来的消息并作相应处理
{
public MessageThread() throws IOException
{
start();
}
public void run()
{
String str,str1,str2;
int i,u,v;
StringBuffer mBuf;
try
{
while (isConnected) {
str = (String) m_input.readObject();
//mb_displayAppend("系统消息:"+str); //调试时用
mBuf = new StringBuffer(str);
if (mBuf.indexOf(NAME_LIST_RESPONSE)==0) {
//服务器返回在线空闲用户列表
mBuf.delete(0,NAME_LIST_RESPONSE.length());
if (!Entered[0]) {
userNum = -1;
while (mBuf.indexOf(NAME_LIST)==0) {
mBuf.delete(0,NAME_LIST.length());
i = mBuf.indexOf(NAME_LIST);
userNum++;
if (i==-1) {
userName[userNum] = mBuf.toString();
break;
} else {
userName[userNum] = mBuf.substring(0,i);
mBuf.delete(0,i);
}
}
for (i=0; i<=buttonNum; i++) p[2].remove(b[i]);
buttonNum = userNum;
mb_displayAppend("找到"+(userNum+1)+"个(空闲)在线用户。");
for (i=0; i<=userNum; i++) {
b[i] = new JButton(userName[i]);
b[i].addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton jb = (JButton)e.getSource();
if (!Entered[0]) {
Entered[0] = true;
Opponent[0] = jb.getText();
}
}
}
);
p[2].add(b[i]);
mb_displayAppend(userName[i]);
}
p[2].updateUI();
}
} else
if (mBuf.indexOf(PLAY_REQUEST)==0) {
m_Message = str;
Invited = true;
} else
if (str.equals(REQUEST_ACCEPTED)) {
mb_displayAppend("您的邀请成功了!");
InvitationAccepted = true;
RequestReplied = true;
} else
if (str.equals(REQUEST_REJECTED)) {
Opponent[0] = EXIT_NAME;
mb_displayAppend("对不起,您的邀请被谢绝了。");
InvitationAccepted = false;
RequestReplied = true;
} else
if (str.equals(SIDE_DECISION)) {
J_FIR_Choose jfb = new J_FIR_Choose(myName);
jfb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfb.setSize(250,200);
jfb.setLocation(100,200);
jfb.setVisible(true);
while (jfb.Choice[0]==-1)
;
if (jfb.Choice[0]==0) {
m_output.writeObject(SIDE_COLOR+"BLACK");
mb_displayAppend("您选择了黑棋。");
} else
if (jfb.Choice[0]==1) {
m_output.writeObject(SIDE_COLOR+"WHITE");
mb_displayAppend("您选择了白棋。");
} else {
m_output.writeObject(SIDE_COLOR+"RANDOM");
mb_displayAppend("您选择了随机。");
}
jfb.dispose();
} else
if (mBuf.indexOf(SIDE_COLOR)==0) {
mBuf.delete(0,SIDE_COLOR.length());
str = mBuf.toString();
if (str.equals("BLACK")) {
ColorInHand = 1;
mb_displayAppend("您执黑先下。");
} else
if (str.equals("WHITE")) {
ColorInHand = 2;
mb_displayAppend("您执白后下。");
}
if (ColorInHand==1) JOptionPane.showMessageDialog(null,"您执黑棋。");
else JOptionPane.showMessageDialog(null,"您执白棋。");
for (i=0; i<=buttonNum; i++) p[2].remove(b[i]);
buttonNum = -1;
SideDecided = true;
} else
if (mBuf.indexOf(PLAYING_COMMAND)==0) {
mBuf.delete(0,PLAYING_COMMAND.length());
str = mBuf.toString();
int Data;
Data = Integer.parseInt(str);
if (Data>=0 && Data<=LINE_NUM*LINE_NUM) {
n++;
x[n] = Data/LINE_NUM;
y[n] = Data%LINE_NUM;
m_board.updateBoard(x,y,n);
mb_displayAppend("对方下了(双方)第"+(n+1)+"步棋。");
countTurns--;
isReady = true;
}
} else
if (mBuf.indexOf(WINNER_DECIDED)==0) {
mBuf.delete(0,WINNER_DECIDED.length());
str = mBuf.toString();
if (str.equals("Win")) str = "您";
PopContent = new String(str+"获得了胜利。");
MessageExist = true;
while (MessageExist)
;
J_FIR_TwoChoice jft = new J_FIR_TwoChoice("要保存对局吗?","是","否");
jft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jft.setSize(220,200);
jft.setLocation(450,450);
jft.setVisible(true);
while (jft.u_Choice[0]==-1)
;
if (jft.u_Choice[0]==1) {
jft.dispose();
m_Save();
} else jft.dispose();
ReplayOptionDecided = false;
ReplaySelected = false;
GameOver = true;
readyToPlay = false;
jft = new J_FIR_TwoChoice("再来一局吗?","是的","退出");
jft.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jft.setSize(220,200);
jft.setLocation(450,450);
jft.setVisible(true);
while (jft.u_Choice[0]==-1)
;
if (jft.u_Choice[0]==1) {
ReplaySelected = true;
ReplayOptionDecided = true;
} else
if (jft.u_Choice[0]==-2) {
m_output.writeObject(I_WANT_TO_GO);
LookingForOnliners = true;
mb_displayAppend("您退出了对局室。");
ReplaySelected = false;
ReplayOptionDecided = true;
}
jft.dispose();
n = -1;
m_board.updateBoard(x,y,-1);
} else
if (str.equals(I_WANT_TO_GO)) {
mb_displayAppend(Opponent[0]+"离开了对局室。");
if (ReplaySelected) {
LookingForOnliners = true;
m_output.writeObject(I_WANT_TO_GO);
mb_displayAppend("您离开了对局室。");
}
InvitationAccepted = false;
RequestReplied = true;
n = -1;
m_board.updateBoard(x,y,-1);
} else
if (str.equals(PLAY_AGAIN)) {
mb_displayAppend(Opponent[0]+"也想再下一局,开战吧!");
InvitationAccepted = true;
RequestReplied = true;
n = -1;
m_board.updateBoard(x,y,n);
} else
if (str.equals(EXIT_SERVER)) {
PopContent = "您已退出服务器。欢迎再来!";
MessageExist = true;
while (MessageExist)
;
isConnected = false;
n = -1;
m_board.updateBoard(x,y,n);
//进行其他处理
} else
if (str.equals(DROP_LINE)) {
PopContent = "对方掉线了,您获得了胜利。";
MessageExist = true;
} else
if (mBuf.indexOf(CONTINUE_REQUEST)==0) {
m_Message = str;
Invited = true;
} else
if (mBuf.indexOf(STEP_NUM)==0) {
mb_displayAppend("正在接收对局数据,请稍候...");
mBuf.delete(0,STEP_NUM.length());
str = mBuf.toString();
n = Integer.parseInt(str);
tempN = -1;
} else
if (mBuf.indexOf(STEP_CONTENT)==0) {
mBuf.delete(0,STEP_CONTENT.length());
str = mBuf.toString();
int Data = Integer.parseInt(str);
if (Data>=0 && Data<=LINE_NUM*LINE_NUM) {
tempN++;
x[tempN] = Data/LINE_NUM;
y[tempN] = Data%LINE_NUM;
m_board.updateBoard(x,y,tempN);
if (tempN==n) {
m_output.writeObject(DATA_RECEIVING_COMPLETED);
mb_displayAppend("数据接收完毕。");
}
}
} else
if (str.equals(I_REGRET)) {
PopContent = "对方想悔棋,您是否同意?";
ChoiceExist = true;
} else
if (str.equals(REGRET_AS_YOU_LIKE)) {
PopContent = Opponent[0]+"同意让你悔棋。";
MessageExist = true;
if (n%2==0 && ColorInHand==1) n--; else
if (n%2==0 && ColorInHand==2) { if (n>=1) n-=2; } else
if (n%2==1 && ColorInHand==1) { if (n>=1) n-=2; } else
if (n%2==1 && ColorInHand==2) { if (n>=0) n--; }
m_board.updateBoard(x,y,n);
} else
if (str.equals(NO_REGRET)) {
PopContent = Opponent[0]+"不同意你悔棋。";
MessageExist = true;
} else
{
mb_displayAppend(Opponent[0]+"说:"+str);
}
}
}
catch (Exception ee)
{
System.out.println("读入线程发生异常:"+ee);
ee.printStackTrace();
}
}
}
public static void main(String args[])
{
J_FIR_Client app = new J_FIR_Client();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(690,800);
app.setLocation(605,0);
app.setVisible(true);
app.mb_run();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -