From ad9c59e31ff84462f2ed27302b584c46dc7fdef8 Mon Sep 17 00:00:00 2001 From: Kiley Lewis Date: Wed, 17 Apr 2019 20:01:22 -0500 Subject: [PATCH 1/2] towers readme --- 03week/towersOfHanoi/README.md | 14 ++++++++++++++ 03week/{ => towersOfHanoi}/towersOfHanoi.js | 0 2 files changed, 14 insertions(+) create mode 100644 03week/towersOfHanoi/README.md rename 03week/{ => towersOfHanoi}/towersOfHanoi.js (100%) diff --git a/03week/towersOfHanoi/README.md b/03week/towersOfHanoi/README.md new file mode 100644 index 000000000..bd8f311d4 --- /dev/null +++ b/03week/towersOfHanoi/README.md @@ -0,0 +1,14 @@ +TOWERS OF HANOI LOGIC: + +* checking for win: + - do not check for a win on a. + - to determine a win check that either 'b' or 'c' has all #'s in descending order. greatest to least. + +* moving #'s: + - get user input. + - when we want to move a # we must check its value to make sure it is less than the # you are trying to place it on. + - only able to move a # from the top of a stack and place a # on the top of a stack. (from the end of an array and placed on the end of another array). + - we can always move #'s into an empty array, but you cannot choose from an empty stack / stack won't be updated, message will be printed "choose a valid stack". + - update stacks object by user input. startStack, endStack. + + \ No newline at end of file diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi/towersOfHanoi.js similarity index 100% rename from 03week/towersOfHanoi.js rename to 03week/towersOfHanoi/towersOfHanoi.js From d1222a90e643c77d4965e983aae4a9dbd766b091 Mon Sep 17 00:00:00 2001 From: Kiley Lewis Date: Mon, 29 Apr 2019 18:53:35 -0500 Subject: [PATCH 2/2] towers --- 03week/towersOfHanoi/towersOfHanoi.js | 39 ++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/03week/towersOfHanoi/towersOfHanoi.js b/03week/towersOfHanoi/towersOfHanoi.js index 3cf6df049..ffc6c73bc 100644 --- a/03week/towersOfHanoi/towersOfHanoi.js +++ b/03week/towersOfHanoi/towersOfHanoi.js @@ -19,23 +19,50 @@ function printStacks() { console.log("c: " + stacks.c); } -function movePiece() { - // Your code here +function movePiece(startAnswer, endAnswer) { + console.log("start " + startAnswer); + console.log("end " + endAnswer); + + let firstItem = startAnswer.pop(); + + let newStack = endAnswer.push(firstItem); + + return newStack; } -function isLegal() { - // Your code here +function isLegal(startAnswer, endAnswer) { + const myPiece = startAnswer[startAnswer.length - 1]; + const destination = endAnswer[endAnswer.length - 1]; + + if (myPiece > destination) { + return false; + } else { + return true; + } } function checkForWin() { - // Your code here + if (stacks.b.length == 4 || stacks.c.length == 4) { + return true; + } else { + return false; + } } function towersOfHanoi(startStack, endStack) { - // Your code here + let startAnswer = stacks[startStack]; + let endAnswer = stacks[endStack]; + + if (isLegal(startAnswer, endAnswer)) { + movePiece(startAnswer, endAnswer); + } else { + console.log("not a legal move"); + } + + checkForWin(); }