英文:
I need to split this data into its own sets so that i may call on them when need
问题
在Java代码中,我所要做的主要工作是将这个文件读入内部数据结构中。
我只是不知道如何将每个房间分开,并在需要时保持其数据在一起,同时仍然能够单独地按照需要调用id或description等内容,但仍然将其保持在房间内,因此卧室保留所有信息,但在需要时我可以单独地调用id或description。
(这是我的数据,它们是带有ID、名称、关于它的摘要和出口通道的房间)
房间:
ID=1,名称=卧室
描述:
你站在一个黑暗的房间中央。这是你的房间,但感觉有些不对劲,一切都消失了,只剩下一个黑暗的房间。
你看到门下面有一盏红灯在闪烁,似乎是一条出口的路。从这里你可以向北走出门。
出口
北 2;
ID=2,名称=走廊
描述:
你走进走廊,仍然注意到一盏红灯,但它仍然在另一端闪烁。你四处看看,注意到墙上有一些撕裂的地方,
充满了黑色虚空般的斑点。慢慢地吸收了更多的房间。其他地方都是黑色和灰色,就像你的房间一样,只是更多的物体消失了。
你知道红灯在走廊的另一端,通往客厅的入口。
出口
北 3,南 1;
ID=3,名称=客厅
描述:
你走进客厅,它仍然保留了一些颜色,但你注意到颜色正在慢慢消失,在房间中间的桌子上,被两个灰色的沙发包围着。
你朝东边看去,听到餐厅传来的尖叫声,
但同时也注意到西边有动静,通往地下室。
出口
东 4,西 5,南 2;
ID=4,名称=餐厅
描述:
你进入了餐厅,注意到房间里的阴影在移动,你感到一股冷风。
你继续听到尖叫声,但不是来自这个房间,而是来自通往前院的东边。随着你靠近,尖叫声越来越响。
出口
西 3,东 6;
ID=5,名称=地下室
描述:
你进入了地下室,里面空空荡荡,寒冷异常。你注意到你的口中有烟雾,同时地下室的每个角落也有三个不同的地方
都冒着烟雾,就像你一样。除了你从东边来的地方,没有其他出口。
出口
东 3;
ID=6,名称=前院
描述:
你进入了前院,注意到草坪上有阴影在移动,但前方没有人,只有你、花园和你的思绪,车道上没有汽车。
你注意到你是孤独的,但可能会听到和看到异常的事物。房子漂浮在无尽的虚空中,没有任何地方可去。
你唯一的出路是回到屋子里,向西走。
出口
西 4;
英文:
in java code
The main this I have to do is read this file into an internal data structure
I just don't know how to split each room up and keep its data together while being able to call on either the id or description etc. by its self when need but still keeping it as all in the room, so bedroom keeps all its information but when need I can call id or description separably
(this is my data they are rooms with a ID , name , summary about it and its exit ways)
Rooms:
ID=1, name = Bed Room
Description :
You are standing in the middle of a dark room. It's your room, but something feels off everything is gone just a plan dark room.
You see a red light sighing under the door it seems like a way to exit.
You can go North from here out the Door.
exits
North 2;
ID=2, name = Hallway
Description :
You enter the Hallway still noticing a red light, but it's still coming way down at the other end. You look around and notice patches of torn spots on the wall
full of black voids looking spots. Slowly sucking in more of the room. Everything else is black, and grey, just like your room was with more objects missing.
You know the red light is at the other end of the hallway with a entrance to the living room
exits
North 3, South 1;
ID=3, name = Living-room
Description :
You enter the living room its still has some color left, but you notice its slowly fading away on the table in the middle of the room surrounded by to gray couches.
you look over to your East and hear a Scream coming from the Dinning-room
but also notice Movement through the West Leading to the Basement
exits
East 4, West 5, South 2;
ID=4, name = Dinning-room
Description :
You have entered the Dinning-room and Notice shadows moving around the room you feel a cold and lifeless wind.
You are continuing to hear Screams but not from this room its coming from the East that leads to the Front-yard The screams get louder for ever step you get closer
exits
West 3, East 6;
ID=5, name = Basement
Description :
You have enter the Basement its empty and cold never felt temperatures this low. you notice smoke coming out of you mouth but also three different spots in
each corner of the basement also forming smoke just as you are.
There is not Exit other then East where you came from.
exits
East 3;
ID=6, name = Front-yard
Description :
You entered the Front-yard you notices Shadows moving along the lawn back in the fort but no ones there just you and you garden and your thoughts the driveway has no Cars
You notice you're all alone, but it can be possible you hear and see unexceptionable things. The house is floating in a endless void there is no where to go.
your only way is back into the house West
exits
West 4;
答案1
得分: 1
看着你的数据,为了确定适当的实现方式,我觉得 出口 方向应该是一个 enum
。
public enum Direction {
EAST, NORTH, SOUTH, WEST;
}
而一个 出口 是方向加上一个数字的组合,即给定方向上有多少个房间出口。所以我创建了一个 RoomExit
类。
public class RoomExit {
private Direction direction;
private int count;
public RoomExit(Direction direction, int count) {
if (count < 0) {
throw new IllegalArgumentException("negative exits");
}
this.direction = direction;
this.count = count;
}
public Direction getDirection() {
return direction;
}
public int getCount() {
return count;
}
}
最后,你的示例数据描述了一系列房间,每个房间都有一个 ID、一个名称、一个描述和一些出口。所以我创建了一个 RoomData
类。
import java.util.ArrayList;
import java.util.List;
public class RoomData {
private int id;
private String name;
private String description;
private List<RoomExit> exits;
public RoomData(int id, String name) {
this.id = id;
this.name = name;
exits = new ArrayList<>();
}
public boolean addExit(RoomExit exit) {
return exits.add(exit);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<RoomExit> getExits() {
return exits;
}
public String toString() {
return String.format("%d %s [%s] %d exits", id, name, description, exits.size());
}
}
然后,我创建了一个 "driver" 类,它读取包含房间数据的文本文件(我将该文件命名为 roomdata.txt
),并创建一个房间列表。
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class RoomTest {
// ...(以下内容省略)
}
以下是运行上述 "driver" 程序时的输出。
There are 6 rooms.
1 Bed Room [You are standing in the middle of a dark room. It's your room, but something feels off everything is gone just a plan dark room.
You see a red light sighing under the door it seems like a way to exit.
You can go North from here out the Door.
] 1 exits
2 Hallway [You enter the Hallway still noticing a red light, but it's still coming way down at the other end. You look around and notice patches of torn spots on the wall
full of black voids looking spots. Slowly sucking in more of the room. Everything else is black, and grey, just like your room was with more objects missing.
You know the red light is at the other end of the hallway with an entrance to the living room
] 2 exits
3 Living-room [You enter the living room, it still has some color left, but you notice it's slowly fading away on the table in the middle of the room surrounded by two gray couches.
You look over to your East and hear a Scream coming from the Dining-room
but also notice Movement through the West Leading to the Basement
] 3 exits
4 Dining-room [You have entered the Dining-room and Notice shadows moving around the room you feel a cold and lifeless wind.
You are continuing to hear Screams but not from this room, it's coming from the East that leads to the Front-yard. The screams get louder for every step you get closer
] 2 exits
5 Basement [You have entered the Basement, it's empty and cold, never felt temperatures this low. You notice smoke coming out of your mouth but also three different spots in
each corner of the basement, also forming smoke just like you are.
There is no Exit other than East where you came from.
] 1 exits
6 Front-yard [You entered the Front-yard, you notice shadows moving along the lawn back in the fort but no one's there, just you and your garden and your thoughts. The driveway has no cars.
You notice you're all alone, but it can be possible you hear and see unexplained things. The house is floating in an endless void, there is nowhere to go.
Your only way is back into the house, West.
] 1 exits
英文:
Looking at your data, in order to determine an appropriate implementation, I feel that the the exit directions should be an enum
.
public enum Direction {
EAST, NORTH, SOUTH, WEST;
}
And an exit is a combination of a direction plus a number, i.e. how many room exits are there in the given direction. So I created a RoomExit
class.
public class RoomExit {
private Direction direction;
private int count;
public RoomExit(Direction direction, int count) {
if (count < 0) {
throw new IllegalArgumentException("negative exits");
}
this.direction = direction;
this.count = count;
}
public Direction getDirection() {
return direction;
}
public int getCount() {
return count;
}
}
Finally, your sample data describes a series of rooms where each room has an ID, a name, a description and a number of exits. So I created a RoomData
class.
import java.util.ArrayList;
import java.util.List;
public class RoomData {
private int id;
private String name;
private String description;
private List<RoomExit> exits;
public RoomData(int id, String name) {
this.id = id;
this.name = name;
exits = new ArrayList<>();
}
public boolean addExit(RoomExit exit) {
return exits.add(exit);
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public List<RoomExit> getExits() {
return exits;
}
public String toString() {
return String.format("%d %s [%s] %d exits", id, name, description, exits.size());
}
}
Then I created a "driver" class that reads the text file containing the rooms data (I named this file roomdata.txt
) and creates a list of rooms.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class RoomTest {
private static final String COMMA = ",";
private static final String DESCRIPTION = "Description :";
private static final String EQUALS = "=";
private static final String EXITS = "exits";
private static final String ID = "ID";
private static final String SEMI_COLON = ";";
private static int getRoomId(String line) {
Objects.requireNonNull(line, "null line");
String[] firstSplit = line.split(COMMA);
if (firstSplit.length != 2) {
throw new IllegalArgumentException("Unexpected line: " + line);
}
String[] secondSplit = firstSplit[0].split(EQUALS);
if (secondSplit.length != 2) {
throw new IllegalArgumentException("Unexpected line: " + line);
}
int roomId = Integer.parseInt(secondSplit[1].trim());
return roomId;
}
private static String getRoomName(String line) {
Objects.requireNonNull(line, "null line");
String[] firstSplit = line.split(COMMA);
if (firstSplit.length != 2) {
throw new IllegalArgumentException("Unexpected line: " + line);
}
String[] secondSplit = firstSplit[1].split(EQUALS);
if (secondSplit.length != 2) {
throw new IllegalArgumentException("Unexpected line: " + line);
}
return secondSplit[1].trim();
}
private static void setRoomExits(RoomData room, String line) {
Objects.requireNonNull(line, "null line");
line = line.trim();
if (line.endsWith(SEMI_COLON)) {
line = line.substring(0, line.length() - 1);
}
String[] exits = line.split(COMMA);
for (String exit : exits) {
exit = exit.trim();
String[] parts = exit.split(" ");
if (parts.length != 2) {
throw new IllegalArgumentException("Unexpected exit: " + exit);
}
RoomExit roomExit = new RoomExit(Direction.valueOf(parts[0].trim().toUpperCase()),
Integer.parseInt(parts[1].trim()));
room.addExit(roomExit);
}
}
public static void main(String[] args) {
try (FileReader fr = new FileReader("roomdata.txt");
BufferedReader br = new BufferedReader(fr)) {
boolean isExits = false;
List<RoomData> rooms = new ArrayList<>();
RoomData room = null;
String line = br.readLine();
StringBuilder description = null;
while (line != null) {
if (line.startsWith(ID)) {
if (room != null) {
isExits = false;
room.setDescription(description.toString());
rooms.add(room);
description = null;
}
room = new RoomData(getRoomId(line), getRoomName(line));
}
else if (DESCRIPTION.equals(line)) {
description = new StringBuilder();
}
else if (EXITS.equals(line)) {
isExits = true;
}
else {
if (isExits) {
setRoomExits(room, line);
}
else {
description.append(line);
description.append(System.lineSeparator());
}
}
line = br.readLine();
}
if (room != null) {
room.setDescription(description.toString());
rooms.add(room);
}
System.out.printf("There are %d rooms.%n", rooms.size());
rooms.forEach(System.out::println);
}
catch (IOException xIo) {
xIo.printStackTrace();
}
}
}
Here is the output when running the above "driver" program.
There are 6 rooms.
1 Bed Room [You are standing in the middle of a dark room. It's your room, but something feels off everything is gone just a plan dark room.
You see a red light sighing under the door it seems like a way to exit.
You can go North from here out the Door.
] 1 exits
2 Hallway [You enter the Hallway still noticing a red light, but it's still coming way down at the other end. You look around and notice patches of torn spots on the wall
full of black voids looking spots. Slowly sucking in more of the room. Everything else is black, and grey, just like your room was with more objects missing.
You know the red light is at the other end of the hallway with a entrance to the living room
] 2 exits
3 Living-room [You enter the living room its still has some color left, but you notice its slowly fading away on the table in the middle of the room surrounded by to gray couches.
you look over to your East and hear a Scream coming from the Dinning-room
but also notice Movement through the West Leading to the Basement
] 3 exits
4 Dinning-room [You have entered the Dinning-room and Notice shadows moving around the room you feel a cold and lifeless wind.
You are continuing to hear Screams but not from this room its coming from the East that leads to the Front-yard The screams get louder for ever step you get closer
] 2 exits
5 Basement [You have enter the Basement its empty and cold never felt temperatures this low. you notice smoke coming out of you mouth but also three different spots in
each corner of the basement also forming smoke just as you are.
There is not Exit other then East where you came from.
] 1 exits
6 Front-yard [You entered the Front-yard you notices Shadows moving along the lawn back in the fort but no ones there just you and you garden and your thoughts the driveway has no Cars
You notice you're all alone, but it can be possible you hear and see unexceptionable things. The house is floating in a endless void there is no where to go.
your only way is back into the house West
] 1 exits
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论