Last active
March 26, 2022 12:06
-
-
Save jeroenvanwijgerden/1c67d3af4086b8f84dc7adb52af77b18 to your computer and use it in GitHub Desktop.
small Choose Your Own Adventure program in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const states = { | |
1 : ["You are in a dark dungeon. In front of you a heavy oaken door stands ajar. Behind you is a hole from which foul odors rise.", | |
[[2, "Go through the door.", "You go through the door."], | |
[3, "Go down the hole.", "As you carefully clamber down the hole, your grip slips and you plummet into the darkness."]]], | |
2 : ["You are in a narrow, dimly lit corridor. To your left is more darkness, to your right is light.", | |
[[3, "Go left.", "As you walk, your foot lands of a pressure plate. With the low rumble of stone against stone, the floor opens and you fall down."], | |
[4, "Go right.", "You walk along the corridor."]]], | |
3 : ["After a monstrous splash you emerge, gasping, from filth-laden water. In the distance you see the edge of the sewage canal. Below the surface you feel that you're standing on some sort of log.", | |
[[5, "Swim to the edge.", "After many strokes through the murky water you hoist yourself onto the edge."], | |
[6, "Dive to the log.", "One last time you fill your lungs with air and go under."]]], | |
4 : ["You come across a guard who kills you, an escaped prisoner, on the spot."], | |
5 : ["You find yourself in a goblin camp."], | |
6 : ["Through the murky water you see a faintly glowing orb on the side of the log. The orb turns towards you. The crocodile to which the eye belongs strikes quick as lightning and kills you with its countless razor teeth."] | |
} | |
function act_out(state) { | |
const [text, choices] = state | |
console.log(text); | |
if (choices) { | |
console.log("What do you do?") | |
choices.forEach(([_nr_next_state, text, _action_text], index) => console.log(index + ") " + text)) | |
let index_selected_choice = 0 | |
do { | |
index_selected_choice = parseInt(prompt()) | |
} while (!(0 <= index_selected_choice && index_selected_choice < choices.length)) | |
const [nr_next_state, _text, action_text] = choices[index_selected_choice]; | |
console.log(action_text) | |
act_out(states[nr_next_state]) | |
} else { | |
console.log("Game over.") | |
} | |
} | |
act_out(states[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment