实现一个简易的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 13:05:30 浏览: 84
java课程设计MUD多人在线游戏简单模拟
5星 · 资源好评率100%
好的,下面是一个简易的MUD游戏的Java代码实现:
服务器端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MudServer {
private List<Connection> connections = new ArrayList<>();
private Map<String, Room> rooms = new HashMap<>();
public static void main(String[] args) {
MudServer server = new MudServer();
server.init();
server.start();
}
private void init() {
// 创建虚拟世界
Room startRoom = new Room("Start Room", "This is the start room.", null);
Room northRoom = new Room("North Room", "This is the north room.", startRoom);
Room southRoom = new Room("South Room", "This is the south room.", startRoom);
Room eastRoom = new Room("East Room", "This is the east room.", startRoom);
Room westRoom = new Room("West Room", "This is the west room.", startRoom);
Room upRoom = new Room("Up Room", "This is the up room.", startRoom);
Room downRoom = new Room("Down Room", "This is the down room.", startRoom);
startRoom.addExit("north", northRoom);
startRoom.addExit("south", southRoom);
startRoom.addExit("east", eastRoom);
startRoom.addExit("west", westRoom);
startRoom.addExit("up", upRoom);
startRoom.addExit("down", downRoom);
rooms.put(startRoom.getName(), startRoom);
rooms.put(northRoom.getName(), northRoom);
rooms.put(southRoom.getName(), southRoom);
rooms.put(eastRoom.getName(), eastRoom);
rooms.put(westRoom.getName(), westRoom);
rooms.put(upRoom.getName(), upRoom);
rooms.put(downRoom.getName(), downRoom);
}
private void start() {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("MUD server started on port 8888.");
while (true) {
Socket socket = serverSocket.accept();
Connection connection = new Connection(socket);
connections.add(connection);
connection.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void broadcast(String message) {
for (Connection connection : connections) {
connection.sendMessage(message);
}
}
private class Connection extends Thread {
private Socket socket;
private String username;
private Room currentRoom;
public Connection(Socket socket) {
this.socket = socket;
}
public void run() {
try {
sendMessage("Welcome to the MUD server!");
sendMessage("Please enter your username:");
String line = receiveMessage();
if (line == null) {
return;
}
username = line.trim();
sendMessage("Hello " + username + "!");
sendMessage(currentRoom.getDescription());
while (true) {
line = receiveMessage();
if (line == null) {
return;
}
String[] parts = line.trim().split("\\s+");
if (parts.length == 0) {
continue;
}
String command = parts[0];
switch (command) {
case "look":
case "l":
sendMessage(currentRoom.getDescription());
break;
case "north":
case "south":
case "east":
case "west":
case "up":
case "down":
Room destination = currentRoom.getExit(parts[0]);
if (destination == null) {
sendMessage("You cannot go that way.");
} else {
currentRoom = destination;
sendMessage(currentRoom.getDescription());
broadcast(username + " has entered the room.");
}
break;
case "quit":
broadcast(username + " has left the game.");
return;
default:
sendMessage("Unknown command: " + command);
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
connections.remove(this);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void sendMessage(String message) throws IOException {
socket.getOutputStream().write((message + "\r\n").getBytes());
socket.getOutputStream().flush();
}
private String receiveMessage() throws IOException {
byte[] buffer = new byte[1024];
int length = socket.getInputStream().read(buffer);
if (length == -1) {
return null;
}
return new String(buffer, 0, length).trim();
}
}
private class Room {
private String name;
private String description;
private Map<String, Room> exits = new HashMap<>();
private List<String> players = new ArrayList<>();
public Room(String name, String description, Room exit) {
this.name = name;
this.description = description;
if (exit != null) {
exits.put("back", exit); // add a "back" exit to the previous room
}
}
public String getName() {
return name;
}
public String getDescription() {
String result = description + "\r\n";
if (!players.isEmpty()) {
result += "Players in this room:\r\n";
for (String player : players) {
result += "- " + player + "\r\n";
}
}
result += "Exits:\r\n";
for (String direction : exits.keySet()) {
result += "- " + direction + ": " + exits.get(direction).getName() + "\r\n";
}
return result;
}
public void addExit(String direction, Room room) {
exits.put(direction, room);
}
public Room getExit(String direction) {
return exits.get(direction);
}
public void addPlayer(String player) {
players.add(player);
}
public void removePlayer(String player) {
players.remove(player);
}
}
}
```
客户端代码:
```java
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.Socket;
public class MudClient extends JFrame implements ActionListener {
private JTextField textField;
private JTextArea textArea;
private Socket socket;
public static void main(String[] args) {
MudClient client = new MudClient();
client.init();
client.connect();
}
private void init() {
// 创建可视化界面
setTitle("MUD Client");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textArea = new JTextArea();
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
panel.setLayout(new BorderLayout(0, 0));
textField = new JTextField();
panel.add(textField, BorderLayout.CENTER);
textField.setColumns(10);
textField.addActionListener(this);
JButton btnSend = new JButton("Send");
panel.add(btnSend, BorderLayout.EAST);
btnSend.addActionListener(this);
}
private void connect() {
try {
socket = new Socket("localhost", 8888);
new Thread(new Receiver()).start();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessage(String message) {
try {
socket.getOutputStream().write((message + "\r\n").getBytes());
socket.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
private class Receiver implements Runnable {
public void run() {
try {
byte[] buffer = new byte[1024];
int length;
while ((length = socket.getInputStream().read(buffer)) != -1) {
String message = new String(buffer, 0, length);
SwingUtilities.invokeLater(new Updater(message));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class Updater implements Runnable {
private String message;
public Updater(String message) {
this.message = message;
}
public void run() {
textArea.append(message);
}
}
public void actionPerformed(ActionEvent e) {
String message = textField.getText().trim();
if (!message.isEmpty()) {
sendMessage(message);
textField.setText("");
}
}
}
```
以上代码是一个简单的MUD游戏的实现,实现了基本要求。但是需要注意的是,这个代码只是一个简单的实现,还需要考虑到很多细节问题,比如安全性、稳定性、性能等方面。
阅读全文