The Room Adventure: Additional Features

Keep an item inside a specific room when randomizing items

Difficulty: ★★☆☆☆

Requirement: randomizeItems function

You may create your game where you want some of your items to be randomly scattered, but other items kept in a certain place. For example, if you have a room that needs a screwdriver and you want to keep the screwdriver in the basement, now you'll be able to.

Let's create a way of ignoring specific rooms when we shuffle items. This way, we can control the game the way we want to.

What we need to do

  • Add properties to rooms that we don't want to shuffle.
  • As we prepare to randomize items, set the non-shuffled fixWith items into their desired itemFound locations.
  • Skip over pre-set items as we set other items.

Step 1: Go into your randomizeItems function

All changes will take place in your randomizeItems function. For this feature to work, we need to make sure there is a fixWith and a matching itemFound. So you actually have to set the locations of two things. We'll do this by adding a property to the non-shuffled rooms.

To begin, we need to start with an empty array so we can track the items we don't want to move.

This goes at the very top of the function as shown.

Step 2: Removing the non-shuffle items from the list

The function starts with a loop that cycles through all the rooms and makes a list of all the fixWith items. Some of those will be items we don't want to move. So let's pull them off that list.

This goes right after the player.itemsLeftToFix increase.

Step 3: Skipping rooms

We pulled the items off the list so we don't move them. Now we need to make sure we don't set new items to those rooms. Instead of altering our availableRooms object, we're just going to put a check in the loop.

Let's move down to the for...in loop that actually randomizes the items.

If you added the feature that makes sure no itemFound items are in their fixWith rooms, then the loop you're looking for will be wrapped in a do...while loop, but this loop is still inside.

I stuck the console.log() call in there just for debugging. You don't have to include it at all. But if you do and you run the program, open up developer tools (try pressing F12) and you should see the rooms that were skipped. Those should be the same as the ones you gave noShuffle properties to. If they don't show up, it's possible the room names are not spelled the same, or maybe one room isn't on the availableRooms list for some reason, such as if the optional randomizeRoomLayout function dropped one of the rooms.

Generally, when I'm checking to see if something is in an array or not, I check to see if the indexOf is or is not -1. But seeing !== -1 means "not equal to -1" or "the room is not, not on the list." Translation: "It is on the list." So this time, I made the comparision >= 0 so it's a little easier to read and understand.

Step 4: Adding a noShuffle property to a room

All that's left is to set up a noShuffle property with your rooms when you create them. All you need to do is to go your getRooms function and find the room or rooms you want to keep from shuffling and add a new noShuffle property pointing to the room the item should be found in.

I'm going to put the basement's fixWith item in the office every time.

Remember, you can put the property anywhere in the object, but make sure it's not inside an inner object like exits.

If you included the optional feature that has multiple brokenThings (the brokenArray) for the program to pull from, this feature will still put the fixWith item into the specified room.

A friend of mine made a Beauty and the Beast version of this game and she always wanted true love to be found in the Library. The true love was needed in the West Wing where the enchanted rose was the brokenThing. If she gave the West Wing a brokenArray with a chair as the brokenThing, then Mrs. Potts' Complete Guide To Chair Repair would be found in the Library instead of true love, which would be fine because the enchanted rose wouldn't be broken in that run of the game.

Now that you know that you can find true love in the library, get searching!

—Dr. Wolf