diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..68586da Binary files /dev/null and b/.DS_Store differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..c9a1a5d --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "build-a-bot", + "version": "1.0.0", + "description": "Work with your team to build a bot (command line app)", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/xramirez/build-a-bot.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/xramirez/build-a-bot/issues" + }, + "homepage": "https://github.com/xramirez/build-a-bot#readme", + "keywords": [], + "dependencies": { + "fs": "^0.0.1-security", + "prompt-sync": "^4.2.0" + } +} diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..a927627 --- /dev/null +++ b/src/index.js @@ -0,0 +1,72 @@ +const prompt = require('prompt-sync')({ sigint: true }); +const fs = require('fs'); + +//Be able to read from a file + +function playGame() { + let tree = fs.readFileSync('tree.json'); + let root = JSON.parse(tree); + //console.log(root); + + let node = root; + let input = ""; + let isGuess = false; + + while (isGuess == false) { + input = prompt(`${node.question} `).toLowerCase(); + if (input == 'yes') { + node = node.yes; + //console.log(node); + } + else if (input == 'no') { + node = node.no; + //console.log(node); + } + else + console.log("Make sure you answer with a yes or a no! Maybe doesn't cut it..."); + for (let options in node) { + if (options == 'guess') { + while (true) { + input = prompt(`${node.guess} `).toLowerCase(); + if (input == 'yes') + return true; + else if (input == 'no') + return false; + else + console.log("Make sure you answer with a yes or a no! Saying maybe doesn't cut it...") + } + } + } + } +} + +function startAndLoop() { + console.log("Welcome! This is the Pet Guessing Bot!\n\nGuess an animal that you'd usually have as a pet."); + console.log("We're going to ask you a list of questions to try and guess what kind of animal you're thinking of!") + console.log("You must either with answer with either yes or no!\n\nReady? Let's get down to it:"); + let repeatGame = true; + while (repeatGame == true) { + let gameWon = playGame(); + let input = "" + if (gameWon == true) + console.log("Yay! Thanks for playin'!"); + if (gameWon == false) + console.log("Looks like you've got a pet we didn't even think of!"); + input = prompt("Would you like to play again? ") + if (input == 'yes') { + console.log("Perfect! Let's do this:\n\n\n"); + } + else if (input == 'no') { + console.log("We're sad to see you go :( Thanks for playing!"); + repeatGame = false; + } + else + { + console.log("Well, you didn't say yes, so we'll call it for now. See you next time!"); + repeatGame = false; + } + } +} + +startAndLoop() +//console.log(root.question) \ No newline at end of file diff --git a/src/tree.json b/src/tree.json new file mode 100644 index 0000000..8d9ef2a --- /dev/null +++ b/src/tree.json @@ -0,0 +1,96 @@ +{ + "question": "Are you a mammal?", + "yes": { + "question": "Are you a rodent?", + "yes": { + "question": "Do you have a long tail?", + "yes": { + "question": "Is your tail furry?", + "yes": { + "guess": "Are you a ferret?" + }, + "no" : { + "guess": "Are you a mouse/rat?" + } + },"no" :{ + "question": "Do you have quills?", + "yes": { + "guess": "Are you a hedgehog?" + }, + "no" : { + "guess": "Are you a hamster/guinea pig?" + } + } + },"no": { + "question": "Do you have hooves?", + "yes": { + "question": "Are you usually used for milk?", + "yes": { + "question": "Are you used for beef patties?", + "yes": { + "guess": "Are you a cow?" + }, + "no": { + "guess": "Are you a goat?" + } + },"no": { + "question": "Are you used for bacon?", + "yes": { + "guess": "Are you a pig?" + },"no": { + "guess": "Are you a horse?" + } + } + },"no": { + "question": "Do you bounce?", + "yes": { + "guess": "Are you a rabbit?" + },"no": { + "question": "Do you have retractable claws?", + "yes": { + "guess": "Are you a cat?" + },"no": { + "guess": "Are you a dog?" + } + } + } + } + },"no": { + "question": "Are you a bird?", + "yes": { + "question": "Do you sing?", + "yes": { + "guess": "Are you a parrot?" + },"no": { + "question": "Do you gobble gobble?", + "yes": { + "guess": "Are you a turkey?" + },"no": { + "guess": "Are you a chicken?" + } + } + },"no": { + "question": "Are you a reptile?", + "yes": { + "question": "Do you have legs?", + "yes": { + "question": "Do you have a shell?", + "yes": { + "guess": "Are you a turtle?" + },"no": { + "guess": "Are you a lizard?" + } + },"no": { + "guess": "Are you a snake?" + } + },"no": { + "question": "Do you have legs?", + "yes": { + "guess": "Are you a frog?" + },"no": { + "guess": "Are you a fish?" + } + } + } + } +} \ No newline at end of file