From e90023d7cb81c1ec2080dd770fd6fdda7a6b76d2 Mon Sep 17 00:00:00 2001 From: Jim Schaefer Date: Thu, 7 Mar 2019 20:48:28 -0600 Subject: [PATCH] commit towers of hanoi --- 03week/towersOfHanoi.js | 52 +++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..f7c9d09c3 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -19,24 +19,56 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { - // Your code here - +function isLegal(startArray, endArray) { + let startLength = startArray.length-1; + let endLength = endArray.length-1; + + if (endArray[endLength] == undefined){ + return true; + } else if (startArray[startLength] < endArray[endLength]) { + return true; + } else { + return false; + } + } -function isLegal() { - // Your code here +function movePiece(startStack, endStack) { + + let startLength = stacks[startStack].length; + let endLength = stacks[endStack].length; + let piece = stacks[startStack][startLength-1]; + + stacks[startStack].pop(piece) + stacks[endStack].push(piece) } -function checkForWin() { - // Your code here +function resetGame() { +stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; + +} +function checkForWin() { + if (stacks.c[0] == 4 && stacks.c[1] == 3 && stacks.c[2] == 2 && stacks.c[3] == 1){ + console.log("winner"); + resetGame(); + } } function towersOfHanoi(startStack, endStack) { - // Your code here + //Legality check + let legal = isLegal(stacks[startStack], stacks[endStack]) + if(legal){ + movePiece(startStack, endStack) + } + + checkForWin(); } function getPrompt() { @@ -80,7 +112,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); @@ -91,4 +123,4 @@ if (typeof describe === 'function') { getPrompt(); -} +} \ No newline at end of file