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 67% rename from 03week/towersOfHanoi.js rename to 03week/towersOfHanoi/towersOfHanoi.js index 3cf6df049..ffc6c73bc 100644 --- a/03week/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(); }