From 52a5b2171594fb953a0348ded666194c4e53bf20 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sat, 29 Jul 2017 13:24:27 -0500 Subject: [PATCH 1/8] Mastermind and In Class Project Initial Push --- 03week/inClassProject.js | 74 ++++++++++++++++++++++++++++++++++++++ 03week/mastermind.js | 77 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 03week/inClassProject.js diff --git a/03week/inClassProject.js b/03week/inClassProject.js new file mode 100644 index 000000000..b6fd9df4e --- /dev/null +++ b/03week/inClassProject.js @@ -0,0 +1,74 @@ +'use strict'; + +console.log('Project 2 - Loops'); +console.log(''); +console.log(''); + + +// Test 1 - For loop +console.log('Test 1 - for loop'); +const carsInReverse = ['Ford','Chevy','Buick','Chrysler','Nissan', 'Toyota']; +for (let i = 0; i< carsInReverse.length; i++) { + console.log(carsInReverse[i]); +} +console.log(''); + +// Test 2 - for... in loop. +console.log('Test 2 - For ... in loop'); +const persons = { + firstName:'Jane', + lastName:'Doe', + birthDate:'Jan 5, 1925', + gender:'female' +}; + +for (const key in persons) { + console.log(key); + // console.log(`${key}: ${persons[key]}`); +} + +for (let key in persons) { + if (key === 'birthDate') { + console.log(`She was born on ${persons[key]}`); + } +} +console.log(''); + +// Test 3 - while loop +console.log('Test 3 - using while loops'); +let i = 1; +let numList = '' +while (i <= 1000) { + numList += i + ', '; + i++; +} +console.log(numList); +console.log(''); + +// Test 4 - Using do..while loop +console.log('Test 4 - using do..while loop'); +i = 1; +numList = '' +do { + numList += i + ', '; + i++; +} while (i <= 1000); +console.log(numList); +console.log(''); + +// Test 5 - For vs while +console.log('Test 5 - For vs while loops'); +console.log('When is a for loop better than a while?\nWell... when you are looping over a known number of items, like in an array'); +console.log('How is the readability affected?\nFor loops define start value, condition to check, and iteration\nWhile loops only define the condition to check'); +console.log(''); + +// Test 6 - for vs for..in loop +console.log('Test 6 - For vs for..in loop'); +console.log('What is the difference between a for loop and a for..in loop\nThe for..in loop is a special loop for objects. One can loop on the keys of the object'); +console.log(''); + + +// Test 7 - While vs do..while +console.log('Test 7 - While vs do..while loop'); +console.log('What is the difference between a while loop and do..while loop\nThe while loop checks the condition at the beginning of the loop.\nThe do..while loop, goes through the loop at least once, and checks the condition at the end.'); +console.log(''); diff --git a/03week/mastermind.js b/03week/mastermind.js index 60e5cfa18..5cd3842ad 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -1,4 +1,52 @@ 'use strict'; +/******************************** WHITE BOARD NOTES **************************************** +// Mastermind... There is a set of items (letters, numbers, colors, shapes, etc.), the Gamemaster selects items from this set +// and places them in a certain order. The goal is for you to guess the right items in the right order. +// The Gamemaster (the one that knows the right combination), gives you 2 hints after each guess. +// a) right item, right place +// b) right item, wrong place. +// if it is a wrong item (it is also by definition in the wrong place), so it is not counted. +// You can use these hints to improve on your guess. +// You continue guessing and receiving hints until you get the right combination. +// +// For our computer game, the items are letters... a-h. +// The Computer is Gamemaster. It will randomly select 4 letters in a certain combination. This is the winning combination +// I will make a guess. Need to test to make sure player only select letters a-h. Need to test that player only pick 4. +// Computer will test for win. If the player won, announce winner. End game. +// If player didn't win, Computer will tell player how many are "right item right order" - "right item wrong order". It will look like 1-3, 2-1, etc. +// The player will guess again. +// +// ********************************** PSEUDO CODE ************************************* +// generateSolution() // Given. Computer selects winning combination (combo of 4 letters) +// if valid entry(guess) then // Test for a) letters a-h, b) guess.length = solution.length (just 4 letters) +// if guess = solution then +// Announce winner and number of guesses it took! +// End game +// else +// guessCount++ +// console.log(generateHint(myGuess)) +// Continue playing game +// end if +// else +// Tell user to make another selection +// end if + +// generateHint(myGuess) +// // Test for exact Match +// Loop on the characters of myGuess +// if myGuess[i] === solution[i] +// exactMatch++ +// end if +// end loop +// +// // Test for matching character. not in right position +// Loop on characters of myGuess +// if character exists in solution then +// matchChar++ +// end if +// end loop +// return exactMatch + matchChar +*****************************************************************************************/ const assert = require('assert'); const readline = require('readline'); @@ -30,11 +78,36 @@ function getRandomInt(min, max) { function generateHint() { // your code here + // if myGuess[i] = solution[i] then + // exactMatch++ + // else if (solutionArr.some(value => myGuess[i] === value)) + console.log('generating a hint for you'); +} + +// validEntry() tests the user's entry. Valid entries only contain letters a-h. Only 4 letters. +function validEntry(guess) { + // Make sure length is correct + console.log(solution); + console.log(guess); + // Testing for length and testing if all the letters in guess are valid. + return guess.length === 4; + // guess.length === solution.length && guess.split('').every(myChar => letters.some(validLetter => myChar === validLetter)); } function mastermind(guess) { - solution = 'abcd'; // Comment this out to generate a random solution - // your code here + //solution = 'abcd'; // Comment this out to generate a random solution + guess = guess.toLowerCase().trim(); // this is a "cleanup" statement will change all letters to lowercase and remove spaces. + + if (validEntry(guess)) { + if (guess === solution) { + console.log('Great job. You won'); + } else { + generateHint(); + } + } else { + console.log('Hey dude, you need to re-enter your guess. Use letters a-h, only 4 letters') + } + } From 7b542676d468a9efc763ec6f69b1897df1e36842 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sat, 29 Jul 2017 13:39:38 -0500 Subject: [PATCH 2/8] completed validEntry() function --- 03week/mastermind.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index 5cd3842ad..7fd352722 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -90,8 +90,7 @@ function validEntry(guess) { console.log(solution); console.log(guess); // Testing for length and testing if all the letters in guess are valid. - return guess.length === 4; - // guess.length === solution.length && guess.split('').every(myChar => letters.some(validLetter => myChar === validLetter)); + return guess.length === solution.length && guess.split('').every(myChar => letters.some(validLetter => myChar === validLetter)); } function mastermind(guess) { From fbebcc53373e435ede11e6fc08228ffefca3a1ef Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sat, 29 Jul 2017 15:12:04 -0500 Subject: [PATCH 3/8] Added generateHint() function --- 03week/mastermind.js | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index 7fd352722..611b51163 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -76,21 +76,32 @@ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { +function generateHint(myGuess) { // your code here - // if myGuess[i] = solution[i] then - // exactMatch++ - // else if (solutionArr.some(value => myGuess[i] === value)) - console.log('generating a hint for you'); + let exactMatch = 0; + let correctLetter = 0; + let solutionArr = solution.split(''); + for (let i=0; i myGuess[i] === solutionLetter)) { + correctLetter++; + } + } + } + return `${exactMatch}-${correctLetter}`; } // validEntry() tests the user's entry. Valid entries only contain letters a-h. Only 4 letters. -function validEntry(guess) { - // Make sure length is correct - console.log(solution); - console.log(guess); +function validEntry(myGuess) { // Testing for length and testing if all the letters in guess are valid. - return guess.length === solution.length && guess.split('').every(myChar => letters.some(validLetter => myChar === validLetter)); + return myGuess.length === solution.length && myGuess.split('').every(myChar => letters.some(validLetter => myChar === validLetter)); } function mastermind(guess) { @@ -101,7 +112,7 @@ function mastermind(guess) { if (guess === solution) { console.log('Great job. You won'); } else { - generateHint(); + console.log(`Hint (Exact Match-Correct Letter): ${generateHint(guess)}`); } } else { console.log('Hey dude, you need to re-enter your guess. Use letters a-h, only 4 letters') From 91d7df70846f408ecf06944fd8bb61d56992b685 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sat, 29 Jul 2017 18:04:35 -0500 Subject: [PATCH 4/8] Added guesses and hints to board array --- 03week/mastermind.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index 611b51163..df1e15c36 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -81,17 +81,25 @@ function generateHint(myGuess) { let exactMatch = 0; let correctLetter = 0; let solutionArr = solution.split(''); - for (let i=0; i myGuess[i] === solutionLetter)) { - correctLetter++; + // This for loop now looks for a correct letter, but not in the right position. + for (let i=0; i Date: Sat, 29 Jul 2017 19:32:45 -0500 Subject: [PATCH 5/8] fixed bug in generateHint(), improved board array display, added control for user to quite game or restart game. --- 03week/mastermind.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index df1e15c36..5f71e39c2 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -99,6 +99,7 @@ function generateHint(myGuess) { if (myGuessArr[i] === solutionArr[j]) { correctLetter++; solutionArr[j] = ''; // clear out value. This ensures we don't count duplicates. + break; // just in case there are duplicate values in the solution, once it finds a letter match, break out. } } } @@ -113,28 +114,44 @@ function validEntry(myGuess) { } function mastermind(guess) { - solution = 'abca'; // Comment this out to generate a random solution + // solution = 'caa'; // Comment this out to generate a random solution guess = guess.toLowerCase().trim(); // this is a "cleanup" statement will change all letters to lowercase and remove spaces. if (validEntry(guess)) { if (guess === solution) { - console.log('Great job. You won'); + board[board.length] = `${guess} : ${generateHint(guess)}`; + return true; // player won! } else { - board[board.length] = `${guess}: ${generateHint(guess)}`; - // console.log(`Hint (Exact Match-Correct Letter): ${generateHint(guess)}`); + board[board.length] = `${guess} : ${generateHint(guess)}`; } } else { - console.log('Hey dude, you need to re-enter your guess. Use letters a-h, only 4 letters') + console.log('Hey dude, you need to re-enter your guess. Use letters a-h. Also, only 4 letters') } - + return false; // game is still going on. } function getPrompt() { - rl.question('guess: ', (guess) => { - mastermind(guess); - printBoard(); - getPrompt(); + rl.question('guess or (Q to quit): ', (guess) => { + // This statement gives the user some control to quit the game instead of CTRL-C. + if (guess.toLowerCase().trim() === 'q') { + console.log('Quiting...'); + process.exit(0); + } + + if (mastermind(guess)) { + printBoard(); + console.log(`Great job. You won!\nYou cracked the code in ${board.length} moves.`); + console.log('Starting new game...'); + // initialize variables... + while (board.length > 0) {board.pop();} // clears board. + solution = ''; // set solution to NULL before generating new combination. Otherwise, it will append to the old. + generateSolution(); + getPrompt(); + } else { + printBoard(); + getPrompt(); + } }); } From 399dfc1581af5e691800cce0267c7a0b03578bab Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sat, 29 Jul 2017 19:37:17 -0500 Subject: [PATCH 6/8] fixed bug in generateHint(), improved board array display, added control for user to quit or restart game --- 03week/mastermind.js | 1 - 1 file changed, 1 deletion(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index 5f71e39c2..c032dc841 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -130,7 +130,6 @@ function mastermind(guess) { return false; // game is still going on. } - function getPrompt() { rl.question('guess or (Q to quit): ', (guess) => { // This statement gives the user some control to quit the game instead of CTRL-C. From 78ca04f15a67fc3f6b4bbd877005e845ed99a225 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Sun, 30 Jul 2017 14:55:18 -0500 Subject: [PATCH 7/8] Used quicker method to clear board. Improved generateSolution. --- 03week/mastermind.js | 53 +++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index c032dc841..f5af8af7d 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -77,33 +77,36 @@ function getRandomInt(min, max) { } function generateHint(myGuess) { - // your code here + + // Initializing... let exactMatch = 0; let correctLetter = 0; + let matchIndex = -1; + // creating arrays out of the strings. let solutionArr = solution.split(''); let myGuessArr = myGuess.split(''); - // This for loop tests for exact match - for (let i=0; i { + if (letter === solutionArr[index]) { + exactMatch++; + // clear out value. This sets us up to look for correct letter test later. These won't be considered. + solutionArr[index] = ''; + myGuessArr[index] = ''; } - // console.log(myGuessArr[i], solutionArr[i]); // This is our debug to show us the solution while testing. - } - // This for loop now looks for a correct letter, but not in the right position. - for (let i=0; i { + if (letter) { // letter could be null from previous forEach statement. + matchIndex = solutionArr.indexOf(letter); // Let's find a matching letter in the solution! + if (matchIndex !== -1) { // We found a letter match in the string. + correctLetter++ + solutionArr[matchIndex] = ''; // clear out value so value won't be considered with next letter tests. } - } - } + } // closing brace for if (letter) + }); + return `${exactMatch}-${correctLetter}`; } @@ -114,7 +117,7 @@ function validEntry(myGuess) { } function mastermind(guess) { - // solution = 'caa'; // Comment this out to generate a random solution + // solution = 'abca'; // Comment this out to generate a random solution guess = guess.toLowerCase().trim(); // this is a "cleanup" statement will change all letters to lowercase and remove spaces. if (validEntry(guess)) { @@ -143,13 +146,13 @@ function getPrompt() { console.log(`Great job. You won!\nYou cracked the code in ${board.length} moves.`); console.log('Starting new game...'); // initialize variables... - while (board.length > 0) {board.pop();} // clears board. + board.length = 0; // clears board. solution = ''; // set solution to NULL before generating new combination. Otherwise, it will append to the old. generateSolution(); getPrompt(); } else { - printBoard(); - getPrompt(); + printBoard(); + getPrompt(); } }); } From 6d7277463ab94d2449e57503ea2943be2093fb69 Mon Sep 17 00:00:00 2001 From: Craig Copeland Date: Wed, 2 Aug 2017 20:13:32 -0500 Subject: [PATCH 8/8] Improved GetPrompt(), added Guess Limit. --- 03week/mastermind.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/03week/mastermind.js b/03week/mastermind.js index f5af8af7d..3fc1f7ba8 100644 --- a/03week/mastermind.js +++ b/03week/mastermind.js @@ -58,6 +58,7 @@ const rl = readline.createInterface({ let board = []; let solution = ''; let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +const GUESSLIMIT = 20; function printBoard() { for (let i = 0; i < board.length; i++) { @@ -116,19 +117,18 @@ function validEntry(myGuess) { return myGuess.length === solution.length && myGuess.split('').every(myChar => letters.some(validLetter => myChar === validLetter)); } +// This i function mastermind(guess) { // solution = 'abca'; // Comment this out to generate a random solution guess = guess.toLowerCase().trim(); // this is a "cleanup" statement will change all letters to lowercase and remove spaces. - if (validEntry(guess)) { + if (validEntry(guess)) { // Testing to make sure + board[board.length] = `${guess} : ${generateHint(guess)}`; if (guess === solution) { - board[board.length] = `${guess} : ${generateHint(guess)}`; return true; // player won! - } else { - board[board.length] = `${guess} : ${generateHint(guess)}`; } } else { - console.log('Hey dude, you need to re-enter your guess. Use letters a-h. Also, only 4 letters') + console.log('Hey there, you need to re-enter your guess. Use letters a-h. Also, only 4 letters') } return false; // game is still going on. } @@ -150,6 +150,15 @@ function getPrompt() { solution = ''; // set solution to NULL before generating new combination. Otherwise, it will append to the old. generateSolution(); getPrompt(); + } else if (board.length === GUESSLIMIT) { + printBoard(); + console.log(`Sorry, you did not guess within ${GUESSLIMIT} moves. Answer: ${solution}.`); + console.log('Starting new game...'); + // initialize variables... + board.length = 0; // clears board. + solution = ''; // set solution to NULL before generating new combination. Otherwise, it will append to the old. + generateSolution(); + getPrompt(); } else { printBoard(); getPrompt();