java - Java中的客户端-服务器猜数字游戏(套接字编程)
问题描述
我想用猜数字游戏写一个客户端-服务器通信代码示例。我的Java不是很好。 代码必须这样做:客户端将连接到服务器。服务器会生成一个随机数。客户将尝试猜测它。我可以输入 1 次。然后它冻结。
这是我的 GuessingGame 代码
{
private int goal;
private boolean correct;
public GuessingGame()
{
goal = (int) (Math.random() * 9 + 1);
correct = false;
}
public int getGoal()
{
return goal;
}
public boolean getCorrect()
{
return correct;
}
public String guess(int guess)
{
if(guess == goal)
{
correct = true;
return "Well done, your answer is correct";
}
else if(guess > goal)
{
return "Too high, can you try it with another number?";
}
else return "Too low, you can try to pick up more bigger number please";
}
}
这是我的服务器代码
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerSide
{
public static void main(String[] args) throws IOException
{
GuessingGame game = new GuessingGame();
ServerSocket socket = null;
Socket client = null;
String result;
boolean correct = false;
int attempt;
try
{
socket = new ServerSocket(1234);
}
catch(IOException ioe)
{
System.err.println(ioe);
return;
}
System.out.println("Our server is still running...");
client = socket.accept();
System.out.println("Yes, our Client had connected");
DataInputStream in = new DataInputStream(new BufferedInputStream(client.getInputStream()));
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
while(!correct)
{
attempt = in.readInt();
result = game.guess(attempt);
correct = game.getCorrect();
out.writeUTF(result);
out.writeBoolean(correct);
out.flush();
if(correct == false)
{
client = socket.accept();
attempt = in.readInt();
result = game.guess(attempt);
correct = game.getCorrect();
out.writeUTF(result);
out.writeBoolean(correct);
out.flush();
}
else
{
client.close();
socket.close();
}
}
}
}
这是我的客户代码
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class ClientSide
{
public static void main(String[] args) throws IOException
{
System.out.println("This is Number Guessing Game. \nChoose any number between 1 to 10 : ");
Scanner keyboard = new Scanner(System.in);
int attempt = 0;
try
{
attempt = keyboard.nextInt();
if(attempt < 1 || attempt > 9)
{
System.out.println("Your number is too large/small, please make a guess between 1 to 10");
attempt = keyboard.nextInt();
}
}
catch(NumberFormatException nfe)
{
System.out.println("Just choose numbers! Try again");
attempt = keyboard.nextInt();
}
try
{
Socket server = new Socket("localhost", 1234);
System.out.println("Connecting...");
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(server.getOutputStream()));
DataInputStream in = new DataInputStream(new BufferedInputStream(server.getInputStream()));
out.writeInt(attempt);
out.flush();
System.out.println("Our server is still running...");
String result = in.readUTF();
boolean correct = in.readBoolean();
System.out.println(result);
while (!correct)
{
attempt = keyboard.nextInt();
out.writeInt(attempt);
out.flush();
System.out.println("Our server is still running...");
result = in.readUTF();
System.out.println(result);
correct = in.readBoolean();
}
server.close();
keyboard.close();
System.out.println("Finish. Thank you");
System.exit(0);
}
catch(IOException ioe)
{
System.err.println(ioe);
}
}
}
样本输出
This is Number Guessing Game.
Choose any number between 1 to 10 :
23
Your number is too large/small, please make a guess between 1 to 10
12
Connecting...
Our server is still running...
Too high, can you try it with another number?
15
Our server is still running...
解决方案
推荐阅读
- php - 如何将关联数组与索引数组合并
- clang - Clang 使用 fuzzer 参数显示编译器错误
- node.js - 编辑组件 html 时 Angular CLI 不会重新加载?
- algorithm - 无法计算以下算法的时间复杂度
- python - 最后一行代码是什么意思,x1[ x1[:,1]>3 ]
- c# - 如何在 C# 中正确处理第三方对象(TelegramBotClient)
- excel - 合并重复记录,然后使用 vlookup 从另一张表中提取数据
- ios - ViewControllers 之间的延迟自定义转换
- java - 将用户输入转换为字符串,遇到多个字母的问题
- javascript - 来自javascript的字符串“true”在php中未被识别为该字符串值