diff --git a/03week/README.txt b/03week/README.txt new file mode 100644 index 000000000..e8b65774b --- /dev/null +++ b/03week/README.txt @@ -0,0 +1,16 @@ +Towers of Hanio + +Object of the game +Moved the stack from a to b, or c. + +How to play +startStack choose the letter for the row where you would like to take the item from +endStack choose where you would like the number to be placed. +Larger numbers are not allowed to be placed on smaller numbers. + + +Plan +Use pop() and push() to move the number selected by the user to which ever array was selected. +Check if a move is legal, or not. Do this by checking if the startStack if larger then the endStack, or if the user is choose a row with a number in the array and allowing the user to move to an empty array. +Check for win. Check if either b, or c are full while a and either b, or c are empty. +Console log Winner when the user has achieved the goal. \ No newline at end of file diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..ebe3b7ddf 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,94 +1,136 @@ -'use strict'; +"use strict"; -const assert = require('assert'); -const readline = require('readline'); +const assert = require("assert"); +const readline = require("readline"); const rl = readline.createInterface({ - input: process.stdin, - output: process.stdout + input: process.stdin, + output: process.stdout }); let stacks = { - a: [4, 3, 2, 1], - b: [], - c: [] + a: [4, 3, 2, 1], + b: [], + c: [] }; function printStacks() { - console.log("a: " + stacks.a); - console.log("b: " + stacks.b); - console.log("c: " + stacks.c); + console.log("a: " + stacks.a); + console.log("b: " + stacks.b); + console.log("c: " + stacks.c); } -function movePiece() { - // Your code here - +function movePiece(startStack, endStack) { + // Your code here + // Remove, or pop() the last item of the array select by startStack + // stacks[startStack].pop(); + // Move, or push() the item remove from pop() to the location selected by endStack + // stacks[endStack].push(); + stacks[endStack].push(stacks[startStack].pop()); } -function isLegal() { - // Your code here - +function restart() { + // Restart puts the board back to the original + let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; +} +function validMove() { + if (startStack == "a" || "b" || ("c" && endStack == "a") || "b" || "c") { + return true; + } else { + return false; + } } +function isLegal(startStack, endStack) { + // Your code here + + let start = stacks[startStack]; + let end = stacks[endStack]; + // check to see number being moved by startStack is greater than the last number in the array being chose by endStack + if ( + start[start.length - 1] < end[end.length - 1] || + (start.length != 0 && end.length == 0) + // check if startStack has an item in the array and allowing the move be made to an empty array + ) { + return true; + } else { + return false; + } +} function checkForWin() { - // Your code here - + // Your code here + // Checks to see if all numbers are in row b, or in row c + if (stacks.a.length === 0 && stacks.b.length === 0) { + return true; + } else if (stacks.a.length === 0 && stacks.c.length === 0) { + return true; + } else { + return false; + } } function towersOfHanoi(startStack, endStack) { - // Your code here - + // Your code here + if (isLegal(startStack, endStack) == true) { + movePiece(startStack, endStack); + if (checkForWin() == true) { + console.log("Winner"); + restart(); + } + } else { + console.log("Move is not legal"); + } } function getPrompt() { - printStacks(); - rl.question('start stack: ', (startStack) => { - rl.question('end stack: ', (endStack) => { - towersOfHanoi(startStack, endStack); - getPrompt(); - }); - }); + printStacks(); + rl.question("start stack: ", startStack => { + rl.question("end stack: ", endStack => { + towersOfHanoi(startStack, endStack); + getPrompt(); + }); + }); } // Tests -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); - }); - }); - +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(); - + getPrompt(); }