Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 74 additions & 10 deletions 03week/towersOfHanoi.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

// assert libary is used for testing
const assert = require('assert');
const readline = require('readline');
const rl = readline.createInterface({
Expand All @@ -19,23 +19,87 @@ function printStacks() {
console.log("c: " + stacks.c);
}

function movePiece() {
// Your code here

}

function isLegal() {
// Your code here
/**
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) {
// 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);

}

function checkForWin() {
// Your code here
}

/**
* 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) {
// 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"
* winning state means : you restacked on either stacks b or c, biggest to smallest
*
*/
function checkForWin() {
// 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;
}
}


/**
* 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
if(isLegal(startStack,endStack) == true){
movePiece(startStack, endStack);
} else {
console.log("illegal move or entry");
}
checkForWin();

}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the tests.

Expand Down
1 change: 1 addition & 0 deletions JS211_ArrayPractice
Submodule JS211_ArrayPractice added at 1e0085