Text Adventure Time! (Converting the Rooms 2D Array to a HashMap)

Text Adventure Time! (Converting the Rooms 2D Array to a HashMap)

Some time ago, when I was brushing off my Java, I wanted to get my hands dirty while making something fun. I LOVE text adventure RPGs, so that’s what I attempted to build.

This is the fourth post documenting my single user dungeon development adventure.

In my last Java Text Aventure post, I left off at commit 4253cd9, where I converted the Enum command handling to a HashMap of Runnables.

In this post I’ll be covering the conversion from a 2D Room array to a HashMap of Room objects! I’ll also be directly linking the rooms by adding a list of exits to each room, instead of arbitrarily controlling the connections via coordinates in the 2D array.

Here is the list of posts published on this project so far!

Check out the repo for this post and follow along!

Repo:

Target commits:

Running the game:

The World before the great 2D array sundering

This is what the World looked like:

Harsh. Unforgiving. Bounded by the Terrible Arbitrary Constants WIDTH and HEIGHT. Forcing us to live in fear of the inevitable ArrayIndexOutOfBoundsException!

No longer! We cast off the yoke of these gaolers, these false idols. No more shall we suffer repression (you saw it, didn’t you? !) at the hands of room[0][1] and its ilk, forced to dig out the connection between rooms by manually comparing names with coordinates.

Stage 1 of the liberation: the exits HashMap!

This is not the complete solution, just a first step in the right direction. Instead of a single String containing the directions and names of all rooms connecting to a particular room, the exits are now a HashMap, with the keys as the directions, and the values as the Room names.

This:

Becomes this:

Breaking the 2D array chains

The Room.exits HashMap parameter is updated again, so that the value is another Room object.

Then in World.java, we can give each Room a better sense of identity by calling it by its name:

This way, we don’t have to reference the room by an array coordinate:

But can instead do this:

Where the setExits method (named somewhat inappropriately: there should really be another method named addExits to perform this action) simply puts the entry in the HashMap.

Excellent, no more 2D array!

What did these changes accomplish? Well, now changing rooms is MUCH simpler, and Commands.java can get a facelift.

Since the directional information is stored in each Room object, there’s no need to keep track of x and y coordinate directions to control the relationships between the rooms. That lets us blow away the noisy methods for goNorth, goSouth, goEast, and goWest (16 lines each!) and replace them with a single (still noisy) method: exitRoom (only 15 lines to handle all directions!), which just takes a directional parameter. All we need to keep track of is our current location: Main.currentRoom.

The directional parameter comes from the command entered by the user in the Input class. The Runnable is then selected from the commands HashMap, and the exitRoom method is called with the directional parameter. (See the post on the Runnables implementation to see how this works.)

The commands HashMap entries for direction:

Basically, the room’s exits HashMap is checked for the passed-in direction parameter. If one is found, Main.currentRoom is updated and the new room’s info is printed!

The game plays exactly the same as it did before these changes, of course, but now it’s a lot easier to add new rooms (and even generate rooms and their connections dynamically; more on that in a future post!)

Never flinch from a fra-ee-ay-ee-ay!

2 thoughts on “Text Adventure Time! (Converting the Rooms 2D Array to a HashMap)

Join the Discussion

%d bloggers like this: