public void run() { int j; while (true) { try { CountDownSem.acquire(); if (title.getTurn() == 0) // 刚开始游戏,初始化题目 { for (j = 0; j <= 5; j++) { if (!players[j].equals("虚位以待")) break; } title.setDrawer(players[j]); title.setId(j + 1); title.setTurn(1); title.randomkey(); broadcast(6, title); broadcast(11, scores); } broadcast(2, "-------回合开始-------\n"); timeVoid(); } catch (InterruptedException e) { e.printStackTrace(); } } }
public synchronized void run() { int messageType; String buf; PaintMessage paint; int j, k, num, tmp; boolean state = true; try { readyState[id - 1] = 0; // 进入等待房间 broadcast(4, players); // 当前客户端叫什么名字 /*remoteOut.writeInt(12); remoteOut.flush(); remoteOut.writeUnshared(name);*/ // 大家的名字和等待状态 /*broadcast(4,players); broadcast(9,readyState);*/ while (state) { messageType = remoteIn.readInt(); switch (messageType) { case 1: // 画板上画的像素点 { paint = (PaintMessage) remoteIn.readUnshared(); broadcast(1, paint); break; } case 2: // 聊天框中内容 { buf = (String) remoteIn.readUnshared(); if (buf.equals(title.getKey() + "\n") && id != title.getId() && right[id - 1] == 0) { System.out.println(port + ":" + buf); buf = "(**回答正确**)\n"; right[id - 1] = 1; if (first == 0) { // 第一个人答对 scores[id - 1] += 2; scores[title.getId() - 1] += 3; first = 1; } else { // 非第一个答对的,画的与猜的各加1分 scores[id - 1] += 1; scores[title.getId() - 1] += 1; } // 要重发成绩 broadcast(11, scores); } else if (buf.equals(title.getKey() + "\n") && id != title.getId() && right[id - 1] == 1) buf = "(*@#$*/%(*!)\n"; if (buf.equals(title.getKey() + "\n") && id == title.getId()) buf = "(*@#$*/%(*!)\n"; SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); buf = "[" + df.format(new Date()) + "]" + players[id - 1] + ":" + buf; broadcast(2, buf); for (j = 0; j <= 5; j++) { if (readyState[j] != -1 && right[j] == 0 && j != (title.getId() - 1)) break; } if (j > 5) // 大家都已经答对了 countOver = true; break; } case 5: // 清屏 broadcast(5); break; case 7: // 等待界面,当前用户准备 { readyState[id - 1] = 1; // 检测是否能开始 for (j = 0, num = 0, k = 0; j <= 5; j++) { if (readyState[j] != -1) num++; // 统计等待房间内人数 if (readyState[j] == 0) k++; // 统计没准备的人数 } if (num >= 2 && k == 0) // 可以开始了 readyState[6] = 1; // 传一下准备状态 broadcast(9, readyState); if (readyState[6] == 1) { // 大家的名字 broadcast(4, players); System.out.println("游戏开始!"); CountDownSem.release(); // 开始倒计时 } break; } case 8: // 等待界面,当前用户退出 { readyState[id - 1] = -1; players[id - 1] = "虚位以待"; System.out.println("端口号" + port + "的玩家退出了房间"); state = false; broadcast(4, players); clients.removeElement(remoteOut); // 再检测一下是否符合开始条件 for (j = 0, num = 0, k = 0; j <= 5; j++) { if (readyState[j] != -1) num++; // 统计等待房间内人数 if (readyState[j] == 0) k++; // 统计没准备的人数 } if (num >= 2 && k == 0) // 可以开始了 readyState[6] = 1; // 传一下准备状态 broadcast(9, readyState); if (readyState[6] == 1) { // 大家的名字 broadcast(4, players); System.out.println("游戏开始!"); CountDownSem.release(); // 开始倒计时 } break; } case 12: // 传入用户输入的昵称 name = (String) remoteIn.readUnshared(); players[id - 1] = name; broadcast(4, players); broadcast(9, readyState); break; // case n+1 } } } catch (Exception e) { System.out.println(e.getMessage() + ": 端口为" + port + "的玩家退出了游戏。。"); players[id - 1] = "虚位以待"; readyState[id - 1] = -1; scores[id - 1] = 0; for (tmp = 0; tmp <= 5; tmp++) { // 判断是不是最后一个人 if (readyState[tmp] != -1) break; } if (tmp > 5) readyState[6] = 0; String s = name + "离开了房间。。。\n"; broadcast(2, s); broadcast(4, players); broadcast(11, scores); clients.removeElement(remoteOut); if (id == title.getId()) // 画的人退出了 countOver = true; } }