gpt4 book ai didi

java - Tourbased 游戏 - Threading java - (轮到你 sleep 了?)

转载 作者:行者123 更新时间:2023-12-01 18:41:30 25 4
gpt4 key购买 nike

我正在编写基于巡回赛的游戏(情书的简化版),并且我已经编写了简单的客户端、服务器和卡片的完整逻辑等。但是现在出现了一个问题,如何仅从一个线程客户端获取输入并且不接受消息来自其他人(总结将有 5 个连接的客户端) - 在完成他的巡回赛之后如何通知下一个玩家(他的线程)输入数据......
这是我的客户的代码:

import ...;

public class Console_client {


final static int ServerPort = XXXX;
final static String ServerAddress = "";
BufferedReader in;
PrintWriter out;
Scanner sc = new Scanner(System.in);

/** Runs the client as an application with a closeable frame.*/
public static void main(String[] args) throws Exception {
Console_client client = new Console_client();
client.run();
}

/** Connects to the server then enters the processing loop.*/
private void run() throws IOException {

Socket socket = new Socket(ServerAddress, ServerPort);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);

Thread sendMessage = new Thread(() -> {
while (true) {
out.println(sc.nextLine());
}
});

Thread readMessage = new Thread(() -> {
while (true) {
try {
System.out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
});
sendMessage.start();
readMessage.start();
}
}

我的服务器类看起来像:
    public static void main(String[] args) throws Exception {

System.out.println("The chat server is running.");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
和处理程序:
    private static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;

public Handler(Socket socket) {
this.socket = socket;
}

public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("CONNECT");


while (true) {
name = in.readLine();
if (name == null || !name.startsWith("LOGIN ")) {
out.println("LOGIN ERROR");
} else {
synchronized (names) {
if (!names.contains(name.substring(6))) {
name = name.substring(6);
names.add(name);
out.println("OK");
writers.add(out);
readers.add(in);
Thread.currentThread().setName(name);
break;
} else {
out.println("NAME ERROR");
}
}
}
}

clientsConnected++;
if (clientsConnected == 5) {
GameLogic.start();
}

/* Accept messages from this client and broadcast them.
Ignore other clients that cannot be broadcasted to. */

while (true) {
String input = in.readLine();
synchronized (playersInput) {
synchronized (GameLogic) {
playersInput.add(input);
GameLogic.notify();
}
}
}

} catch (IOException e) {
System.out.println(e);
} finally {
/* This client is going down! Remove its name and its print
writer from the sets, and close its socket. */
if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}

最佳答案

我假设您正在为每个客户端创建一个单独的线程,因为当轮不到他时,客户端可能正在与计算机进行其他对话?或许他是在问它轮到谁了,或者是在和其他玩家聊天什么的?我绝对不明白你为什么要为 sendMessage 和 readMessage 创建单独的线程。通常,系统将从客户端读取,然后在同一个线程中做出响应。这就是它的循环:读取消息、制定响应、发送消息、重复。

假设我的上述理解是正确的,您仍然需要一个中央代理,即这些 ConsoleClient 所属的 LoveLetterGame,其中只有一个用于任何 Activity 的 LoveLetterGame。 (LoveLetterGame 应该是创建 ConsoleClient 实例的那个。这应该包括一个 AtomicInteger 及其轮到的次数。AtomicInteger 包括用于测试和更改值的线程安全方法,另外你应该有一个规则,即只有线程用于当前玩家可以更改该值。

附言我是情书游戏的忠实粉丝。
附言使用 CamelCase for Java,而不是 Underscore_separation。

关于java - Tourbased 游戏 - Threading java - (轮到你 sleep 了?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59924519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com