【问题标题】:Binary search guessing game server side in javajava中的二进制搜索猜谜游戏服务器端
【发布时间】:2016-04-14 20:53:52
【问题描述】:

我对这个程序与网络感到困惑。我应该创建一个猜测服务器,在 5150 创建一个套接字并等待客户端。我的服务器不应该与任意数量的连续客户端一起玩游戏并连续运行,直到客户端连接并发送“SHUT DOWN”作为其初始消息。它应该实现二进制搜索程序,客户端向它发送两个数字,指示列表的下边界和上边界。我的服务器应该回复一个猜测,然后它会收到来自客户端的消息,说明数字是高、低、赢还是输了游戏。很明显,当游戏结束时,客户端和服务器之间的连接就结束了。 到目前为止,这是我的代码,我正在努力解决甚至出错的问题!任何帮助或示例将不胜感激!

public class GuessingServer {
    public static void main(String[] args){
        String inputLine = "";
        int num1 = 0;
        int num2 = 0;
        int guess = 0;
        String response = "";
        try {
            //set up server
            ServerSocket serverSocket = new ServerSocket(5150);
            //accept the client (socket)
            Socket clientSocket = serverSocket.accept();

            //try to creates scanner and print writer
            Scanner scan = new Scanner(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter print = new PrintWriter(clientSocket.getOutputStream(), true);

            //do game stuff:
            while(scan.nextLine()!= null){
                    if(inputLine.equals("SHUT DOWN")){
                        //shuts down the server
                        serverSocket.close();
                    }   
            //scanner to read the line:
            inputLine = scan.nextLine();
            Scanner line = new Scanner(inputLine);
            if(response != ""){
                while(line.hasNext() == true){ //while the scanner line has something next in it 
                    //get the numbers
                    num1 = line.nextInt();
                    num2 = line.nextInt();
                    //decide and send guess to client:
                    guess = (((num2 - num1)/2)+num1);
                    print.println(guess);
                    System.out.println(guess);
                }
            }else if (response.equals("high") || response.equals("low")){
                while(line.hasNext() == true){ //while the scanner line has something next in it 
                    //get the numbers
                    num1 = line.nextInt();
                    num2 = line.nextInt();
                     //-->decide and send guess to client:
                    guess = (((num2 - num1)/2)+num1);
                    print.println(guess);
                    System.out.println(guess);
                }
            }
            else if(response.equals("won") || response.equals("lost")){
                    //game is over:
                scan.close();
                line.close();
                clientSocket.close();
            } 

            scan.close();
            line.close();
            clientSocket.close();
            serverSocket.close();
        }
    } catch (IOException e) {
    }
}

【问题讨论】:

  • “顺序客户”这是什么意思?如果多个用户可以同时玩,那么通常你需要 Runnable 类,并为每个玩家生成一个线程
  • 老实说,我不知道它到底是什么意思,但我认为我们不需要只为我的实验室编写服务器端的代码并为此程序使用 junit 测试文件
  • 我的回答可能会帮助你。当我上网络编程课程时,我们也做过类似的事情
  • 创建线程的部分我认为我们还没有学会如何做到这一点,还有其他方法还是必须这样?

标签: java sockets search server binary-search


【解决方案1】:

我会这样做。

为您的游戏服务器创建一个实现 Runnable 的类,例如

class GameThread implements Runnable {
    public void run() {
        // Game logic here.
        // Read user input
        // If a user guesses the right number, call shutdown on GuessingGame class
}

以及创建线程的游戏服务器程序,例如

class GuessingGame {
    public static void main(String[] args)
    {
        while(true) {
            // Accept connections
        }
        // create threads
    }

    void shutDown() {
        // Cleanup
        // Terminate threads
        // Close connections
        System.exit(1);
    }
}

这允许多个连接到您的游戏服务器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多