Java Server 和 Client 类上的 Rock Paper Scissor 游戏

Rock Paper Scissor game on a Java Server and Client class

提问人:Ezaz Ezaaz 提问时间:3/15/2021 最后编辑:Ezaz Ezaaz 更新时间:3/15/2021 访问量:1388

问:

您好,我是 Java 的新手,我正在尝试制作一个石头、纸、剪刀布游戏,玩家可以在其中玩计算机。我有两个类 Server1 和 Client1,我正在尝试创建一个石头剪刀布游戏。播放器给出输入,计算机使用 java.util.Random 生成一个随机输入,然后将该整数转换为字符串并传递给字符串变量 inputPlayer_2,然后将其与存储在字符串变量 inputPlayer_1 中的播放器的输入进行比较,并根据逻辑确定获胜者。我似乎无法弄清楚为什么游戏不会显示结果。


import java.io.BufferedReader;
import java.util.Random;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server1 
{
    private static ServerSocket server = null;
    private static Socket socket = null;
    private static final int port = 8081;
    public static void main(String[] args) 
    {
        String varPlayer_1 = "";
        String varPlayer_2 = "";
        String inputPlayer_1;
        String inputPlayer_2;
        
        //Generating random values
                Random rand = new Random();
                int upperbound = 3;
                int random = rand.nextInt(upperbound);
        
        //Create IO Objects
        BufferedReader in = null;
        PrintWriter out = null;
        Scanner consoleInput = new Scanner(System.in);
        
        //Start Server
        try 
        {
            System.out.println("Server is starting ...");
            server = new ServerSocket(port);
            System.out.println("Server has started");
        } catch (IOException e) {
            System.out.println("Can not listen to port: " + port);
            System.exit(-1);
        }
        while(true) {
            
            //Create Socket
            try 
            {
                socket = server.accept();
            } 
            catch (IOException e) 
            {
                System.out.println("Communication Error with network");
                System.exit(-1);
            }
            try 
            {
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(socket.getOutputStream(),true);
                //start1
                out.println("Rock = 0, Paper = 1, scissor = 2");
                
                System.out.println("Player chose " + in.readLine());
                System.out.println("Computer chose: " + random);
                
                inputPlayer_2=String.valueOf(random);
                inputPlayer_1 = in.readLine();
                
                
                if (inputPlayer_1.equals(inputPlayer_2)) {
                    out.println("It's a draw.");
                }
                
                else if (inputPlayer_1.equals("0") && inputPlayer_2.equals("2")) {
                    out.println("Player wins.");
                }
                
                else if (inputPlayer_1.equals("2") && inputPlayer_2.equals("0")) {
                    out.println("Computer wins.");
                }
                
                else if (inputPlayer_1.equals("0") && inputPlayer_2.equals("1")) {
                    out.println("Computer wins.");
                }
                
                else if (inputPlayer_1.equals("1") && inputPlayer_2.equals("0")) {
                    out.println("Player wins.");
                }
                
                else if (inputPlayer_1.equals("2") && inputPlayer_2.equals("1")) {
                    out.println("Player wins.");
                }
                
                else if (inputPlayer_1.equals("1") && inputPlayer_2.equals("2")) {
                    out.println("Computer wins.");
                }
                
            }
            catch (IOException e) 
            {
                System.out.print("Player Left");
                consoleInput.close();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class Client1 {
    public static Socket socket = null;
    public static void main(String[] args) 
    {
    
        String response;
        try 
        {
            socket = new Socket("localhost", 8081);
            System.out.println("Connected to Server\n" + "Socket: " + socket.getInetAddress() + ":" + socket.getPort() + "\n" );
        } 
        catch (IOException e) 
            {
                System.out.print("Connection to network can not be established");
            }
        
        BufferedReader in = null;
        PrintWriter out = null;
        Scanner consoleInput = new Scanner(System.in);
        try 
        {
            in = new BufferedReader( new InputStreamReader(socket.getInputStream() ));
            out = new PrintWriter(socket.getOutputStream(), true);
            //start0
            System.out.println("Rules: " + in.readLine());
            System.out.print("Choose any: ");
            out.println(consoleInput.nextLine());
            
            // Catch responses
            response = in.readLine();

            // Display responses
            System.out.println("Winner " + response);
            
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



Java 套接字 java.util.scanner bufferedreader

评论

0赞 sigma1510 3/15/2021
你也有一个主类,对吧?
0赞 Ezaz Ezaaz 3/15/2021
不,我没有。根据教程,我遵循了我不必这样做。它在控制台上运行并接受输入,但不会显示结果。
0赞 sigma1510 3/15/2021
你是如何运行这个程序的?你能和我们分享一下教程吗?你期望什么类型的输出你没有得到,打印输出?
0赞 Ezaz Ezaaz 3/15/2021
我创建了两个包,客户端和服务器。然后继续为每个命名的 client1 和 server1 创建类。写完代码后,我转到:“RUN AS JAVA APPLICATION”,因此运行了该项目。服务器和客户端一开始都会相互通信,但在运行逻辑后,我期待谁赢得了比赛的输出,但它未能显示出来。
1赞 Ezaz Ezaaz 3/15/2021
服务器正在启动... |服务器已启动 |连接到服务器 |套接字:localhost/127.0.0.1:8081 |规则:石头 = 0,纸 = 1,剪刀 = 2 |任选一项: 1 |玩家选择 1 |计算机选择: 1 |在此之后,程序不再接受任何输入,它只是继续运行。

答:

0赞 sigma1510 3/15/2021 #1

您的问题出在您的服务器类中:

System.out.println("Player chose " + in.readLine());
...

inputPlayer_1 = in.readLine();

您不能从两次读取相同的值。一旦服务器读取了要打印的值,就会返回等待来自客户端的更多响应,当然这些响应永远不会到达,因为客户端已经完成了发送。同时,客户端正在等待服务器,因此没有人执行任何操作,从而造成死锁。 因此,您需要保存从本地读取的值,并使用该值:ininin

String valueFromServer = in.readLine();
System.out.println("Rules: " + valueFromServer);
....
// Catch responses
inputPlayer_1 = valueFromServer;