Java 服务器和客户端类上的石头剪刀布游戏
Posted
技术标签:
【中文标题】Java 服务器和客户端类上的石头剪刀布游戏【英文标题】:Rock Paper Scissor game on a Java Server and Client class 【发布时间】:2021-03-14 18:51:56 【问题描述】:您好,我是 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();
【问题讨论】:
你也有一个 Main 类,对吧? 不,我不知道。根据教程,我不必这样做。它在控制台上运行并接受输入,但不会显示结果。 你是如何运行程序的?可以和我们分享一下教程吗?您希望得到什么类型的输出而不是打印输出? 我创建了两个包,客户端和服务器。然后继续为每个命名的 client1 和 server1 创建类。编写代码后,我转到:“RUN AS JAVA APPLICATION”并因此运行该项目。服务器和客户端一开始都相互通信,但在运行逻辑之后,我期待输出谁赢得了比赛,但它没有显示出来。 服务器正在启动... |服务器已启动 |连接到服务器 |套接字:localhost/127.0.0.1:8081 |规则:岩石 = 0,纸 = 1,剪刀 = 2 |选择任何一个: 1 |玩家选择了 1 |电脑选择:1 |在此之后,程序不再接受任何输入,它只是继续运行。 【参考方案1】:您的问题出在您的服务器类中:
System.out.println("Player chose " + in.readLine());
...
inputPlayer_1 = in.readLine();
您不能从in
读取相同的值两次。一旦服务器读取了要打印的值,in
就会返回等待来自客户端的更多响应,这些响应当然永远不会到达,因为客户端已完成发送。同时,客户端正在等待服务器,所以没有人做任何事情,创建一个deadlock。
所以你需要在本地保存从in
读取的值并使用它:
String valueFromServer = in.readLine();
System.out.println("Rules: " + valueFromServer);
....
// Catch responses
inputPlayer_1 = valueFromServer;
【讨论】:
以上是关于Java 服务器和客户端类上的石头剪刀布游戏的主要内容,如果未能解决你的问题,请参考以下文章