Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 03week/towersOfHanoi/README.md
Original file line number Diff line number Diff line change
@@ -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.


39 changes: 33 additions & 6 deletions 03week/towersOfHanoi.js → 03week/towersOfHanoi/towersOfHanoi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}

Expand Down