添加状态检测

main
dai_48k 2024-01-24 16:57:58 +08:00
parent 429f53c7bb
commit b092402eaf
2 changed files with 54 additions and 13 deletions

View File

@ -18,12 +18,14 @@ public class MainFrame extends JFrame {
Cross[][] map = new Cross[9][9]; Cross[][] map = new Cross[9][9];
Cross now = Cross.NONE; Cross now = Cross.NONE;
private String tip = "---";
public MainFrame(ObjectInputStream oi, ObjectOutputStream os) { public MainFrame(ObjectInputStream oi, ObjectOutputStream os) {
now = (Cross) Client.recMsg(oi); now = (Cross) Client.recMsg(oi);
System.out.println(now); System.out.println(now);
setTitle(now == Cross.BLACK ? "●" : "○"); setTitle(now == Cross.BLACK ? "●" : "○");
setSize(200,200 + BASE_HEIGHT); setSize(200,230 + BASE_HEIGHT);
addMouseListener(new MouseAdapter() { addMouseListener(new MouseAdapter() {
@Override @Override
@ -50,8 +52,14 @@ public class MainFrame extends JFrame {
while (true) { while (true) {
try { try {
String o = (String) Client.recMsg(oi); String o = (String) Client.recMsg(oi);
if (o.startsWith("INFO:")) {
tip = o;
} else {
map = DataUtil.rawData(o); map = DataUtil.rawData(o);
}
repaint(); repaint();
} catch (Exception e){ } catch (Exception e){
e.printStackTrace(); e.printStackTrace();
System.out.println("连接断开"); System.out.println("连接断开");
@ -64,7 +72,7 @@ public class MainFrame extends JFrame {
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
g.setColor(Color.white); g.setColor(Color.white);
g.fillRect(0,BASE_HEIGHT,200,200); g.fillRect(0,BASE_HEIGHT,200,230);
g.setColor(Color.lightGray); g.setColor(Color.lightGray);
for (int i = 1; i <= 9; i++) { for (int i = 1; i <= 9; i++) {
@ -81,6 +89,8 @@ public class MainFrame extends JFrame {
} }
} }
} }
g.drawString(tip, BASE_FROM, 200 + BASE_HEIGHT);
} }
@Override @Override

View File

@ -53,6 +53,26 @@ public class UserThread extends Thread{
e.printStackTrace(); e.printStackTrace();
} }
{
boolean allAlive = true;
for (UserThread u: users) {
if (!u.isAlive) allAlive = false;
System.out.print(u.isAlive + " ");
}
if (users.size() == 2 && allAlive) {
users.forEach(u -> {
try {
if (u.isAlive) u.sendMsg("INFO:可以开局啦!");
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
// 不间断接收消息 // 不间断接收消息
Thread thread = new Thread(() -> { Thread thread = new Thread(() -> {
while (true) { while (true) {
@ -61,6 +81,15 @@ public class UserThread extends Thread{
NewClick newClick = (NewClick) o; NewClick newClick = (NewClick) o;
if (newClick.color == (cur.get() ? Cross.BLACK : Cross.WHITE) && newClick.color == self){ if (newClick.color == (cur.get() ? Cross.BLACK : Cross.WHITE) && newClick.color == self){
boolean allAlive = true;
for (UserThread u: users) {
if (!u.isAlive) allAlive = false;
}
if (!allAlive || users.size() != 2) {
sendMsg("INFO:对方未在线");
} else {
boolean result = CalDataUtil.draw(newClick, map); boolean result = CalDataUtil.draw(newClick, map);
if (result) { if (result) {
cur.set(!cur.get()); cur.set(!cur.get());
@ -74,10 +103,12 @@ public class UserThread extends Thread{
}); });
} }
} }
}
} catch (Exception e){ } catch (Exception e){
System.out.println("::" + e.getMessage()); System.out.println("::" + e.getMessage());
System.out.println("连接断开"); System.out.println("连接断开");
isAlive = false; isAlive = false;
break; break;
} }
} }