From a0c2c68171f519c02236ae0b7223796df11eee46 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 11 Jul 2019 17:23:52 -0500 Subject: [PATCH 1/2] No restart --- 03week/README.txt | 9 +++ 03week/towersOfHanoi.js | 175 +++++++++++++++++++++++++--------------- 2 files changed, 117 insertions(+), 67 deletions(-) create mode 100644 03week/README.txt diff --git a/03week/README.txt b/03week/README.txt new file mode 100644 index 000000000..b24e6aa16 --- /dev/null +++ b/03week/README.txt @@ -0,0 +1,9 @@ +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. \ No newline at end of file diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..280fe83b0 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,94 +1,135 @@ -'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 isLegal(startStack, endStack) { + // Your code here + + // const starting = stacks[start]; + // const ending = stacks[end]; + // return ( + // (starting[starting.length - 1] < ending[ending.length - 1] || + // ending.length == 0) && + // starting.length != 0 + // ); + let start = stacks[startStack]; + let end = stacks[endStack]; + if ( + start[start.length - 1] < end[end.length - 1] || + (start.length != 0 && end.length == 0) + ) { + 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 (startStack && endStack) { + if (isLegal(startStack, endStack) == true) { + movePiece(startStack, endStack); + if (checkForWin() == true) { + 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(); } From ccc2d083dd9cb772c9d82c75d371662bc722a3c6 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 11 Jul 2019 17:49:07 -0500 Subject: [PATCH 2/2] final --- 03week/README.txt | 9 ++++++++- 03week/towersOfHanoi.js | 19 ++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/03week/README.txt b/03week/README.txt index b24e6aa16..e8b65774b 100644 --- a/03week/README.txt +++ b/03week/README.txt @@ -6,4 +6,11 @@ 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. \ No newline at end of file +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 280fe83b0..ebe3b7ddf 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -36,22 +36,24 @@ function restart() { c: [] }; } +function validMove() { + if (startStack == "a" || "b" || ("c" && endStack == "a") || "b" || "c") { + return true; + } else { + return false; + } +} function isLegal(startStack, endStack) { // Your code here - // const starting = stacks[start]; - // const ending = stacks[end]; - // return ( - // (starting[starting.length - 1] < ending[ending.length - 1] || - // ending.length == 0) && - // starting.length != 0 - // ); 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 { @@ -72,17 +74,16 @@ function checkForWin() { function towersOfHanoi(startStack, endStack) { // Your code here - // if (startStack && endStack) { 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();