用Java实现一个简易的MUD游戏开发,并给出具体代码,要求如下:包括服务器端和客户端两部分内容 1.同时支持多人在线游戏 2.创建一个虚拟的世界 3.玩家可以在虚拟世界中移动方向(w,e,s,n,ne,se,nw,sw,u,d); 4.玩家的动作(包括连线、移动)对其他相关玩家(同一房间)可见 5.玩家的基本动作包括,查看(look,l),退出(quit) 6.玩家账户信息存储在数据库或文件中(第一次登陆创建账户) 7.客户端能够建立与服务器的链接、向服务器发送消息,接受并显示信息
时间: 2024-04-04 10:32:00 浏览: 112
这是一个比较复杂的项目,需要考虑很多细节,下面是一个简单的 Java MUD 游戏服务器和客户端的实现示例:
服务器端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class MUDServer {
private List<MUDPlayer> players = new ArrayList<>();
private MUDWorld world = new MUDWorld();
public static void main(String[] args) {
MUDServer server = new MUDServer();
server.start();
}
public void start() {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("MUD server started at port 8888.");
while (true) {
Socket socket = serverSocket.accept();
MUDPlayer player = new MUDPlayer(socket, world, players);
players.add(player);
player.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MUDClient {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public static void main(String[] args) {
MUDClient client = new MUDClient();
client.connect();
}
public void connect() {
try {
socket = new Socket("localhost", 8888);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
String serverResponse = in.readLine();
System.out.println(serverResponse);
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
MUDPlayer 类(处理玩家的输入输出和逻辑):
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;
public class MUDPlayer extends Thread {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private MUDWorld world;
private List<MUDPlayer> players;
private String name;
private MUDRoom currentRoom;
public MUDPlayer(Socket socket, MUDWorld world, List<MUDPlayer> players) {
this.socket = socket;
this.world = world;
this.players = players;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println("Welcome to the MUD game! Please enter your name:");
name = in.readLine();
out.println("Welcome, " + name + "!");
if (!world.hasPlayer(name)) {
currentRoom = world.getStartingRoom();
world.addPlayer(name, currentRoom);
} else {
currentRoom = world.getPlayerRoom(name);
out.println("You are now in " + currentRoom.getName() + ".");
broadcast(name + " has joined the game.");
}
String inputLine;
while ((inputLine = in.readLine()) != null) {
String[] tokens = inputLine.split(" ");
if (tokens.length == 0) {
continue;
}
String command = tokens[0];
if ("quit".equalsIgnoreCase(command)) {
break;
} else if ("look".equalsIgnoreCase(command) || "l".equalsIgnoreCase(command)) {
out.println(currentRoom.getDescription());
out.println(currentRoom.getExits());
out.println(currentRoom.getPlayers());
} else if ("say".equalsIgnoreCase(command)) {
broadcast(name + " says: " + inputLine.substring(4));
} else if ("move".equalsIgnoreCase(command)) {
String direction = tokens[1];
MUDRoom nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
out.println("You cannot go that way.");
} else {
currentRoom.removePlayer(name);
nextRoom.addPlayer(name);
currentRoom = nextRoom;
out.println(currentRoom.getDescription());
out.println(currentRoom.getExits());
out.println(currentRoom.getPlayers());
broadcast(name + " has left the room.");
broadcast(name + " has entered the room.");
}
} else {
out.println("Invalid command.");
}
}
out.println("Bye, " + name + "!");
currentRoom.removePlayer(name);
players.remove(this);
broadcast(name + " has left the game.");
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void broadcast(String message) {
for (MUDPlayer player : players) {
if (!player.getName().equals(name) && player.getCurrentRoom() == currentRoom) {
player.sendMessage(message);
}
}
}
public void sendMessage(String message) {
out.println(message);
}
public String getName() {
return name;
}
public MUDRoom getCurrentRoom() {
return currentRoom;
}
}
```
MUDWorld 类(包含虚拟世界的信息和逻辑):
```java
import java.util.HashMap;
import java.util.Map;
public class MUDWorld {
private Map<String, MUDRoom> rooms = new HashMap<>();
private Map<String, MUDPlayer> players = new HashMap<>();
public MUDWorld() {
MUDRoom hall = new MUDRoom("Hall", "You are in the hall.");
MUDRoom kitchen = new MUDRoom("Kitchen", "You are in the kitchen.");
MUDRoom bedroom = new MUDRoom("Bedroom", "You are in the bedroom.");
MUDRoom bathroom = new MUDRoom("Bathroom", "You are in the bathroom.");
hall.addExit("east", kitchen);
hall.addExit("up", bedroom);
kitchen.addExit("west", hall);
bedroom.addExit("down", hall);
bedroom.addExit("east", bathroom);
bathroom.addExit("west", bedroom);
rooms.put("hall", hall);
rooms.put("kitchen", kitchen);
rooms.put("bedroom", bedroom);
rooms.put("bathroom", bathroom);
}
public MUDRoom getStartingRoom() {
return rooms.get("hall");
}
public void addPlayer(String name, MUDRoom room) {
players.put(name, new MUDPlayer(name, room));
room.addPlayer(name);
}
public boolean hasPlayer(String name) {
return players.containsKey(name);
}
public MUDRoom getPlayerRoom(String name) {
return players.get(name).getCurrentRoom();
}
public void removePlayer(String name) {
MUDPlayer player = players.remove(name);
player.getCurrentRoom().removePlayer(name);
}
}
```
MUDRoom 类(代表一个房间):
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MUDRoom {
private String name;
private String description;
private Map<String, MUDRoom> exits = new HashMap<>();
private List<String> players = new ArrayList<>();
public MUDRoom(String name, String description) {
this.name = name;
this.description = description;
}
public void addExit(String direction, MUDRoom room) {
exits.put(direction, room);
}
public MUDRoom getExit(String direction) {
return exits.get(direction);
}
public void addPlayer(String name) {
players.add(name);
}
public void removePlayer(String name) {
players.remove(name);
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public String getExits() {
StringBuilder sb = new StringBuilder();
sb.append("Exits: ");
for (String direction : exits.keySet()) {
sb.append(direction).append(" ");
}
return sb.toString();
}
public String getPlayers() {
StringBuilder sb = new StringBuilder();
sb.append("Players: ");
for (String player : players) {
sb.append(player).append(" ");
}
return sb.toString();
}
}
```
注意:这只是一个简单的示例,还有很多细节需要考虑和实现,比如:玩家账户信息的存储、安全性、游戏内容的丰富性等。
阅读全文