Skip to content
Open
Changes from 1 commit
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
155 changes: 85 additions & 70 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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) {
Expand All @@ -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)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you take this out? this was correct

// 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() {
Expand All @@ -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();
}
// 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()
// }