From a2ac95a1840d182dd5f19c3fd12d1ff03de58d5b Mon Sep 17 00:00:00 2001 From: Aaron Gonzalez Date: Sun, 28 Apr 2019 23:32:11 -0500 Subject: [PATCH] mastermind --- 04week/mastermind.js | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/04week/mastermind.js b/04week/mastermind.js index 60e5cfa18..e21d4cdfd 100644 --- a/04week/mastermind.js +++ b/04week/mastermind.js @@ -8,7 +8,7 @@ const rl = readline.createInterface({ }); let board = []; -let solution = ''; +let solution = 'abcd'; //set global variable #0 let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; function printBoard() { @@ -28,13 +28,45 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { +function generateHint(guess) { //pass in guess to generate hint #2 // your code here + let guessArray = guess.split(''); //create arrays that slit up passed in arguments #2.1 + let solutionArray = solution.split(''); //create arrays that slit up passed in arguments #2.1 + let correctLetterLocations = 0; //determine correct letter locations #2.2 + for (let i = 0; i n; n++){ + let targetIndex = guessArray.indexOf([solutionArray[n]]); + if (targetIndex > -1){ + correctLetters += 1; + solutionArray[n] = null; + } +} + +let hint = correctLetterLocations + "-" + correctLetters; //determine hint #2.4 +console.log(hint); +return hint; } function mastermind(guess) { solution = 'abcd'; // Comment this out to generate a random solution // your code here + if (guess === solution) { //add guess #3 + console.log('You guessed it!') //if guess is equal to solution then return you guessed it #1 + } else { + generateHint(solution, guess); + console.log('try again'); + board += 1; + } + if (board.length >= 10) { //end game #4 + console.log('You ran out! The solution was: ${solution}'); + return; + } } @@ -62,10 +94,10 @@ if (typeof describe === 'function') { describe('#generateHint()', () => { it('should generate hints', () => { - assert.equal(generateHint('abdc'), '2-2'); + assert.equal(generateHint(solution, 'abdc'), '2-2'); }); it('should generate hints if solution has duplicates', () => { - assert.equal(generateHint('aabb'), '1-1'); + assert.equal(generateHint(solution, 'aabb'), '1-1'); }); });