今日小弟学习java之余做了一个java五子棋网络对战,可是有些问题,想请教高手next是用作下棋的,connection是用作连接的,play是游戏运行时监控按键的,看是否悔棋、认输之类操作的。问题如下
play()线程没加时,运行正常,加了play()线程之后,会出现有时可以连续下2个棋子,然而对方只接收到最后一个public class PeoplePlayer { ChessBoard cb;//棋盘
DatagramSocket ds; // 套接字(UDP)
String sendIP; // 发送IP
int sendPort = 3000; // 发送端口
boolean ifConnect; // 是否已经连接
Thread connect, next, playitem; public PeoplePlayer(ChessBoard cb) {
this.cb = cb;
this.ifConnect = false;
try {
ds = new DatagramSocket(3000);
} catch (Exception e) {
e.printStackTrace();
}
this.connection();
} // 启动连接线程
public void connection() {
connect = new Thread(new Runnable() {
public void run() {
while (!ifConnect) {
int i = receive(); // 请求游戏,你同意吗?
if (i == -1) {
int j = JOptionPane.showConfirmDialog(cb.game, "玩家"
+ sendIP + "请求与你联机,是否同意", "联机对战",
JOptionPane.YES_NO_OPTION); // 同意回应并开始游戏
if (j == JOptionPane.YES_OPTION) {
cb.setIfBlack(true);
cb.game.startGame2();
ifConnect = true;
play();
send(-2);
return;
} // 不同意
else {
send(-3);
}
} // 对方同意开始游戏拉
else if (i == -2) {
JOptionPane.showMessageDialog(cb.game, "对方同意开始游戏了:)",
"best wishs", JOptionPane.DEFAULT_OPTION);
ifConnect = true;
play();
return;
} // 很遗憾,对方不想跟你玩
else if (i == -3) {
JOptionPane.showMessageDialog(cb.game, "对方不想跟你联机:)",
"很遗憾", JOptionPane.DEFAULT_OPTION);
} else {
System.out.println("connection得到信息");
}
}
}
});
connect.start();
} public void play() {
playitem = null;
playitem = new Thread(new Runnable() {
public void run() {
while (ifConnect) {
System.out.println("进入play()");
int i = receive();
// 请求悔棋
if (i == -4) {
// 不同意
if (JOptionPane.showConfirmDialog(cb.game, "对方想悔棋",
"请求", JOptionPane.WARNING_MESSAGE) != JOptionPane.YES_OPTION) {
send(-5);
}
// 同意
else {
cb.qiGes[cb.qiZis[cb.lastQiZi].x][cb.qiZis[cb.lastQiZi].y] = cb.noneState;
cb.lastQiZi--;
cb.qiGes[cb.qiZis[cb.lastQiZi].x][cb.qiZis[cb.lastQiZi].y] = cb.noneState;
cb.lastQiZi--;
cb.repaint();
send(-6);
}
return;
} else if (i == -5) {
JOptionPane.showMessageDialog(cb.game, "悔棋请求不被允许",
"waring", JOptionPane.YES_OPTION);
next();
break;
} else if (i == -6) {
JOptionPane.showMessageDialog(cb.game, "可以悔棋了",
"massage", JOptionPane.YES_OPTION);
cb.back();
next();
break;
} else if(i == -7){
if (cb.ifBlack)
cb.setStr("游戏结束,黑子胜");
else
cb.setStr("游戏结束,白子胜");
cb.setGameOver(true);
cb.game.endGame();
}else {
System.out.println("play中得到信息");
System.out.println("play中" + cb.ifBlack);
System.out.println("play中" + cb.lastQiZi);
playitem = null;
next();
break;
}
}
}
});
playitem.start();
} // 开始游戏
public void start() {
cb.setIfBlack(false);
this.sendIP = JOptionPane.showInputDialog(this.cb.game, "输入连接方IP",
"联机对战", JOptionPane.YES_OPTION);
ChatPanel.IP = this.sendIP;
this.send(-1); // 请求连接
} // 收发的数据是int型的
public void send(int position) {
String str = "" + position;
try {
DatagramPacket dpSend = new DatagramPacket(str.getBytes(), str
.getBytes().length, InetAddress.getByName(this.sendIP),
this.sendPort);
ds.send(dpSend);
System.out.println("发送到" + this.sendIP + ":" + str);
} catch (Exception ex) {
ex.printStackTrace();
}
} public int receive() {
byte[] buf = new byte[4];
DatagramPacket dpReceive = new DatagramPacket(buf, buf.length);
try {
ds.receive(dpReceive);
} catch (Exception e) {
// 套接字已经关闭
if (ds.isClosed())
return -3;
e.printStackTrace();
} sendIP = dpReceive.getAddress().getHostAddress();
ChatPanel.IP = this.sendIP;
sendPort = dpReceive.getPort();
// this.chatPanel.setIP(sendIP,name);
String str = new String(buf, 0, dpReceive.getLength());
int i = Integer.parseInt(str);
System.out.println("接收于" + sendIP + ":" + i);
return i;
} public void next() {
next = null;
next = new Thread(new Runnable() {
public void run() {
System.out.println("next正在执行");
connect = null;
System.out.println("connection:" + ifConnect);
if (cb.lastQiZi > -1){
System.out.println("send:" +
15 * cb.qiZis[cb.lastQiZi].x + cb.qiZis[cb.lastQiZi].y);
send(15 * cb.qiZis[cb.lastQiZi].x + cb.qiZis[cb.lastQiZi].y);
}
if (cb.gameOver) {
ifConnect = false;
connection();
return;
}
int i = receive();
System.out.println("   i 为    " + i);
cb.putDown(new ZuoBiao(i / 15, i % 15), !cb.ifBlack);
cb.canPut = true;
if (cb.gameOver) {
ifConnect = false;
connection();
return;
}
}
});
next.start();
play(); } public void back() { if (JOptionPane.showConfirmDialog(cb.game, "确定悔棋?", "悔棋",
JOptionPane.WARNING_MESSAGE) != JOptionPane.YES_OPTION) {
return;
}
System.out.println("悔棋啦");
send(-4); } public void loss() {
send(-7);
} public void qiuHe() {
} public void exit() { }
}