From 9adff0ef22d1ec58ac969549407d5e801a4b658d Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 31 Oct 2018 01:45:38 -0500 Subject: [PATCH] checkpoint 1 --- 03week/towersOfHanoi.js | 88 ++++++++++++++++++++++++++++++++--------- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..a229c0933 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,5 +1,17 @@ 'use strict'; +//whiteboarding +//getpromt to ask from which stack to move and where to. +//if endstack is empty then move, if not then check legal +//move has to be legal, (isLegal) +//if start number is smaller than end number then move piece +//if illegal move tell them and get prompt again +//if legal, pop off of initial stack and push onto next stack (movePiece) +//check for win +//if not win, reprompt +//if win, tell user you won, reset stack (stackReset) + + const assert = require('assert'); const readline = require('readline'); const rl = readline.createInterface({ @@ -17,27 +29,65 @@ function printStacks() { console.log("a: " + stacks.a); console.log("b: " + stacks.b); console.log("c: " + stacks.c); -} - -function movePiece() { - // Your code here - -} - -function isLegal() { - // Your code here +}; -} +const movePiece = (startStack, endStack) => { + const start = stacks[startStack]; + const end = stacks[endStack]; + end.push(start.pop()); +}; -function checkForWin() { - // Your code here +const isLegal = (startStack, endStack) => { + const SSLastNum = stacks[startStack][stacks[startStack].length-1]; + const ESLastnum = stacks[endStack][stacks[endStack].length-1]; + if (ESLastnum > SSLastNum) { + return true; + } else { + return false; + }; +}; -} +const checkForWin = () => { + let checkWin = false; + const solution = [4, 3, 2, 1]; + for (index = 0; index < 4; index++) { + if (stacks['c'][index] == solution[index]) { + checkWin = true; + } else { + checkWin = false; + break; + }; + }; + if(checkWin) { + return "You win!"; + } else { + return "You suck"; + }; +}; -function towersOfHanoi(startStack, endStack) { - // Your code here +const resetStack = () => { + let stacks = { + a: [4, 3, 2, 1], + b: [], + c: [] + }; +}; -} +const towersOfHanoi = (startStack, endStack) => { + if (stacks[endStack].length == 0) { + movePiece(startStack, endStack); + } else { + if (isLegal(startStack, endStack)) { + movePiece(startStack, endStack); + if (checkForWin()) { + alert('Congrats!'); + resetStack(); + } + } else { + alert('Please enter a legal move') + } + } +}; function getPrompt() { printStacks(); @@ -47,7 +97,7 @@ function getPrompt() { getPrompt(); }); }); -} +}; // Tests @@ -80,8 +130,10 @@ if (typeof describe === 'function') { }); describe('#checkForWin()', () => { it('should detect a win', () => { - stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + stacks = { a: [], b: [], c: [4, 3, 2, 1] }; assert.equal(checkForWin(), true); + stacks = { a: [], b: [4, 3, 2, 1], c: [] }; + assert.equal(checkForWin(), false); stacks = { a: [1], b: [4, 3, 2], c: [] }; assert.equal(checkForWin(), false); });