From d84d80b1700e6c9563e0da1d303477d9e4c76560 Mon Sep 17 00:00:00 2001 From: James Messina Date: Tue, 16 Feb 2021 13:03:42 -0600 Subject: [PATCH 1/2] Mastermind Project Complete --- main.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index 64ca54b..13bbb39 100644 --- a/main.js +++ b/main.js @@ -28,13 +28,57 @@ const getRandomInt = (min, max) => { return Math.floor(Math.random() * (max - min)) + min; } -const generateHint = () => { +const generateHint = (guess) => { // your code here + let hints = 0; + let hints2 = 0; + + let guessArray = guess.split(''); + let solutionArray = solution.split(''); + + guessArray.forEach(function(letter, i){ + if(guessArray[i] == solutionArray[i]){ + hints++; + guessArray[i] = 0; + solutionArray[i] = 1; + } + }) + + guessArray.forEach(function(guessLetter, index1){ + solutionArray.forEach(function(solutionLetter, index2){ + if(guessLetter == solutionLetter){ + hints2++; + guessArray[index1] = 0; + solutionArray[index2] = 1; + } + }) + }) + return hints + "-" + hints2 } const mastermind = (guess) => { - solution = 'abcd'; // Comment this out to generate a random solution + //solution = 'abcd'; // Comment this out to generate a random solution // your code here + + if(typeof guess !== 'string' || guess.length !== 4){ + console.log('Guess must be 4 letters long using only letters A-H'); + return false; + } + + guess = guess.toLowerCase().trim(); + + if(guess == solution){ + board = []; + console.log("You guessed it!") + return "You guessed it!"; + }else if(board.length === 10){ + console.log("You lost! The solution was " + solution); + board = []; + generateSolution(); + }else{ + board.push(guess) + console.log(generateHint(guess)) + } } @@ -74,4 +118,11 @@ if (typeof describe === 'function') { generateSolution(); getPrompt(); -} \ No newline at end of file +} +//guess must only be letters/strings from letters array and 4 letters in length, if not then log 'can only use 4 letters from range a-h +//if user inputs whitespace and uppercase letters then use trim method tolowercasw method +//generate a solution for the user to guess +//push each guess into the board array +//compare the board array(guess) to the solution array +//iterate over board array(guess) and compare the element at each index to that of the solution array + //if \ No newline at end of file From b6704793a743b5d10cbe922e9655a8f629c71a69 Mon Sep 17 00:00:00 2001 From: James Messina Date: Tue, 16 Feb 2021 16:17:40 -0600 Subject: [PATCH 2/2] fixed the generateHint function --- main.js | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/main.js b/main.js index 13bbb39..c399a1e 100644 --- a/main.js +++ b/main.js @@ -29,31 +29,23 @@ const getRandomInt = (min, max) => { } const generateHint = (guess) => { - // your code here - let hints = 0; - let hints2 = 0; let guessArray = guess.split(''); let solutionArray = solution.split(''); + let fullHints = 0; + let halfHints = 0; - guessArray.forEach(function(letter, i){ - if(guessArray[i] == solutionArray[i]){ - hints++; - guessArray[i] = 0; - solutionArray[i] = 1; + for(let i = 0; i < guessArray.length; i++){ + if(guessArray[i] === solutionArray[i]){ + fullHints++; + solutionArray[i] = 0; + }else if(solutionArray.indexOf(guessArray[i]) > -1){ + halfHints++; + solutionArray[solutionArray.indexOf(guessArray[i])] = 1; } - }) - - guessArray.forEach(function(guessLetter, index1){ - solutionArray.forEach(function(solutionLetter, index2){ - if(guessLetter == solutionLetter){ - hints2++; - guessArray[index1] = 0; - solutionArray[index2] = 1; - } - }) - }) - return hints + "-" + hints2 + } + + return fullHints + '-' + halfHints; } const mastermind = (guess) => { @@ -66,14 +58,15 @@ const mastermind = (guess) => { } guess = guess.toLowerCase().trim(); - + if(guess == solution){ - board = []; - console.log("You guessed it!") + board = []; + console.log("You guessed it!"); return "You guessed it!"; }else if(board.length === 10){ console.log("You lost! The solution was " + solution); board = []; + solution = ''; generateSolution(); }else{ board.push(guess)