From ccf6dc5a414f9092dd2c8636bf46b2a3b251e158 Mon Sep 17 00:00:00 2001 From: Aaron Gonzalez Date: Sat, 27 Apr 2019 15:28:29 -0500 Subject: [PATCH 1/2] towers of hanoi --- 03week/towersOfHanoi.js | 109 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 103 insertions(+), 6 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..204a6bacd 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -19,24 +19,64 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { - // Your code here +function movePiece(startStack, endStack) { +// Your code here +stacks[endStack].push(stacks[startStack].pop()); +//take last item from an array and place it at the end of a different array +//is the piece we're moving the last item in its array +//check for win at the end of move } +function isValid(startStack,endStack) { + let newStart = startStack.toLowerCase().trim(); + let newEnd = endStack.toLowerCase().trim(); + if(newStart == "a" || newStart == 'b' || newStart == 'c' && newEnd == 'a' || newEnd == 'b' || newEnd == 'c') { + return true; + } else { + return false; + } +} -function isLegal() { +function isLegal(startStack, endStack) { + //is the piece we're moving smaller than the last item in the array we're moving to + //is the piece we're moving the last item in its array + //must take pieces from stack of pieces length > 0 + //start stack same as end stack is illegal // Your code here - + let startArray = stacks[startStack]; + let endArray = stacks[endStack]; +if (startStack != endStack && startArray.length > 0) { + if (endArray.length === 0 || endArray[endArray.length-1]>startArray[startArray.length-1]) { + return true + } else return false +} else { + console.log('Invalid move') + return false + } } function checkForWin() { // Your code here - + //length of tower b or c is equal to 4 +if(stacks.b.toString() == '4,3,2,1' || stacks.c.toString() == '4,3,2,1'){ + console.log('You Win!'); + return true; +} else return false; } function towersOfHanoi(startStack, endStack) { // Your code here - + //check to see if move is legal + //return either true or false + //if yes, execute move, if not, 'try again' + //check for win + if (isValid(startStack, endStack)) { + if (isLegal(startStack, endStack)) { + movePiece(startStack, endStack); + checkForWin() + } else return false; + } else { + console.log('Invalid Input');} } function getPrompt() { @@ -57,6 +97,48 @@ if (typeof describe === 'function') { it('should be able to move a block', () => { towersOfHanoi('a', 'b'); assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); + towersOfHanoi('a', 'c'); + assert.deepEqual(stacks, { a: [4, 3], b: [1], c: [2] }); + towersOfHanoi('b', 'c'); + assert.deepEqual(stacks, { a: [4, 3], b: [], c: [2,1] }); + }); + it('should not be ableto move a block', () => { + towersOfHanoi('b', 'a'); + assert.deepEqual(stacks, { a: [4, 3], b: [], c: [2,1] }); + towersOfHanoi('a', 'c'); + assert.deepEqual(stacks, { a: [4, 3], b: [], c: [2,1] }); + towersOfHanoi('b', 'c'); + assert.deepEqual(stacks, { a: [4, 3], b: [], c: [2,1] }); + }); + }); + describe('#movePiece()', () => { + it( 'should move the piece', () => { + stacks = { + a: [4,3,2], + b: [1], + c: [] + }; + assert.equal(isLegal('a', 'c'), true); + stacks = { + a: [4,3], + b: [1], + c: [2] + }; + assert.equal(isLegal('b', 'c'), true); + }); + it( 'should not move the piece', () => { + stacks = { + a: [4,3], + b: [1], + c: [2] + }; + assert.equal(isLegal('a', 'c'), false); + stacks = { + a: [4,3], + b: [], + c: [2,1] + }; + assert.equal(isLegal('b', 'c'), false); }); }); @@ -68,6 +150,12 @@ if (typeof describe === 'function') { c: [] }; assert.equal(isLegal('a', 'b'), false); + stacks = { + a: [4,3], + b: [2], + c: [1] + }; + assert.equal(isLegal('a', 'c'), false); }); it('should allow a legal move', () => { stacks = { @@ -76,14 +164,23 @@ if (typeof describe === 'function') { c: [] }; assert.equal(isLegal('a', 'c'), true); + stacks = { + a: [4,3], + b: [], + c: [2,1] + }; }); }); describe('#checkForWin()', () => { it('should detect a win', () => { stacks = { a: [], b: [4, 3, 2, 1], c: [] }; assert.equal(checkForWin(), true); + stacks = {a: [], b: [], c: [4,3,2,1] }; //added + assert.equal(checkForWin(), true); stacks = { a: [1], b: [4, 3, 2], c: [] }; assert.equal(checkForWin(), false); + stacks = { a: [4,3,2,1], b: [], c: [] }; //added + assert.equal(checkForWin(), false); }); }); From 42fe0fd3479fa3b090e79fc520a731be922e1fd1 Mon Sep 17 00:00:00 2001 From: Aaron Gonzalez Date: Sat, 27 Apr 2019 15:33:53 -0500 Subject: [PATCH 2/2] try this again --- 03week/towersOfHanoi.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 204a6bacd..decbda367 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -189,3 +189,6 @@ if (typeof describe === 'function') { getPrompt(); } + + +