From c26b04da03dc950d2ded56a95e3e0422f21db98c Mon Sep 17 00:00:00 2001 From: anniebaker Date: Thu, 25 Apr 2019 18:47:35 -0500 Subject: [PATCH] checkpoint1 --- 04week/README.md | 150 ++++++++++++++++++++++++++++++++++++++++ 04week/towersOfHanoi.js | 141 +++++++++++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 04week/README.md create mode 100644 04week/towersOfHanoi.js diff --git a/04week/README.md b/04week/README.md new file mode 100644 index 000000000..827df6e94 --- /dev/null +++ b/04week/README.md @@ -0,0 +1,150 @@ +'use strict'; + +const assert = require('assert'); +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [], +}; + +function printStacks() { + console.log("a: " + stacks.a); + console.log("b: " + stacks.b); + console.log("c: " + stacks.c); +} + +// Take the last element of the starting array and add it to the last part of the ending array +function movePiece(startStack, endStack) { + stacks[endStack].push(stacks[startStack].pop()); +} + + +// Compare the size of the last element of the starting and ending array +function isLegal(startStack, endStack) { +// Allow a move if the array is empty as a special condition + if (stacks[endStack].slice(-1)==""){ + return true; +// Allow a move if the number of the ending array is smaller than the size of the starting array + } else if (stacks[endStack].slice(-1) > stacks[startStack].slice(-1)) { + moves = moves + 1; + return true; + } else { + return false; + } +}; + +// Check to see if the b or c array has the winning arrangement. Array a does not count towards a win. +//loop thru object array +let winStack = [4, 3, 2, 1]; +let gameOver = false; +//check to see if the object property values equal a winning array of [4,3,2,1] +function arraysEqual(arr1, arr2) { + if(arr1.length !== arr2.length) + return false; + for(var i = arr1.length; i--;) { + if(arr1[i] !== arr2[i]) + return false; + } + return true; +} + +function checkForWin() { + //if the function that determined whether the array contents are equal is true, comparing it to winStack, proceed + if (arraysEqual(stacks.b, winStack) || arraysEqual(stacks.c, winStack)) { + console.log("you're a winner!"); + gameOver = true; + return true; + } else { + return false; + } +} + + +function towersOfHanoi(startStack, endStack) { + //first check for win + checkForWin(); + //if check for win is false, continue with moves + if (!gameOver) { + //make sure it's a legal move + if (isLegal(startStack, endStack) == true) { + movePiece(startStack, endStack); + } + } else { + //if you won, don't allow a move and instead log a winning sentence + console.log("You win!"); + } +} + +function 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 in stack b', () => { + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(checkForWin(), true); + stacks = { a: [1], b: [4, 3, 2], c: [] }; + assert.equal(checkForWin(), false); + }); + //sepcifically check if stack c is a winner + it('should detect a win in stack c', () => { + stacks = { a: [], b: [], c: [4, 3, 2, 1] }; + assert.equal(checkForWin(), true); + stacks = { a: [1], b: [4, 3, 2], c: [] }; + assert.equal(checkForWin(), false); + }); + //specifically check to see that stack a cannot trigger a win + it('should not detect a win in stack a', () => { + stacks = { a: [4, 3, 2, 1], b: [], c: [] }; + assert.equal(checkForWin(), false); + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(checkForWin(), true); + }); + //make sure the game is over once a winner is chosen + it('should end game once won, ()', () => { + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(gameOver, true); + }); + }); +} else { + getPrompt(); +} diff --git a/04week/towersOfHanoi.js b/04week/towersOfHanoi.js new file mode 100644 index 000000000..a2a85ff30 --- /dev/null +++ b/04week/towersOfHanoi.js @@ -0,0 +1,141 @@ +** +'use strict'; + +const assert = require('assert'); +const readline = require('readline'); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [], +}; + +function printStacks() { + console.log("a: " + stacks.a); + console.log("b: " + stacks.b); + console.log("c: " + stacks.c); +} + +// Take the last element of the starting array and append it to the last of the ending array +function movePiece(startStack, endStack) { + stacks[endStack].push(stacks[startStack].pop()); +} + + +// Compare the size of the last element of the starting and ending array +function isLegal(startStack, endStack) { +// Allow a move if the array is empty as a special condition + if (stacks[endStack].slice(-1)==""){ + return true; +// Allow a move if the number of the ending array is smaller than the size of the starting array + } else if (stacks[endStack].slice(-1) > stacks[startStack].slice(-1)) { + return true; + } else { + return false; + } +}; + +// Check to see if the b or c array has the winning arrangement. Array a does not count towards a win. +//loop thru object array +let winStack = [4, 3, 2, 1]; +let gameOver = false; +function arraysEqual(arr1, arr2) { + if(arr1.length !== arr2.length) + return false; + for(var i = arr1.length; i--;) { + if(arr1[i] !== arr2[i]) + return false; + } + return true; +} + +function checkForWin() { + if (arraysEqual(stacks.b, winStack) || arraysEqual(stacks.c, winStack)) { + console.log("you're a winner!"); + gameOver = true; + return true; + } else { + return false; + } +} + + +function towersOfHanoi(startStack, endStack) { + checkForWin(); + if (!gameOver) { + if (isLegal(startStack, endStack) == true) { + movePiece(startStack, endStack); + } + } else { + console.log("You win!"); + } +} + +function 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 in stack b', () => { + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(checkForWin(), true); + stacks = { a: [1], b: [4, 3, 2], c: [] }; + assert.equal(checkForWin(), false); + }); + it('should detect a win in stack c', () => { + stacks = { a: [], b: [], c: [4, 3, 2, 1] }; + assert.equal(checkForWin(), true); + stacks = { a: [1], b: [4, 3, 2], c: [] }; + assert.equal(checkForWin(), false); + }); + it('should not detect a win in stack a', () => { + stacks = { a: [4, 3, 2, 1], b: [], c: [] }; + assert.equal(checkForWin(), false); + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(checkForWin(), true); + }); + it('should end game once won, ()', () => { + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(gameOver, true); + }); + }); +} else { + getPrompt(); +}