Last active
December 8, 2022 20:29
-
-
Save peterjacobson/305184d71fa347ea5b95 to your computer and use it in GitHub Desktop.
A simple number guessing game in node.js
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
//var process = require('process'); | |
var readline = require('readline'); | |
var randomNumber = Math.round(Math.random() * 10); | |
var lives = 5; | |
var terminal = readline.createInterface( | |
{ | |
input : process.stdin, | |
output : process.stdout | |
}); | |
terminal.setPrompt('Guess the number! (0-10): '); | |
terminal.prompt(); | |
terminal.on('line', function(answer) | |
{ | |
var answerNum = parseInt(answer); | |
if (answerNum > randomNumber) | |
{ | |
console.log('Too high!'); | |
console.log('You have '+lives+' lives left'); | |
} | |
else if (answerNum < randomNumber) | |
{ | |
console.log('Too low!'); | |
console.log('You have '+lives+' lives left'); | |
} | |
else if (answerNum === randomNumber) | |
{ | |
console.log('W I N N E R ! ! !'); | |
console.log('You lost only '+ (6-lives) + ' lives'); | |
process.exit(0); | |
} | |
else | |
{ | |
console.log("That wasn't a number I recognise"); | |
console.log('You have '+lives+' lives'); | |
} | |
lives--; | |
if (lives == 0) | |
{ | |
console.log('G A M E O V E R ! ! !'); | |
process.exit(0); | |
} | |
terminal.prompt(); | |
}); | |
terminal.on('close', function() | |
{ | |
console.log('C H I C K E N :P') | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment