From 9157a66a36ad3437a3f6b121f36d77d2b0d08157 Mon Sep 17 00:00:00 2001 From: Chris Trevino Date: Tue, 16 Jun 2020 19:04:14 -0500 Subject: [PATCH 1/3] updated ' --- 03week/towersOfHanoi.js | 13 ++++++++++--- JS211_ArrayPractice | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) create mode 160000 JS211_ArrayPractice diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 3cf6df049..e507ad5c5 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -18,18 +18,25 @@ function printStacks() { console.log("b: " + stacks.b); console.log("c: " + stacks.c); } +/* +This function takes in start Stack and end Stack,and moves the top piece +*from the corresponding starting stack to the corresponding edning stack +* @param {*} -function movePiece() { +*/ + +function movePiece(startStack, endStack) { // Your code here + stacks.b.push('X') } -function isLegal() { +function isLegal(startStack, endStack) { // Your code here } -function checkForWin() { +function checkForWin() { // Your code here } diff --git a/JS211_ArrayPractice b/JS211_ArrayPractice new file mode 160000 index 000000000..1e00855c6 --- /dev/null +++ b/JS211_ArrayPractice @@ -0,0 +1 @@ +Subproject commit 1e00855c6576c929c7efa4a62225a720d36dd08a From 2e60dd1f84380ec93ed285adba492d9cfc9e40c7 Mon Sep 17 00:00:00 2001 From: Chris Trevino Date: Tue, 23 Jun 2020 21:15:16 -0500 Subject: [PATCH 2/3] changes --- 03week/towersOfHanoi.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index e507ad5c5..70a54a8bb 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -1,5 +1,5 @@ 'use strict'; - +// assert libary is used for testing const assert = require('assert'); const readline = require('readline'); const rl = readline.createInterface({ @@ -18,29 +18,54 @@ function printStacks() { console.log("b: " + stacks.b); console.log("c: " + stacks.c); } -/* -This function takes in start Stack and end Stack,and moves the top piece -*from the corresponding starting stack to the corresponding edning stack -* @param {*} -*/ + +/** +This function takes in start Stack and end Stack,and moves the top piece +*from the corresponding starting stack to the corresponding ending stack +* @param {*} startStack the name of the starting stack to move the piece from +* @param {*} endStack the name of the ending stack where the moved piece should land +* +*/ + function movePiece(startStack, endStack) { // Your code here stacks.b.push('X') } +/** + * This function takes in the startStack name and endStack name + * and returns true only if the move is legal and valid + * otherwise it return false + * @param {*} startStack + * @param {*} endStack + */ + function isLegal(startStack, endStack) { // Your code here + } - +/** + * This function returns true, if the board is in a "winning state" + * winning state means : you restacked on either stacks b or c, biggest to smallest + * + */ function checkForWin() { // Your code here } + + /** + * This function should take in 2 inputs, the start stack and end stack and process that turn for the player + * @param {*} startStack the stack they want to move from + * @param {*} endStack the stack they want to move too + * check to see if move is legal first, if move is legal "move piece", then ckeck to see if it wins print WIN + * + */ function towersOfHanoi(startStack, endStack) { // Your code here From e6fa0afa77448fddf27cd880ee3ca294e91ae858 Mon Sep 17 00:00:00 2001 From: Chris Trevino Date: Sat, 27 Jun 2020 13:45:31 -0500 Subject: [PATCH 3/3] changes --- 03week/towersOfHanoi.js | 48 ++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/03week/towersOfHanoi.js b/03week/towersOfHanoi.js index 70a54a8bb..1361db472 100644 --- a/03week/towersOfHanoi.js +++ b/03week/towersOfHanoi.js @@ -30,8 +30,11 @@ This function takes in start Stack and end Stack,and moves the top piece */ function movePiece(startStack, endStack) { - // Your code here - stacks.b.push('X') + // Need to first select a piece by removing it from the end of the stack... sTACKS[startStack] and removing from the end. utilizing pop() + let startingPiece = stacks[startStack].pop(); + // then need to push the piece into its its new stack. at end of stack. + stacks[endStack].push(startingPiece); + } @@ -40,13 +43,32 @@ function movePiece(startStack, endStack) { * and returns true only if the move is legal and valid * otherwise it return false * @param {*} startStack - * @param {*} endStack + * @param {*} endStack */ function isLegal(startStack, endStack) { - // Your code here - + // need to make sure program knows which inputs are usable + const onlyInputs = ["a", "b", "c"]; + if(onlyInputs.includes(startStack) == false || onlyInputs.includes(endStack) == false){ + return false; + } + + //if the move is into an empty stack move is legal + if(stacks[endStack].length == 0){ + + return true; + // if the the moving piece is moving to a larger piece the move is legal + + } else if(stacks[startStack].slice(-1) < stacks[endStack].slice(-1)){ + + return true; + + } else { + + return false; + } + } /** * This function returns true, if the board is in a "winning state" @@ -54,8 +76,13 @@ function isLegal(startStack, endStack) { * */ function checkForWin() { - // Your code here - + // if both B or C are full or there array length is 4 they have won else they lost + if (stacks.b.length == 4 || stacks.c.length == 4){ + console.log("You've Won!!!") + return true; + } else { + return false; + } } @@ -67,7 +94,12 @@ function checkForWin() { * */ function towersOfHanoi(startStack, endStack) { - // Your code here + if(isLegal(startStack,endStack) == true){ + movePiece(startStack, endStack); + } else { + console.log("illegal move or entry"); + } + checkForWin(); }