Last active
February 9, 2025 15:33
-
-
Save queerviolet/7d9fb275b292b062fa5b9b4c99eda77d to your computer and use it in GitHub Desktop.
Player inquirer loop for choose your own adventure.
This file contains hidden or 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
"use strict"; | |
var inquirer = require('inquirer'); | |
var game = require('./game.source'); | |
// play(node: Node) -> Promise<Node> | |
function play(node) { | |
if (!node.connections.length) { | |
console.log(node.text); | |
return Promise.resolve({node}); // Note: `{node}` is ES6 object shorthand syntax. ES5 would be Promise.resolve({node: node}) | |
} | |
return inquirer.prompt({ | |
name: 'node', | |
message: node.text, | |
type: 'list', | |
choices: node.connections, | |
}).then(answer => play(answer.node)); // Note: This is an ES6 Arrow function. | |
// ES5 would be `.then(function(answer) { return play(answer.node); })` | |
} | |
play(game.startingPoint) | |
.then(last => console.log('Game over.')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment