Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 444061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:13:58+00:00 2026-05-12T21:13:58+00:00

This may be a duplicate question as I don’t know to phrase the search

  • 0

This may be a duplicate question as I don’t know to phrase the search query. I’m creating a Zork-like text based game in Java where the character moves to different rooms which are connected to each other. I want to be able to list all options a player has available for this room.

For example, Room A is connected east to B, and B is connected west to A, south to C, north to D and so forth.

What data structure should I use or how should I implement this as efficiently as possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:13:59+00:00Added an answer on May 12, 2026 at 9:13 pm

    The first thing to decide is what constitutes a valid direction: is it from a fixed list or can it be freeform text? The simplest solution is to have the four cardinal directions. Some have suggested doing this as an int array. That might be a valid solution in C/C++/C# (enums in all of them are just int constants) but there is no reason to do it in Java.

    In Java you can use a (typesafe) enum—which incidentally can have state and behaviour—and use an EnumMap, which is highly efficient. Internally it’s just an array indexed by enum ordinal value. You might argue what’s the difference between that and an int array? The answer is that the int array inside EnumMap is an internal implementation detail for a typesafe random access collection.

    If you allow freeform text for the exit direction, your structure will look something like this:

    Map<String, Direction> exits;
    

    I don’t recommend this however. I recommend enumerating possible directions:

    public enum Direction {
      NORTH("north", "n"),
      NORTHWEST("northwest", "nw"),
      ...
      IN("in"),
      OUT("out");
    
      private final static Map<String, Direction> INSTANCES;
    
      static {
        Map<String, Direction> map = new HashMap<String, Direction>();
        for (Direction direction : values()) {
          for (String exit : direction.exits) {
            if (map.containsKey(exit)) {
              throw new IllegalStateException("Exit '" + exit + "' duplicated");
            }
            map.put(exit, direction);
          }
        }
        INSTANCES = Collections.unmodifiableMap(map);
      }
    
      private final List<String> exits;
    
      Direction(String... exits) {
        this.exits = Collections.unmodifiableList(Arrays.asList(exits));
      }
    
      public List<String> getExits() { return exits; }
      public String getName() { return exits.get(0); }
      public static Map<String, Direction> getInstances() { return INSTANCES; }
      public static Direction getDirection(String exit) { return INSTANCES.get(exit); }
    }
    

    which you then store with:

    private final Map<Direction, Exit> exits =
      new EnumMap<Direction, Exit>(Direction.class);
    

    That gives you type-safety, performance and extensibility.

    The first way to think about this is as a Map:

    Map<String, Room> exits;
    

    where the key is a freeform direction (north, east, south, etc).

    Next question: what is an Exit? In the simplest case, an Exit is simply what Room you end up in but then you start asking all sorts of questions like:

    • Can the player see the exit?
    • Is the exit closed or open?
    • Can the exit be closed, opened, locked, unlocked, pushed open, etc?
    • Can use of the exit be programmatic (eg you have to be carrying a certain amulet)?
    • Can where you end up be programmatic (eg you could fall down a pit-trap and end up somewhere else entirely)?
    • Can using an exit trigger some other action (eg setting off an alarm)?

    It is necessary to consider the interface for a text adventure game. A player types in commands in the following form:

    Verb [[preposition1] object1 [[preposition2] object2]] 
    

    That’s one possibility at least. Examples would include:

    • sit (verb = sit);
    • open door (verb = open, object1 = door)
    • look at book;
    • lock chest with iron key (verb = lock, object1 = chest, preposition2 = with, object2 = iron key);
    • cast fireball at orc;
    • etc.

    So the above covers a fairly comprehensive set of behaviour. The point of all this is:

    • Exits will support a number of Verbs or Commands (eg you can open/close a door but not a passageway);
    • Monsters and items also will support commands (a wand can be “waved” and an orc can be “hit”);
    • Exits, monsters and items are then all types of Objects (being things in game that can be interacted with in some way).

    so:

    public enum Command { LOOK, HIT, WAVE, OPEN, CLOSE, ... };
    

    (and there will no doubt be behaviour associated with those instances) and:

    public class GameObject {
      boolean isSupported(Command command);
      boolean trigger(Command command);
    }
    
    public class Exit extends GameObject {
      ...
    }
    

    GameObjects may also have other state such as whether they can be seen or not. Interestingly, the Direction enum instances are also arguably Commands, which again changes the abstraction.

    So hopefully that should help point you in the right direction. There is no “right” answer for the abstraction because it all depends on what you need to model and support. This should hopefully give you a starting point however.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 256k
  • Answers 256k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the foreach construct to iterate through each item in… May 13, 2026 at 10:27 am
  • Editorial Team
    Editorial Team added an answer Yes, node content information is in the revisions table, not… May 13, 2026 at 10:27 am
  • Editorial Team
    Editorial Team added an answer see also the Type.IsInstanceOfType method in the .NET framework, if… May 13, 2026 at 10:27 am

Related Questions

This may be a duplicate question as I don't know to phrase the search
SQL to find duplicate entries (within a group) I have a small problem and
I'm passing a table of up to 1000 rows, consisting of name, ID, latitude
I have two tables: OutputPackages (master) |PackageID| OutputItems (detail) |ItemID|PackageID| OutputItems has an index
I am filling items to my ComboBox from a XML file using a DataTable.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.