Skip to content

Instantly share code, notes, and snippets.

@zgulde
Last active February 21, 2016 18:51
Show Gist options
  • Save zgulde/54e91f1d53ccc3419e91 to your computer and use it in GitHub Desktop.
Save zgulde/54e91f1d53ccc3419e91 to your computer and use it in GitHub Desktop.
simple simon for the command line
function sample(array){
return array[Math.floor(Math.random() * array.length)];
}
/**
* colorize and individual color functions adapted from
* https://kpumuk.info/ruby-on-rails/colorizing-console-ruby-script-output/
*/
function colorize(text, code){
return '\033[' + code + 'm' + text + '\033[0m';
}
function red(text){
return colorize(text, 31);
}
function green(text){
return colorize(text, 32);
}
function yellow(text){
return colorize(text, 33);
}
function blue(text){
return colorize(text, 34);
}
function showSequence(sequence, cb){
sequence.forEach(function(color, index){
setTimeout( function(){
showColor(color);
setTimeout( function(){
process.stdout.write('\r ');
}, 800);
}, 1000 * index);
});
setTimeout( function(){
process.stdout.write('\r');
cb();
}, 1000 * (sequence.length + 1) - 800);
}
function showColor(color){
process.stdout.write('\r' + color.func('===' + color.name + '==='));
}
/**
* gets user input from the command line
*
* @param {String} prompt [message to display prompting the user]
* @param {Function} cb [callback function, will be passed the user input
* as a string]
*/
function getInput(prompt, cb){
if (typeof prompt !== 'string' || typeof cb !== 'function') {
throw 'getInput expects a string \'prompt\' and a function \'cb\'';
}
process.stdout.write(prompt);
process.stdin.resume();
process.stdin.once('data', function(data){
cb(data.toString().trim());
});
}
/**
* grabs the first character of every word the user entered and converts it to
* lowercase, checks to see if that string is equal to the same transformation
* applied to the simon sequence of color names
*/
function checkUserInput(userSequence, simonSequence){
return userSequence.trim().split(' ').map(function(color){
return color.substr(0, 1).toLowerCase();
}).join('') === simonSequence.map(function(color){
return color.name.substr(0, 1).toLowerCase();
}).join('');
}
function getNewColor(){
return sample([
{name: 'red', func: red},
{name: 'green', func: green},
{name: 'yellow', func: yellow},
{name: 'blue', func: blue}
]);
}
function startRound(simonSequence){
console.log('\033cScore: ' + simonSequence.length);
simonSequence.push(getNewColor());
showSequence(simonSequence, function(){
getInput('enter the sequence separated by spaces\n> ', function(input){
if (checkUserInput(input, simonSequence)){
startRound(simonSequence);
} else {
endGame(simonSequence);
}
});
});
}
function endGame(simonSequence){
console.log('Game Over!');
console.log('Simon Sequence: ');
process.stdout.write(simonSequence.map(function(color){
return color.func(color.name);
}).join(' | '));
console.log('');
console.log('Your Score: ' + (simonSequence.length - 1));
process.exit();
}
console.log('Simple Simon!');
startRound([]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment