diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..14d8893a5 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -7,39 +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: [] }; -function printStacks() { +// 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; +} + +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() { - // Your code here - +// 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() { - // Your code here - +// 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 - +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: + 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) ) { + 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) => { @@ -80,7 +144,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);