添加状态检测

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

View File

@ -53,6 +53,26 @@ public class UserThread extends Thread{
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(() -> {
while (true) {
@ -61,23 +81,34 @@ public class UserThread extends Thread{
NewClick newClick = (NewClick) o;
if (newClick.color == (cur.get() ? Cross.BLACK : Cross.WHITE) && newClick.color == self){
boolean result = CalDataUtil.draw(newClick, map);
if (result) {
cur.set(!cur.get());
users.forEach(u -> {
try {
if (u.isAlive) u.sendMsg(DataUtil.parseData(map, newClick));
} catch (IOException e) {
e.printStackTrace();
}
});
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);
if (result) {
cur.set(!cur.get());
users.forEach(u -> {
try {
if (u.isAlive) u.sendMsg(DataUtil.parseData(map, newClick));
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
}
} catch (Exception e){
System.out.println("::" + e.getMessage());
System.out.println("连接断开");
isAlive = false;
break;
}
}