客户端与服务器玩猜数字游戏java
时间: 2023-12-13 16:05:02 浏览: 89
好的,这里给你提供一个简单的客户端-服务器猜数字游戏的Java实现。首先,我们需要定义一些常量和变量来表示游戏的状态和数据:
```java
public class GuessingGame {
private static final int MIN_VALUE = 1;
private static final int MAX_VALUE = 100;
private static final int MAX_TRIES = 5;
private int secretNumber;
private int triesLeft;
public GuessingGame() {
secretNumber = (int) (Math.random() * (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
triesLeft = MAX_TRIES;
}
public String guess(int number) {
triesLeft--;
if (number == secretNumber) {
return "Congratulations! You guessed the secret number!";
} else if (number < secretNumber) {
return "The secret number is higher than your guess. Tries left: " + triesLeft;
} else {
return "The secret number is lower than your guess. Tries left: " + triesLeft;
}
}
public boolean isGameOver() {
return triesLeft == 0;
}
}
```
这个类包含了一个猜数字游戏的逻辑,它可以返回玩家每次猜测的结果,以及判断游戏是否结束。现在,我们需要实现客户端和服务器的代码。
客户端:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class GuessingGameClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5000);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String message;
do {
message = input.readLine();
System.out.println(message);
if (!message.startsWith("Congratulations")) {
String guess = keyboard.readLine();
output.println(guess);
}
} while (!message.startsWith("Congratulations") && !message.startsWith("Sorry"));
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个客户端代码首先建立了一个连接到服务器的Socket,然后创建了一个输入流和一个输出流。它不断地读取服务器发送的消息,并且如果消息不是“Congratulations”或“Sorry”,就从命令行读取玩家的猜测,并且发送给服务器。
服务器:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class GuessingGameServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(5000);
System.out.println("Waiting for clients...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter output = new PrintWriter(clientSocket.getOutputStream(), true);
GuessingGame game = new GuessingGame();
output.println("Welcome to the guessing game! I'm thinking of a number between " + GuessingGame.MIN_VALUE + " and " + GuessingGame.MAX_VALUE + ".");
String guess;
do {
guess = input.readLine();
int number = Integer.parseInt(guess);
String result = game.guess(number);
output.println(result);
} while (!result.startsWith("Congratulations") && !result.startsWith("Sorry") && !game.isGameOver());
if (game.isGameOver()) {
output.println("Sorry! You ran out of tries. The secret number was " + game.secretNumber + ".");
}
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这个服务器代码首先等待客户端的连接,然后创建了一个输入流和一个输出流。它创建了一个GuessingGame实例,并且发送一个欢迎消息到客户端。然后它不断地读取客户端发送的猜测,调用GuessingGame的guess方法来获得猜测的结果,并且发送给客户端。如果游戏结束,则发送一个结束消息到客户端。
现在,你可以运行GuessingGameServer来启动服务器,然后运行多个GuessingGameClient来连接到服务器并且玩游戏了。
阅读全文