diff --git a/04week/mastermind.js b/04week/mastermind.js index 60e5cfa18..4fc2745f8 100644 --- a/04week/mastermind.js +++ b/04week/mastermind.js @@ -11,6 +11,8 @@ let board = []; let solution = ''; let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; + + function printBoard() { for (let i = 0; i < board.length; i++) { console.log(board[i]); @@ -28,16 +30,45 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { - // your code here +function generateHint(solution, guess) { + let solutionArray = solution.split(''); + let guessArray = guess.split(''); + let correctLetterLocations = 0; + for (let i=0; i -1) { + correctLetters = correctLetters + 1; + solutionArray[j] = null; + } + } + return correctLetterLocations+"-" + correctLetters; + //fancier hint generator that the NPM tests won't like: + // return correctLetterLocations+" correct and " + correctLetters + " in the wrong place"; } + function mastermind(guess) { - solution = 'abcd'; // Comment this out to generate a random solution - // your code here + solution = 'abcd'; + if (board.length >= 10){ + console.log('You ran out of guesses!') + } else { + if (guess != solution) { + let hints = generateHint(solution, guess); + board.push(hints) + } else { + console.log('You guessed it!'); + return 'You guessed it!'; + } + } } - function getPrompt() { rl.question('guess: ', (guess) => { mastermind(guess); @@ -59,19 +90,15 @@ if (typeof describe === 'function') { assert.equal(mastermind(solution), 'You guessed it!'); }); }); - 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'); }); - }); - } else { - generateSolution(); getPrompt(); -} +} \ No newline at end of file