From df762c0628b14c4a0e7749b9e396a01ac2ee61fc Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Wed, 23 Jan 2019 19:32:42 -0600 Subject: [PATCH 1/4] built towersOfHanoi. passes all tests --- 03week/towersOfHanoi.js | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..25bccb714 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -13,6 +13,14 @@ let stacks = { c: [] }; +let reset = () => { + stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; +} + function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); @@ -21,22 +29,36 @@ function printStacks() { function movePiece() { // Your code here - } -function isLegal() { - // Your code here - +function isLegal(startStack, endStack) { + let lastStart = stacks[startStack].length - 1; + let lastEnd = stacks[endStack].length - 1; + if( (stacks[startStack][lastStart] < stacks[endStack][lastEnd]) || stacks[endStack][lastEnd] == undefined ) { + return true; + } + else return false; } function checkForWin() { - // Your code here - + if( stacks.c.length == 4 ) { + return true; + } + else return false; } function towersOfHanoi(startStack, endStack) { // Your code here - + if( !isLegal(startStack, endStack) ) { + console.log('Try again with a valid move!'); + } + else { + stacks[endStack].push(stacks[startStack].pop()); + } + if( checkForWin() ) { + console.log('You win!'); + reset(); + } } function getPrompt() { @@ -80,7 +102,7 @@ if (typeof describe === 'function') { }); describe('#checkForWin()', () => { it('should detect a win', () => { - stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + stacks = { a: [], b: [], c: [4, 3, 2, 1] }; assert.equal(checkForWin(), true); stacks = { a: [1], b: [4, 3, 2], c: [] }; assert.equal(checkForWin(), false); From d129c483d466ad6818507bb29966b9cf1f62b98b Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Thu, 24 Jan 2019 03:14:29 -0600 Subject: [PATCH 2/4] added move count and best score. added comments. --- 03week/towersOfHanoi.js | 62 ++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 25bccb714..352a7f13e 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -7,61 +7,103 @@ const rl = readline.createInterface({ output: process.stdout }); +// the 'towers', as an object which contains three arrays: let stacks = { a: [4, 3, 2, 1], b: [], c: [] }; +// a couple variables to count number of moves per game as well as best score: +let moveCount = 0; +let bestCount = 0; + +// reset function to reset when a game is complete: let reset = () => { stacks = { a: [4, 3, 2, 1], b: [], c: [] }; + moveCount = 0; } -function printStacks() { +let printStacks = () => { console.log("a: " + stacks.a); console.log("b: " + stacks.b); console.log("c: " + stacks.c); } -function movePiece() { - // Your code here +// validity check function with a switch statement to explicitly define valid inputs: +let isValid = ( startStack, endStack ) => { + switch( startStack ) { + case 'a': + case 'b': + case 'c': + break; + default: + return false; + } + switch( endStack ) { + case 'a': + case 'b': + case 'c': + return true; + default: + return false; + } } -function isLegal(startStack, endStack) { +// function to check for legality of attempted move: +let isLegal = (startStack, endStack) => { + // define a couple variables to represent the final item in each selected stack for comparsion purposes: let lastStart = stacks[startStack].length - 1; let lastEnd = stacks[endStack].length - 1; + // if the final item in the chosen startStack is less than the final item in the chosen endStack, OR if the chosen endStack is empty (undefined), return true (move is valid): if( (stacks[startStack][lastStart] < stacks[endStack][lastEnd]) || stacks[endStack][lastEnd] == undefined ) { return true; } else return false; } -function checkForWin() { +// check for win: if the final tower is full, returns true: +let checkForWin = () => { if( stacks.c.length == 4 ) { return true; } else return false; } -function towersOfHanoi(startStack, endStack) { - // Your code here - if( !isLegal(startStack, endStack) ) { +let towersOfHanoi = (startStack, endStack) => { + // check if the inputs are valid: + if(!isValid(startStack, endStack)) { + console.log('Try again, choose either "a", "b", or "c"!'); + } + // then check if the move is legal: + else if( !isLegal(startStack, endStack) ) { console.log('Try again with a valid move!'); } + // if it's valid and legal, move the piece: else { + // pop the final element in the array defined by startStack, and push it to the array defined by endStack: stacks[endStack].push(stacks[startStack].pop()); + // add one to the number of moves which have occured: + moveCount++ } + // see if checkForWin() evaluates as true, and if so log "You win!" and reset the board: if( checkForWin() ) { - console.log('You win!'); + // if game is won in less moves than the best score (or if it's the first score), update best score: + if( (moveCount < bestCount) || (bestCount == 0) ) { + bestCount = moveCount; + } + // console.log the win, display how many moves it took to win and the player's best score: + console.log(`You won in ${moveCount} moves! Your best was ${bestCount} moves!`); + // reset the board: reset(); } } -function getPrompt() { +let getPrompt = () => { printStacks(); rl.question('start stack: ', (startStack) => { rl.question('end stack: ', (endStack) => { From 4b9198b8e1aaabf33cb802a582e058345fd92345 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Thu, 24 Jan 2019 19:54:05 -0600 Subject: [PATCH 3/4] did something --- 03week/towersOfHanoi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 352a7f13e..736403b27 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -86,7 +86,7 @@ let towersOfHanoi = (startStack, endStack) => { // if it's valid and legal, move the piece: else { // pop the final element in the array defined by startStack, and push it to the array defined by endStack: - stacks[endStack].push(stacks[startStack].pop()); + stacks[endStack].push( stacks[startStack].pop() ); // add one to the number of moves which have occured: moveCount++ } From bf96c0d6987dcb277995c70dfb5349a4b3485256 Mon Sep 17 00:00:00 2001 From: Aaron Altounian Date: Thu, 24 Jan 2019 23:46:43 -0600 Subject: [PATCH 4/4] changed a comment etc --- 03week/towersOfHanoi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 736403b27..14d8893a5 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -90,7 +90,7 @@ let towersOfHanoi = (startStack, endStack) => { // add one to the number of moves which have occured: moveCount++ } - // see if checkForWin() evaluates as true, and if so log "You win!" and reset the board: + // see if checkForWin() evaluates as true: if( checkForWin() ) { // if game is won in less moves than the best score (or if it's the first score), update best score: if( (moveCount < bestCount) || (bestCount == 0) ) {