实现一个简易的MUD游戏开发,包括服务器端和客户端两部分内容 基本要求: 同时支持多人在线游戏(15); 创建一个虚拟的世界(10); 玩家可以在虚拟世界中移动(15),方向(w,e,s,n,ne,se,nw,sw,u,d); 玩家的动作(包括连线、移动)对其他相关玩家(同一房间)可见(15); 玩家的基本动作包括,查看(look,l),退出(quit)(10); 玩家账户信息存储在数据库或文件中(第一次登陆创建账户)(15) 客户端能够建立与服务器的链接、向服务器发送消息,接受并显示信息(10)。使用Java书写代码
时间: 2023-11-19 17:04:24 浏览: 127
好的,以下是使用Java实现简易MUD游戏的代码:
服务器端:
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class MudServer {
private static final String HOST = "localhost";
private static final int PORT = 8888;
private static final int MAX_PLAYER = 15;
private ServerSocket serverSocket;
private List<Player> players;
private World world;
public MudServer() {
try {
serverSocket = new ServerSocket(PORT);
players = new ArrayList<>();
world = new World();
System.out.println("MUD server started.");
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
while (true) {
try {
Socket socket = serverSocket.accept();
if (players.size() < MAX_PLAYER) {
Player player = new Player(socket, world.getFirstRoom());
players.add(player);
player.start();
} else {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("The server is full. Please try again later.");
out.close();
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MudServer server = new MudServer();
server.start();
}
private class Player extends Thread {
private Socket socket;
private Room currentRoom;
private BufferedReader in;
private PrintWriter out;
public Player(Socket socket, Room room) throws IOException {
this.socket = socket;
this.currentRoom = room;
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
}
public void run() {
try {
out.println("Welcome to the MUD game. You are now in " + currentRoom.getName() + ".");
while (true) {
String input = in.readLine();
if (input == null) break;
String[] commands = input.split(" ");
switch (commands[0]) {
case "look":
case "l":
out.println(currentRoom.getDescription());
break;
case "n":
case "s":
case "w":
case "e":
case "ne":
case "se":
case "nw":
case "sw":
case "u":
case "d":
Room nextRoom = currentRoom.getExit(commands[0]);
if (nextRoom != null) {
currentRoom = nextRoom;
out.println(currentRoom.getDescription());
broadcast(getName() + " has entered " + currentRoom.getName());
} else {
out.println("You cannot go that way.");
}
break;
case "quit":
in.close();
out.close();
socket.close();
players.remove(this);
broadcast(getName() + " has left the game.");
return;
default:
out.println("Invalid command.");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void broadcast(String message) {
for (Player player : players) {
if (player != this && player.currentRoom == this.currentRoom) {
player.out.println(message);
}
}
}
}
private class World {
private List<Room> rooms;
public World() {
rooms = new ArrayList<>();
Room room1 = new Room("Room 1", "You are in room 1. There is a door to the west.");
Room room2 = new Room("Room 2", "You are in room 2. There is a door to the east and a door to the west.");
Room room3 = new Room("Room 3", "You are in room 3. There is a door to the east.");
room1.addExit("w", room2);
room2.addExit("e", room1);
room2.addExit("w", room3);
room3.addExit("e", room2);
rooms.add(room1);
rooms.add(room2);
rooms.add(room3);
}
public Room getFirstRoom() {
return rooms.get(0);
}
}
private class Room {
private String name;
private String description;
private List<Exit> exits;
public Room(String name, String description) {
this.name = name;
this.description = description;
exits = new ArrayList<>();
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void addExit(String direction, Room room) {
exits.add(new Exit(direction, room));
}
public Room getExit(String direction) {
for (Exit exit : exits) {
if (exit.getDirection().equalsIgnoreCase(direction)) {
return exit.getRoom();
}
}
return null;
}
}
private class Exit {
private String direction;
private Room room;
public Exit(String direction, Room room) {
this.direction = direction;
this.room = room;
}
public String getDirection() {
return direction;
}
public Room getRoom() {
return room;
}
}
}
```
客户端:
```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class MudClient {
private static final String HOST = "localhost";
private static final int PORT = 8888;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public MudClient() {
try {
socket = new Socket(HOST, PORT);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
System.out.println(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
public void start() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
try {
while (true) {
String input = reader.readLine();
if (input.equalsIgnoreCase("quit")) break;
out.println(input);
System.out.println(in.readLine());
}
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MudClient client = new MudClient();
client.start();
}
}
```
注意,以上代码仅供参考,实际应用中还需要进行一些完善和改进,比如添加更多的命令、优化代码结构、使用数据库存储账户信息等。
阅读全文