Created
October 30, 2015 04:45
-
-
Save pl12133/57f72770314cbc7b28ed to your computer and use it in GitHub Desktop.
Daily Programmer challenge 2015-10-28
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'; | |
function mastermind() { | |
// Requires | |
var fs = require('fs'); | |
var readline = require('readline'); | |
/// | |
// Members | |
var prompts = { | |
banner: function() { | |
return 'Welcome to the Mastermind Guessing Game'; | |
}, | |
correct: function() { | |
return 'Correct! Secret Word was ' + chosenSecretWord; | |
}, | |
guess: function() { | |
return 'Guess? (' + guessesRemaining-- + ' left) '; | |
}, | |
lose: function() { | |
return 'You Lose! Secret Word was ' + chosenSecretWord + ', try again'; | |
}, | |
invalidDifficulty: function() { | |
return 'Invalid Difficulty, try again'; | |
} | |
} | |
var FILE_NAME = './enable1.txt'; // filename of dictionary | |
var SAMPLE_SIZE; //determined by difficulty | |
var WORD_LENGTH; //determined by difficulty | |
var dictionary = fs.readFileSync(FILE_NAME, { encoding: 'utf-8' }).split('\n'); | |
var difficulty; // prompted | |
var words; // dictionary filtered by word length | |
var randomSample; // random sampling of words array | |
var guessesRemaining; // number of remaining guesses | |
var chosenSecretWord; // secret word randomly chosen from randomSample | |
/// | |
function numCommonCharacters(word1, word2) { | |
// Assume two words are equal length | |
word1 = word1.toLowerCase(); | |
word2 = word2.toLowerCase(); | |
return word1.split('').reduce(function(memo, current, index) { | |
if (current === word2.charAt(index)) | |
memo += 1; | |
return memo; | |
}, 0); | |
} | |
function nLetterWordsFilter(n, words) { | |
function doFilter(words) { | |
return words.filter(function(elem) { | |
return elem.length === n; | |
}); | |
} | |
// If no words are provided return curried function | |
if (!words) return doFilter; | |
// If words return a filtered array | |
return doFilter(words); | |
} | |
function init() { | |
// Initialize the game | |
SAMPLE_SIZE = 2 * difficulty + 4; | |
WORD_LENGTH = 3 * difficulty; | |
guessesRemaining = 4; | |
// Filter dictionary by word length | |
words = nLetterWordsFilter(WORD_LENGTH, dictionary); | |
// Create a random sample | |
randomSample = new Array(SAMPLE_SIZE).fill(0).map(function() { | |
return words[Math.floor(Math.random() * words.length)]; | |
}); | |
// Choose a secret word | |
chosenSecretWord = randomSample[Math.floor(Math.random() * randomSample.length)]; | |
// Print choices | |
console.log("Choices:"); | |
console.log(randomSample); | |
} | |
// input listening beginning is the beginning of the game | |
function beginInputListening() { | |
var rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: true | |
}); | |
// Print the banner if there is one | |
if (prompts.banner) console.log(prompts.banner()); | |
rl.setPrompt('Difficulty? 1-5 '); | |
rl.prompt(); | |
rl.on('line', function(line) { | |
// Difficulty is the first thing prompted | |
if (!difficulty) { | |
difficulty = +line; | |
if ((0 >= difficulty) || (difficulty > 5)) { | |
console.log(prompts.invalidDifficulty()); | |
difficulty = false; | |
rl.prompt(); | |
return; | |
} else { | |
// Init game and start guessing | |
init(); | |
rl.setPrompt(prompts.guess()); | |
rl.prompt(); | |
return; | |
} | |
} | |
// Check the Guesses | |
if (chosenSecretWord === line) { | |
// Correct answer | |
console.log(prompts.correct()); | |
guessesRemaining = 0; | |
rl.close(); | |
} | |
if (guessesRemaining > 0) { | |
// Still guessing | |
var lettersInCommon = numCommonCharacters(chosenSecretWord, line); | |
console.log(lettersInCommon + '/' + WORD_LENGTH + ' correct'); | |
rl.setPrompt(prompts.guess()); | |
rl.prompt(); | |
} else { | |
// Game over | |
console.log(prompts.lose()); | |
rl.close(); | |
} | |
}).on('close', function() { | |
process.exit(0); | |
}); | |
} | |
return { | |
// The only function that needs to be exposed is beginInputListening | |
// this function will begin the game | |
play: beginInputListening | |
} | |
} | |
var game = mastermind(); | |
game.play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment