Skip to content

Instantly share code, notes, and snippets.

@Rob-pw
Created March 6, 2014 05:24
Show Gist options
  • Save Rob-pw/9383099 to your computer and use it in GitHub Desktop.
Save Rob-pw/9383099 to your computer and use it in GitHub Desktop.
A simple guessing game, produced for Matt Hartensveld of R.I.T.
/**
* Created by robert on 06/03/14.
*/
"use strict";
var readline = require('readline'),
rl = readline.createInterface(process.stdin, process.stdout);
/**
* This is our game object, we can create instances of this.
* @param triesLeft The number of tries the user has left.
* @param minNumber The minimum value of the number to guess
* @param maxNumber The maximum value of the number to guess
* @constructor
*/
var GuessingGame = function (triesLeft, minNumber, maxNumber) {
// Private variable for the numberToGuess
var numberToGuess = Math.floor(Math.random() * (maxNumber - minNumber + 1)) + minNumber;
/**
* Tests the guess against the hidden value
* @param guess The guessed number
* @returns An object with a boolean value for correctness,
* and an integer of -1 to 1 (inclusive) showing the relative position of the guess.
*/
this.wasGuess = function (guess) {
if (triesLeft-- <= 0) {
return false;
}
var correct = (guess === numberToGuess);
return {
correct : correct,
position : (correct ? 0
: guess > numberToGuess ? 1
: -1)
}
};
};
GuessingGame.prototype.makeGuess = function (guess) {
var guessResult = this.wasGuess(guess),
position = guessResult.position;
if (!guessResult) {
console.log("Sorry, too many tries.");
return false;
}
console.log("Your guess was " + (position === 0 ? "spot-on, good job!"
: position === 1 ? 'too high'
: 'too low'));
if (guessResult.correct) {
this.makeGuess = function () {
console.log("The game is over, you have won.");
};
return true;
}
// Implicit
// return undefined;
};
// ** Now, let's setup the game. **
(function init () {
var game,
maxTries,
minNumber,
maxNumber,
currentStageNumber = 0,
currentStage,
numberOfStages;
/**
* I'm trying to be clever here.
* I've made an array of stages, with prompts and callback functions
* for once input is received.
*
* We loop through this array, and look for the callback to return true (for success),
* or false for failure.
*/
var stages = ["Welcome to the game!",
{
prompt: "Please, enter the max number of tries",
callback: function (input) {
maxTries = input;
}
},
{
prompt: "Now, what's the minimum possible value of the number?",
callback: function (input) {
minNumber = input;
}
},
{
prompt: "Finally, what's the max?",
callback: function (input) {
maxNumber = input;
// We have everything we need.
console.log(maxTries, minNumber, maxNumber);
game = new GuessingGame(maxTries, minNumber, maxNumber);
}
},
{
prompt: "What's your guess?",
callback: function (guess) {
return game.makeGuess(guess);
}
}
];
// Cache the numberOfStages.
numberOfStages = stages.length;
/**
* Recursive function which we call once input is received..
* Highly skeptical that this is a 'good' approach, but it's 5am, so that's my excuse.
*/
function nextStage () {
currentStage = stages[currentStageNumber];
if(typeof currentStage === "string") {
// It's only strings :3
console.log(currentStage);
currentStageNumber += 1;
return nextStage();
} else {
/**
* We call the question method of readline, passing the prompt to display,
* and the callback for once input is received.
*/
rl.question(currentStage.prompt + "> ", function (line) {
var result = currentStage.callback(parseInt(line, 10));
if(result !== undefined) {
/**
* If we either succeed in finding the number (true),
* or fail because we've run out of tries (false),
* let's exit.
*/
return;
}
/**
* If we're on any stage other-than 'make a guess',
* let's increment the stage number and carry on.
*
* Otherwise we just loop till we find an answer,
* or fail to guess in the given try allowance.
*/
if (currentStageNumber < numberOfStages - 1) {
currentStageNumber += 1;
}
nextStage();
});
}
}
/**
* Start the recursive loop.
*/
nextStage();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment