Skip to content
Open
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
86 changes: 77 additions & 9 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
'use strict';

/* Function planning
function movePiece will move from start stack to end stack
function isLegal will test whether the move is legal- will return true or false
function checkForWin will check each move for a win- will return true or false
function reset will reset the game is checkForWin is true
2 new tests - test if the startStack is empty
- small block onto larger block
*/


const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
Expand All @@ -19,24 +29,66 @@ function printStacks() {
console.log("c: " + stacks.c);
}

function movePiece() {
// Your code here

function movePiece(startStack, endStack) {
if (startStack === 'a' && endStack === 'b') {

Choose a reason for hiding this comment

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

for every stack and letter, you're checking if the stack is a,b, or c, twice, that's a lot of repetition that you can condense.

let mover = stacks.a.pop();

Choose a reason for hiding this comment

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

should be const

stacks.b.push(mover);
} else if (startStack === 'a' && endStack === 'c') {
let mover = stacks.a.pop();

Choose a reason for hiding this comment

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

should be const

stacks.c.push(mover);
} else if (startStack === 'b' && endStack === 'a') {
let mover = stacks.b.pop();

Choose a reason for hiding this comment

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

should be const

stacks.a.push(mover);
} else if (startStack === 'b' && endStack === 'c') {
let mover = stacks.b.pop();

Choose a reason for hiding this comment

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

should be const

stacks.c.push(mover);
} else if (startStack === 'c' && endStack === 'a') {
let mover = stacks.c.pop();

Choose a reason for hiding this comment

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

should be const

stacks.a.push(mover);
} else {
let mover = stacks.c.pop();

Choose a reason for hiding this comment

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

should be const

stacks.b.push(mover);
}
}

function isLegal() {
// Your code here

function isLegal(start, end) {
const startStack = stacks[start];
const endStack = stacks[end];
if (startStack.length === 0) {

Choose a reason for hiding this comment

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

anytime that you see that you're returning true or false, return the evaluation

return false;
}
if (endStack.length === 0) {
return true;
} else if (startStack[startStack.length - 1] > endStack[endStack.length - 1]) {
return false;
} else {
return true;
}
}

function checkForWin() {
// Your code here
if (stacks.b.length === 4 || stacks.c.length === 4) {

Choose a reason for hiding this comment

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

anytime that you see that you're returning true or false, return the evaluation

console.log('You win!')
return true;
} else {
return false;
}
}

function reset() {
stacks = {
a: [4, 3, 2, 1],
b: [],
c: []
};
}

function towersOfHanoi(startStack, endStack) {
// Your code here

movePiece(startStack, endStack);

Choose a reason for hiding this comment

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

you should only move the piece if your function is legal, and you should only check for win if your function is legal

isLegal(startStack, endStack);
if (checkForWin()) {
reset();
}
}

function getPrompt() {
Expand Down Expand Up @@ -69,6 +121,14 @@ if (typeof describe === 'function') {
};
assert.equal(isLegal('a', 'b'), false);
});
it('should allow a small block on a big block', () => {
stacks = {
a: [4, 3],
b: [1],
c: [2],
};
assert.equal(isLegal('b', 'c'), true);
});
it('should allow a legal move', () => {
stacks = {
a: [4, 3, 2, 1],
Expand All @@ -77,6 +137,14 @@ if (typeof describe === 'function') {
};
assert.equal(isLegal('a', 'c'), true);
});
it('should only move if startStack is not empty', () => {
stacks = {
a: [4, 3, 2, 1],
b: [],
c: []
};
assert.equal(isLegal('b', 'c'), false);
});
});
describe('#checkForWin()', () => {
it('should detect a win', () => {
Expand Down