提问人:Ezaz Ezaaz 提问时间:3/15/2021 最后编辑:Ezaz Ezaaz 更新时间:3/15/2021 访问量:1388
Java Server 和 Client 类上的 Rock Paper Scissor 游戏
Rock Paper Scissor game on a Java Server and Client class
问:
您好,我是 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();
}
}
}
答:
0赞
sigma1510
3/15/2021
#1
您的问题出在您的服务器类中:
System.out.println("Player chose " + in.readLine());
...
inputPlayer_1 = in.readLine();
您不能从两次读取相同的值。一旦服务器读取了要打印的值,就会返回等待来自客户端的更多响应,当然这些响应永远不会到达,因为客户端已经完成了发送。同时,客户端正在等待服务器,因此没有人执行任何操作,从而造成死锁。
因此,您需要保存从本地读取的值,并使用该值:in
in
in
String valueFromServer = in.readLine();
System.out.println("Rules: " + valueFromServer);
....
// Catch responses
inputPlayer_1 = valueFromServer;
评论