From fa1227af0a07d3e5376f91a6bec36ee5518c4688 Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Wed, 25 Oct 2017 20:52:21 -0500 Subject: [PATCH 1/2] Towers of Hanoi --- 03week/towersOfHanoi.js | 83 ++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 40b9ebe92..433af2bde 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -13,42 +13,91 @@ let stacks = { c: [] }; +/* +*This function displays the "board" to the user +*/ function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); console.log("c: " + stacks.c); } -function movePiece() { - // Your code here - +/* +*This function removes a block from a stack and adds that block to a different stack +* +* @param startStack the stack we are removing the element from +* @param endStack the stack we are adding the element to +*/ +function movePiece(startStack, endStack) { + let blockMove = stacks[startStack].pop(); //remove the "top block" from startStack + stacks[endStack].push(blockMove); //add the removed block to endStack } -function isLegal() { - // Your code here - +/* +*This function checks to see if the proposed move is legal +* +* @param startStack the stack we are removing the element from +* @param endStack the stack we are adding the element to +* @return true if the move is legal +*/ +function isLegal(startStack, endStack) { + if (((startStack==='a')||(startStack==='b')||(startStack==='c'))&& + ((endStack==='a')||(endStack==='b')||(endStack==='c'))) { //checking to make sure input is valid + let begin = stacks[startStack]; + let final = stacks[endStack]; + //console.log("Length of startStack: " + begin.length); + //console.log("Value of block being moved: " + begin[begin.length - 1]); + //console.log("Value of block being topped off: " + final[final.length - 1]); + if (begin[begin.length - 1] > final[final.length - 1]) { //making sure the "block" is being move onto a larger "block" + console.log("***ILLEGAL MOVE***"); + } else{ + return true; + } + } else{ + console.log("***Invalid input.***") + } } +/* +*This function checks for a win +*/ function checkForWin() { - // Your code here - + if (stacks.c[3] === 1){ //checks for the "1" block on top of a four-stack in stack "c." + console.log("**************"); + console.log("***YOU WIN!***"); + console.log("**************"); + stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; + } } +/* +*This function runs the game +* +* @param startStack the stack we are removing the element from +* @param endStack the stack we are adding the element to +*/ function towersOfHanoi(startStack, endStack) { - // Your code here - + if (isLegal(startStack, endStack)) { //if move is legal, move piece then check for win + movePiece(startStack, endStack); + checkForWin(); + } } +/* +*This function prompts the user for inputs then runs towersOfHanoi +*/ function getPrompt() { printStacks(); - rl.question('start stack: ', (startStack) => { - rl.question('end stack: ', (endStack) => { - towersOfHanoi(startStack, endStack); - getPrompt(); + rl.question('start stack: ', (startStack) => { //get user input to define what stack the "block" is being taken from + rl.question('end stack: ', (endStack) => { //get user to define what stack the "block" is being placed on + towersOfHanoi(startStack, endStack); //run game logic + getPrompt(); //recursion to prompt next move }); }); } -getPrompt(); - - +getPrompt(); //start game on run From 7a1bca38b4e8dd205fc635ef2d248ca8cdfd0723 Mon Sep 17 00:00:00 2001 From: Patrick Creamer Date: Mon, 30 Oct 2017 18:26:57 -0500 Subject: [PATCH 2/2] Mastermind done --- 04week/mastermind.js | 88 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 6 deletions(-) diff --git a/04week/mastermind.js b/04week/mastermind.js index 60e5cfa18..7e338f5fb 100644 --- a/04week/mastermind.js +++ b/04week/mastermind.js @@ -2,6 +2,7 @@ const assert = require('assert'); const readline = require('readline'); +const colors = require('colors'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -11,33 +12,106 @@ let board = []; let solution = ''; let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +/* +*This function prints the board: the hints and their corresponding guesses +*/ function printBoard() { for (let i = 0; i < board.length; i++) { console.log(board[i]); } } +/* +*This function generates a random 4-letter solution using the first 8 letters of the alphabet (duplicates allowed) +*/ function generateSolution() { for (let i = 0; i < 4; i++) { const randomIndex = getRandomInt(0, letters.length); solution += letters[randomIndex]; +// console.log('Solution is: ' + solution); } } +/* +*This function generates a random integer from which to correspond to a random letter in generateSolution +* +* @param min minimum value allowed +* @param max maximum value allowed +*/ function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; } -function generateHint() { - // your code here +/* +*This function gives a hint based on the inputted guess; for each letter that exactly matches between the guess and the solution, redPeg will increment by one. For each letter that matches, except for location, whitePeg will increment by one. +* +* @param guess The guess entered by the user +*/ +function generateHint(guess) { + let redPeg = 0; + let whitePeg = 0; + let guessArray = guess.split(""); + let solutionArray = solution.split(""); + for (let i=0; i < 4; i++) { + let x = guessArray[i]; + let y = solutionArray[i]; + if (guessArray[i]===solutionArray[i]){ + //for each guess letter that matches each solution letter, tick up one red, then set index of array to null to avoid false positives when tallying whitePeg + redPeg++; + solutionArray[i] = null; + } + } + for (let i=0; i < 4; i++) { + //for index of each guess letter that matches a letter (but not idex of) solution letter, tick up one white + if ((guessArray[i]!==solutionArray[i])&&(solutionArray.indexOf(guessArray[i])!==(-1))) { + whitePeg++; + solutionArray[solutionArray.indexOf(guessArray[i])] = null; + } + } + let redPegStr = redPeg.toString(); + let whitePegStr = whitePeg.toString(); + let hint = ((redPegStr.red)+"-"+(whitePegStr.white)); + //console.log("Hint: "+hint); + board.push(hint+" --- "+guess); + return hint; +} + +/* +*This function checks the guess against the solution +* +* @param guess The user inputted guess +*/ +function checkForWin(guess) { + let win = "You guessed it!"; + let reguess = "Guess again."; + let lose = "You ran out of turns! The solution was "+solution; + if (solution===guess){ + console.log(win); + return win; + }else if ((solution!=guess)&&(board.length < 10)){ + console.log(reguess); + return reguess; + }else if ((solution!=guess)&&(board.length = 10)) { + console.log(lose); + return lose; + } } +/* +*This function runs the mastermind game +* +* @param guess The user inputted guess +*/ function mastermind(guess) { solution = 'abcd'; // Comment this out to generate a random solution - // your code here + generateHint(guess); + //console.log(solution); + checkForWin(guess); } - +/* +*This function prompts the user for an inputted guess +*/ function getPrompt() { rl.question('guess: ', (guess) => { mastermind(guess); @@ -62,10 +136,12 @@ if (typeof describe === 'function') { describe('#generateHint()', () => { it('should generate hints', () => { - assert.equal(generateHint('abdc'), '2-2'); + let expected = ('2'.red)+"-"+('2'.white); + assert.equal(generateHint('abdc'), expected); }); it('should generate hints if solution has duplicates', () => { - assert.equal(generateHint('aabb'), '1-1'); + let expected = ('1'.red)+"-"+('1'.white); + assert.equal(generateHint('aabb'), expected); }); });