From d275a62522c1248e4a57ed773fc4b8b5558c83f5 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Mon, 29 Oct 2018 23:10:49 -0500 Subject: [PATCH 1/7] move piece function written --- 03week/towersOfHanoi.js | 148 +++++++++++++++++++++++++++++++++------- 1 file changed, 122 insertions(+), 26 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..a81c98210 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,7 +1,14 @@ -'use strict'; +// player clicks block to place on peg +// if peg is empty, block will be placed +// if peg already has block(s) + // if block on peg is smaller than block in hand, return 'invalid move' + // if block on peg is larger than block in hand, block will be placed +// once all blocks are moved to last peg, return 'winner' -const assert = require('assert'); -const readline = require('readline'); +"use strict"; + +const assert = require("assert"); +const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout @@ -19,30 +26,117 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { - // Your code here - + // Your code starts here + +function movePiece(startStack, endStack) { + // if peg already has block(s) + // get the array of the starting stack + let startingStack = getStackValue(startStack); + // pop off the end value + const inHand = startingStack.pop(); + // place the popped off value on the end of the ending stack + const endingStack = getStackValue(endStack).push(inHand); + // grab the last item that was placed on the ending stack + const lastItem = endstack.pop() + // if it's smaller than the inHand value, it's an invalid move + if(lastItem < inHand) { + console.log('Invalid move. Try again') + // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg + } else { + endingStack + } + + +// switch (endStack) { +// case "b": +// if (startingStack === "a") { +// return (stacks = { +// a: startingStack, +// b: endingStack, +// c: stacks.c +// }); +// } else if (startingStack === "c") { +// return (stacks = { +// a: stacks.a, +// b: endingStack, +// c: startingStack +// }); +// } +// break; +// case "a": +// return stacks.b; +// break; +// case "c": +// return stacks.c; +// break; +// default: +// return "Invalid Move"; +// } +// } + +function getStackValue(stack) { + switch (stack) { + case "a": + return stacks.a; + break; + case "b": + return stacks.b; + break; + case "c": + return stacks.c; + break; + default: + return "Invalid Move"; + } } -function isLegal() { +function isLegal(startStack, endStack) { // Your code here - + // if peg is empty, block will be placed + let startArray = getStackValue(startStack); + let endArray = getStackValue(endStack); + + if (endArray.length === 0) { + return true; + } + // switch (endStack) { + // case 'a': + // end = stacks.a; + // break; + // case 'b': + // end = stacks.b; + // break; + // case 'c': + // end = stacks.c; + // break; + // default: + // return 'Invalid Move' + // } + // if block on peg is smaller than block in hand, return 'invalid move' + // if block on peg is larger than block in hand, block will be placed } function checkForWin() { // Your code here - + // once all blocks are moved to last peg, return 'winner' } function towersOfHanoi(startStack, endStack) { // Your code here + // player clicks block to place on peg + // call isLegal function + if (isLegal(startStack, endStack)) { + movePiece(startStack, endStack); + } + // call function move piece here + // call checkForWin function } function getPrompt() { printStacks(); - rl.question('start stack: ', (startStack) => { - rl.question('end stack: ', (endStack) => { + rl.question("start stack: ", startStack => { + rl.question("end stack: ", endStack => { towersOfHanoi(startStack, endStack); getPrompt(); }); @@ -51,44 +145,46 @@ function getPrompt() { // Tests -if (typeof describe === 'function') { +// it('should keep player from placing larger block on top of smaller block', () => { +// assert.equal(rockPaperScissors('rOcK', ' paper '), "Hand two wins!"); +// assert.equal(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!"); +// assert.equal(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!"); +// }); - describe('#towersOfHanoi()', () => { - it('should be able to move a block', () => { - towersOfHanoi('a', 'b'); +if (typeof describe === "function") { + describe("#towersOfHanoi()", () => { + it("should be able to move a block", () => { + towersOfHanoi("a", "b"); assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); }); }); - describe('#isLegal()', () => { - it('should not allow an illegal move', () => { + describe("#isLegal()", () => { + it("should not allow an illegal move", () => { stacks = { a: [4, 3, 2], b: [1], c: [] }; - assert.equal(isLegal('a', 'b'), false); + assert.equal(isLegal("a", "b"), false); }); - it('should allow a legal move', () => { + it("should allow a legal move", () => { stacks = { a: [4, 3, 2, 1], b: [], c: [] }; - assert.equal(isLegal('a', 'c'), true); + assert.equal(isLegal("a", "c"), true); }); }); - describe('#checkForWin()', () => { - it('should detect a win', () => { + describe("#checkForWin()", () => { + it("should detect a win", () => { stacks = { a: [], b: [4, 3, 2, 1], c: [] }; assert.equal(checkForWin(), true); stacks = { a: [1], b: [4, 3, 2], c: [] }; assert.equal(checkForWin(), false); }); }); - } else { - getPrompt(); - -} +} \ No newline at end of file From 84955acba00b2777e2f79a8c85f3f599fe169033 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Mon, 29 Oct 2018 23:17:20 -0500 Subject: [PATCH 2/7] isLegal and checkForWin functions written --- 03week/towersOfHanoi.js | 71 +++++++---------------------------------- 1 file changed, 11 insertions(+), 60 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index a81c98210..a0944ca39 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -36,43 +36,6 @@ function movePiece(startStack, endStack) { const inHand = startingStack.pop(); // place the popped off value on the end of the ending stack const endingStack = getStackValue(endStack).push(inHand); - // grab the last item that was placed on the ending stack - const lastItem = endstack.pop() - // if it's smaller than the inHand value, it's an invalid move - if(lastItem < inHand) { - console.log('Invalid move. Try again') - // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg - } else { - endingStack - } - - -// switch (endStack) { -// case "b": -// if (startingStack === "a") { -// return (stacks = { -// a: startingStack, -// b: endingStack, -// c: stacks.c -// }); -// } else if (startingStack === "c") { -// return (stacks = { -// a: stacks.a, -// b: endingStack, -// c: startingStack -// }); -// } -// break; -// case "a": -// return stacks.b; -// break; -// case "c": -// return stacks.c; -// break; -// default: -// return "Invalid Move"; -// } -// } function getStackValue(stack) { switch (stack) { @@ -91,34 +54,22 @@ function getStackValue(stack) { } function isLegal(startStack, endStack) { - // Your code here // if peg is empty, block will be placed - let startArray = getStackValue(startStack); - let endArray = getStackValue(endStack); - - if (endArray.length === 0) { - return true; + // start by grabbing the last item that was placed on the ending stack + const lastItem = endstack.pop() + // if it's smaller than the inHand value, it's an invalid move + if(lastItem < inHand) { + console.log('Invalid move. Try again') + // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg + } else { + endingStack } - // switch (endStack) { - // case 'a': - // end = stacks.a; - // break; - // case 'b': - // end = stacks.b; - // break; - // case 'c': - // end = stacks.c; - // break; - // default: - // return 'Invalid Move' - // } - // if block on peg is smaller than block in hand, return 'invalid move' - // if block on peg is larger than block in hand, block will be placed -} function checkForWin() { - // Your code here // once all blocks are moved to last peg, return 'winner' + if(stacks.c === [4, 3, 2, 1]){ + console.log('Winner!') + } } function towersOfHanoi(startStack, endStack) { From f9ff7bead63ee111af7c8adb7c127a3def7baac6 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Mon, 29 Oct 2018 23:38:52 -0500 Subject: [PATCH 3/7] towers of hanoi attempt number 1 --- 03week/towersOfHanoi.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index a0944ca39..ad5696726 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -36,6 +36,9 @@ function movePiece(startStack, endStack) { const inHand = startingStack.pop(); // place the popped off value on the end of the ending stack const endingStack = getStackValue(endStack).push(inHand); + if(inHand) { + endingstack + }; function getStackValue(stack) { switch (stack) { @@ -73,15 +76,13 @@ function checkForWin() { } function towersOfHanoi(startStack, endStack) { - // Your code here - - // player clicks block to place on peg - // call isLegal function +// check to see if move is legal if (isLegal(startStack, endStack)) { + // if move is legal, place piece on peg movePiece(startStack, endStack); + // once the piece has been moved, check for a win + checkForWin(); } - // call function move piece here - // call checkForWin function } function getPrompt() { From 4f80914894f2714dc32edb8a33b657261692e9b1 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Tue, 30 Oct 2018 22:28:58 -0500 Subject: [PATCH 4/7] movePiece function complete and working --- 03week/towersOfHanoi.js | 155 ++++++++++++++++++++++------------------ 1 file changed, 85 insertions(+), 70 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index ad5696726..3f6867782 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,8 +1,8 @@ // player clicks block to place on peg // if peg is empty, block will be placed // if peg already has block(s) - // if block on peg is smaller than block in hand, return 'invalid move' - // if block on peg is larger than block in hand, block will be placed +// if block on peg is smaller than block in hand, return 'invalid move' +// if block on peg is larger than block in hand, block will be placed // once all blocks are moved to last peg, return 'winner' "use strict"; @@ -26,19 +26,20 @@ function printStacks() { console.log("c: " + stacks.c); } - // Your code starts here +// Your code starts here function movePiece(startStack, endStack) { // if peg already has block(s) // get the array of the starting stack - let startingStack = getStackValue(startStack); + const startingStack = getStackValue(startStack); // pop off the end value const inHand = startingStack.pop(); // place the popped off value on the end of the ending stack - const endingStack = getStackValue(endStack).push(inHand); - if(inHand) { - endingstack - }; + const endingStack = getStackValue(endStack); + if (inHand) { + endingStack.push(inHand); + } +} function getStackValue(stack) { switch (stack) { @@ -57,32 +58,32 @@ function getStackValue(stack) { } function isLegal(startStack, endStack) { - // if peg is empty, block will be placed - // start by grabbing the last item that was placed on the ending stack - const lastItem = endstack.pop() + // start by targeting the last item that was placed on the ending stack + const lastItem = endstack.pop(); // if it's smaller than the inHand value, it's an invalid move - if(lastItem < inHand) { - console.log('Invalid move. Try again') - // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg + if (lastItem < inHand) { + console.log("Invalid move. Try again"); + // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg } else { - endingStack - } - -function checkForWin() { - // once all blocks are moved to last peg, return 'winner' - if(stacks.c === [4, 3, 2, 1]){ - console.log('Winner!') + endingStack.push(inHand); } } +// function checkForWin() { +// // once all blocks are moved to last peg, return 'winner' +// if (stacks.c === [4, 3, 2, 1]) { +// console.log("Winner!"); +// } +// } + function towersOfHanoi(startStack, endStack) { -// check to see if move is legal - if (isLegal(startStack, endStack)) { - // if move is legal, place piece on peg - movePiece(startStack, endStack); - // once the piece has been moved, check for a win - checkForWin(); - } + // check to see if move is legal + // if (isLegal(startStack, endStack)) { + // if move is legal, place piece on peg + movePiece(startStack, endStack); + // once the piece has been moved, check for a win + // checkForWin(); + // } } function getPrompt() { @@ -94,49 +95,63 @@ function getPrompt() { }); }); } +getPrompt(); // Tests -// it('should keep player from placing larger block on top of smaller block', () => { -// assert.equal(rockPaperScissors('rOcK', ' paper '), "Hand two wins!"); -// assert.equal(rockPaperScissors('Paper', 'SCISSORS'), "Hand two wins!"); -// assert.equal(rockPaperScissors('rock ', 'sCiSsOrs'), "Hand one wins!"); -// }); - -if (typeof describe === "function") { - describe("#towersOfHanoi()", () => { - it("should be able to move a block", () => { - towersOfHanoi("a", "b"); - assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); - }); - }); +// describe("#movePiece()", () => { +// it("should pick up the last piece player clicked on", () => { +// stacks = { +// a: [4, 3, 2], +// b: [], +// c: [] +// }; +// assert.equal(movePiece("a")); +// }); +// it("should place the last piece player clicked on", () => { +// stacks = { +// a: [4, 3, 2], +// b: [1], +// c: [] +// }; +// assert.equal(movePiece("a")); +// }); - describe("#isLegal()", () => { - it("should not allow an illegal move", () => { - stacks = { - a: [4, 3, 2], - b: [1], - c: [] - }; - assert.equal(isLegal("a", "b"), false); - }); - it("should allow a legal move", () => { - stacks = { - a: [4, 3, 2, 1], - b: [], - c: [] - }; - assert.equal(isLegal("a", "c"), true); - }); - }); - describe("#checkForWin()", () => { - it("should detect a win", () => { - stacks = { a: [], b: [4, 3, 2, 1], c: [] }; - assert.equal(checkForWin(), true); - stacks = { a: [1], b: [4, 3, 2], c: [] }; - assert.equal(checkForWin(), false); - }); - }); -} else { - getPrompt(); -} \ No newline at end of file +// if (typeof describe === "function") { +// describe("#towersOfHanoi()", () => { +// it("should be able to move a block", () => { +// towersOfHanoi("a", "b"); +// assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); +// }); +// }); + +// describe("#isLegal()", () => { +// it("should not allow an illegal move", () => { +// stacks = { +// a: [4, 3, 2], +// b: [1], +// c: [] +// }; +// assert.equal(isLegal("a", "b"), false); +// }); +// it("should allow a legal move", () => { +// stacks = { +// a: [4, 3, 2, 1], +// b: [], +// c: [] +// }; +// assert.equal(isLegal("a", "c"), true); +// }); +// }); + +// describe("#checkForWin()", () => { +// it("should detect a win", () => { +// stacks = { a: [], b: [4, 3, 2, 1], c: [] }; +// assert.equal(checkForWin(), true); +// stacks = { a: [1], b: [4, 3, 2], c: [] }; +// assert.equal(checkForWin(), false); +// }); +// }); +// } else { +// getPrompt() +// } From ee8543ea2ebe4e3e216468409cd95ee7586bae07 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Wed, 31 Oct 2018 20:39:29 -0500 Subject: [PATCH 5/7] attempted to fix legalMove function and failed --- 03week/towersOfHanoi.js | 48 ++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3f6867782..38b1653b8 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,6 +1,6 @@ // player clicks block to place on peg // if peg is empty, block will be placed -// if peg already has block(s) +// if peg already has block(s): // if block on peg is smaller than block in hand, return 'invalid move' // if block on peg is larger than block in hand, block will be placed // once all blocks are moved to last peg, return 'winner' @@ -58,32 +58,46 @@ function getStackValue(stack) { } function isLegal(startStack, endStack) { - // start by targeting the last item that was placed on the ending stack - const lastItem = endstack.pop(); + // if peg is not empty, target the last item that was placed on the ending stack + const lastItem = endStack.pop; + const startingStack = getStackValue(startStack); + const inHand = startingStack.pop(); // if it's smaller than the inHand value, it's an invalid move if (lastItem < inHand) { console.log("Invalid move. Try again"); // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg - } else { - endingStack.push(inHand); } } -// function checkForWin() { -// // once all blocks are moved to last peg, return 'winner' -// if (stacks.c === [4, 3, 2, 1]) { -// console.log("Winner!"); -// } -// } +function checkForWin() { + // once all blocks are moved to last peg, return 'winner' + if (stacks.c == [4, 3, 2, 1]) { + return true; + } +} + +function reset() { + if (checkForWin) { + let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; + } +} function towersOfHanoi(startStack, endStack) { // check to see if move is legal - // if (isLegal(startStack, endStack)) { - // if move is legal, place piece on peg - movePiece(startStack, endStack); - // once the piece has been moved, check for a win - // checkForWin(); - // } + if (isLegal(startStack, endStack)) { + // if move is legal, place piece on peg + movePiece(startStack, endStack); + // once the piece has been moved, check for a win + if (checkForWin()) { + console.log("Winner!"); + } + } else { + console.log("Not a legal move. Try again."); + } } function getPrompt() { From bc82dddeeafc0a8da4ba5f732cf2820cdf25a909 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 18:42:46 -0500 Subject: [PATCH 6/7] corrected movePiece and isLegal functions --- 03week/towersOfHanoi.js | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 38b1653b8..2c3be41b3 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -31,40 +31,28 @@ function printStacks() { function movePiece(startStack, endStack) { // if peg already has block(s) // get the array of the starting stack - const startingStack = getStackValue(startStack); + const startingStack = stacks[startStack]; // pop off the end value const inHand = startingStack.pop(); // place the popped off value on the end of the ending stack - const endingStack = getStackValue(endStack); + const endingStack = stacks[endStack]; if (inHand) { endingStack.push(inHand); } } -function getStackValue(stack) { - switch (stack) { - case "a": - return stacks.a; - break; - case "b": - return stacks.b; - break; - case "c": - return stacks.c; - break; - default: - return "Invalid Move"; - } -} - function isLegal(startStack, endStack) { // if peg is not empty, target the last item that was placed on the ending stack - const lastItem = endStack.pop; - const startingStack = getStackValue(startStack); - const inHand = startingStack.pop(); - // if it's smaller than the inHand value, it's an invalid move - if (lastItem < inHand) { + const endingStack = stacks[endStack]; + const lastItem = endingStack[endingStack.length - 1]; + const startingStack = stacks[startStack]; + const inHand = startingStack[startingStack.length - 1]; + if (endingStack.length === 0 || inHand < lastItem) { + return true; + } else { + // if it's smaller than the inHand value, it's an invalid move console.log("Invalid move. Try again"); + return false; // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg } } From 6065778738533e0474066d7d0bb3837660006672 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 3 Nov 2018 18:56:22 -0500 Subject: [PATCH 7/7] everything works yay --- 03week/towersOfHanoi.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 2c3be41b3..e44501fb9 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -42,31 +42,34 @@ function movePiece(startStack, endStack) { } function isLegal(startStack, endStack) { - // if peg is not empty, target the last item that was placed on the ending stack + // targeting the array that represents endingStack const endingStack = stacks[endStack]; + // targeting the last item that was placed on the ending stack const lastItem = endingStack[endingStack.length - 1]; + // targeting the array that represents startingStack const startingStack = stacks[startStack]; + // targeting the last item of startingStack const inHand = startingStack[startingStack.length - 1]; + // if the peg is empty or the value of inHand is less than the value of lastItem, return true if (endingStack.length === 0 || inHand < lastItem) { return true; } else { - // if it's smaller than the inHand value, it's an invalid move + // if the lastItem value is smaller than the inHand value, it's an invalid move console.log("Invalid move. Try again"); return false; - // if the peg is empty, or the block on the peg is larger than the block in hand, place it on the peg } } function checkForWin() { // once all blocks are moved to last peg, return 'winner' - if (stacks.c == [4, 3, 2, 1]) { + if (stacks.c.length === 4) { return true; } } function reset() { if (checkForWin) { - let stacks = { + stacks = { a: [4, 3, 2, 1], b: [], c: [] @@ -82,6 +85,7 @@ function towersOfHanoi(startStack, endStack) { // once the piece has been moved, check for a win if (checkForWin()) { console.log("Winner!"); + reset(); } } else { console.log("Not a legal move. Try again.");