Choose Your Own Adventure

Explore different paths, make choices, and see where the adventure takes you!

Design

I wanted to create a choose-your-own-adventure where all of the data about the locations and the movement for the game were all contained in the data, not in the program structure. If you look at the code, you can see how little code there is. There are about 15 lines of actual code, and all of the information about the game, all of the movement, and so on, is included in a single data object.

Challenges

I couldn't remember at first how to store data in a JavaScript when it was being triggered by elements on a webpage. Then I remembered that actually, as long as you do not reload the page, global variables keep their value. That is how I kept track of "currentRoom."

Solution

This choose-your-own-adventure game takes a unique approach by storing all the data about the locations and movement in a single data object. The game design prioritizes simplicity and ease of maintenance, with only about 15 lines of actual code.

Instead of embedding the game logic within the program structure, the data object contains all the information about the game, including descriptions of locations, available exits, and more. This approach allows for easy updates and modifications to the game content without changing the underlying code.


          rooms = {
            cu1: {
                name: "Classroom Unit One",
                description: "This is a large lecture room that holds maybe a couple hundred people. \
                    Half of the seats are filled by laconic students who are all looking at their phones and \
                    laptops. The lecturer hasn't arrived yet, so maybe you are still early for class. Perhaps \
                    there is still time to get a cup of coffee at the cafe. You'll need that too, if you want \
                    to survive your Astronomy 101 lecture.",
                exitKeys: ['foyer', 'forest1'],
                exitTexts: ["Go out the front of the lecture hall",
                    "Slip out the back door."],
            }
        

One of the key advantages of this design is how easy it is to extend the game with additional elements like images or sounds. By simply modifying the data object, you can incorporate visual or auditory elements into the gameplay, enhancing the overall experience for the players.