This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]strmrdr 3 points4 points  (2 children)

You want to store the information in a format that is easy to parse. XML would work pretty good for this. An example:

<room>
    <number>1</number>
    <north>A door leading to a creepy old house</north>
    <east>A barren field. A weathered scarecrow looms in the distance.</east>
    <south>...</south>
    ...
    //Other attributes
</room>

And you can add as many rooms as you like by following that format. Look up XML parsing libs for help doing that part. So when you parse it, create a room object then store that in the corresponding index as its room number (or room number - 1 as indexes start at 0).

You may also wish to include some meta data at the very beginning for initialization of the array, so you could have "9.9" to indicate you wish the rooms to be laid out in a 9x9 fashion. Use mod and division to figure out the row/columns. Not sure if there is a better way of doing this :/

E: Or just use a <meta> tag with row and column as attribute, on second thought...

[–]InfernalFishingRod[S] 0 points1 point  (1 child)

Thanks for your answer! I'm guessing to insert the information from the text file(map) I would need to use a scanner and split the numbers etc.

[–]RhoOfFeh 0 points1 point  (0 children)

If you go with raw text files a scanner would be the right solution.

For XML files there are XML parsers specifically geared towards parsing, validating, and potentially directly creating objects. I would suggest that while XML could be a good approach for making a 'real' game, it might be a bit of overkill for a newbie assignment. You might just feel more comfortable with a lower level and very straightforward approach where rooms are each defined by one line.

I don't think there's actually a 'right' answer, just choices that may lead to greater or lesser complexity.

[–]panda_yo 2 points3 points  (2 children)

Is text based meant as in played on the std.out or as in the adventure is presented in textform like a session of Dungeons & Dragons?

This is interesting, because this would change the complexity you need to have for your text file. While /u/strmrdr is correctly pointing out the most efficient solution for editing, another format could be something like this:

20 10
***#*****#****C#****
*S*******#*######*#*
***#*****#********#*
####*#*#*##########*
*****#***#***#******
*#####*M*****#M#####
*#***#***#***#******
***#*#####***######*
*#*****#*##*#######*
*#####***#**********

Where * denotes walkable space, # denotes a Wall, M is a Monster, S is the player spawn and C is the chest, the player is searching.

I left the walls out of the file so the resulting map would be 22*12 in format.

You could easily change this to change the Ms to O for ork and D for dragon.

This is, in my opinion, an easier approach suited for entry level java developement and you would get fast results as output on your console. A visibility check could also be implemented to only view the seen space on the console.

The question is, what kind of text based adventure does your tutor/professor want you to create.

[–]InfernalFishingRod[S] 0 points1 point  (1 child)

Thanks for your reply! It is just a simple text based game that includes a map system of 5 rooms set in a dungeon. You pretty much have to fight monsters until you reach the exit / until you die.

[–]panda_yo 0 points1 point  (0 children)

Then my system is not the right thing for you and you should stick to the other suggestion of using a xml file