-
Notifications
You must be signed in to change notification settings - Fork 0
Towers of Hanoi #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: gh-pages
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,42 +13,91 @@ let stacks = { | |
| c: [] | ||
| }; | ||
|
|
||
| /* | ||
| *This function displays the "board" to the user | ||
| */ | ||
| function printStacks() { | ||
| console.log("a: " + stacks.a); | ||
| console.log("b: " + stacks.b); | ||
| console.log("c: " + stacks.c); | ||
| } | ||
|
|
||
| function movePiece() { | ||
| // Your code here | ||
|
|
||
| /* | ||
| *This function removes a block from a stack and adds that block to a different stack | ||
| * | ||
| * @param startStack the stack we are removing the element from | ||
| * @param endStack the stack we are adding the element to | ||
| */ | ||
| function movePiece(startStack, endStack) { | ||
| let blockMove = stacks[startStack].pop(); //remove the "top block" from startStack | ||
| stacks[endStack].push(blockMove); //add the removed block to endStack | ||
| } | ||
|
|
||
| function isLegal() { | ||
| // Your code here | ||
|
|
||
| /* | ||
| *This function checks to see if the proposed move is legal | ||
| * | ||
| * @param startStack the stack we are removing the element from | ||
| * @param endStack the stack we are adding the element to | ||
| * @return true if the move is legal | ||
| */ | ||
| function isLegal(startStack, endStack) { | ||
| if (((startStack==='a')||(startStack==='b')||(startStack==='c'))&& | ||
| ((endStack==='a')||(endStack==='b')||(endStack==='c'))) { //checking to make sure input is valid | ||
| let begin = stacks[startStack]; | ||
| let final = stacks[endStack]; | ||
| //console.log("Length of startStack: " + begin.length); | ||
| //console.log("Value of block being moved: " + begin[begin.length - 1]); | ||
| //console.log("Value of block being topped off: " + final[final.length - 1]); | ||
| if (begin[begin.length - 1] > final[final.length - 1]) { //making sure the "block" is being move onto a larger "block" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should return a false if it is not a legal move. This implementation allows you to move piece back to its own stack. Not wrong just weird that it would be allowed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also allow me to move from an empty stack to another empty stack, or from an empty stack to a non-empty stack |
||
| console.log("***ILLEGAL MOVE***"); | ||
| } else{ | ||
| return true; | ||
| } | ||
| } else{ | ||
| console.log("***Invalid input.***") | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| *This function checks for a win | ||
| */ | ||
| function checkForWin() { | ||
| // Your code here | ||
|
|
||
| if (stacks.c[3] === 1){ //checks for the "1" block on top of a four-stack in stack "c." | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like that you reset the board. |
||
| console.log("**************"); | ||
| console.log("***YOU WIN!***"); | ||
| console.log("**************"); | ||
| stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [] | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| *This function runs the game | ||
| * | ||
| * @param startStack the stack we are removing the element from | ||
| * @param endStack the stack we are adding the element to | ||
| */ | ||
| function towersOfHanoi(startStack, endStack) { | ||
| // Your code here | ||
|
|
||
| if (isLegal(startStack, endStack)) { //if move is legal, move piece then check for win | ||
| movePiece(startStack, endStack); | ||
| checkForWin(); | ||
| } | ||
| } | ||
|
|
||
| /* | ||
| *This function prompts the user for inputs then runs towersOfHanoi | ||
| */ | ||
| function getPrompt() { | ||
| printStacks(); | ||
| rl.question('start stack: ', (startStack) => { | ||
| rl.question('end stack: ', (endStack) => { | ||
| towersOfHanoi(startStack, endStack); | ||
| getPrompt(); | ||
| rl.question('start stack: ', (startStack) => { //get user input to define what stack the "block" is being taken from | ||
| rl.question('end stack: ', (endStack) => { //get user to define what stack the "block" is being placed on | ||
| towersOfHanoi(startStack, endStack); //run game logic | ||
| getPrompt(); //recursion to prompt next move | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| getPrompt(); | ||
|
|
||
|
|
||
| getPrompt(); //start game on run | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really like the documentation, and it was obvious you used log statements to debug as you were going along. Nice!