forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Adding partnerChallenge.js and 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
Open
copelandhouse2
wants to merge
6
commits into
gh-pages
Choose a base branch
from
week3a-towersOfHanoi
base: gh-pages
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9dc3d1a
Adding partnerChallenge.js and white board of towers of Hanoi
copelandhouse2 4bf5254
Built skeleton code for Towers of Hanoi
copelandhouse2 fc09e4d
Towers of Hanoi pretty much complete.
copelandhouse2 fce8b47
Added the ability to exit the game. Added some comments
copelandhouse2 5aa7597
ToH Officially complete: a) Ran linter, b) cleaned up comments.
copelandhouse2 045ea83
Improved checkForWin... now only a single return.
copelandhouse2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| 'use strict' | ||
|
|
||
| const partnerObj = { | ||
| firstName:'Craig', | ||
| lastName:'Copeland', | ||
| address1:'904 Prize Oaks Dr', | ||
| address2:'', | ||
| city:'Cedar Park', | ||
| state:'TX', | ||
| zip:'78613', | ||
| printAddress: () => { | ||
| console.log(partnerObj.address1); | ||
| console.log(partnerObj.address2); | ||
| console.log(`${partnerObj.city} ${partnerObj.state} ${partnerObj.zip}`); | ||
| } | ||
|
|
||
| }; | ||
|
|
||
| partnerObj.printAddress(); | ||
| console.log(`Hello ${partnerObj.firstName} ${partnerObj.lastName}`); | ||
|
|
||
| partnerObj.lastName = 'cars'; | ||
|
|
||
|
|
||
| const partnerArr = Object.keys(partnerObj); | ||
| console.log(partnerArr , 'partner Array'); | ||
|
|
||
|
|
||
| for (let i=0; i < partnerArr.length; i++) { | ||
| console.log(partnerArr[i]); | ||
| console.log(partnerObj[partnerArr[i]]); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,43 +8,133 @@ const rl = readline.createInterface({ | |
| }); | ||
|
|
||
| let stacks = { | ||
| a: [4, 3, 2, 1], | ||
| a: [3, 2, 1], | ||
| b: [], | ||
| c: [] | ||
| }; | ||
|
|
||
| const winCount = stacks.a.length; | ||
| let message = ''; | ||
|
|
||
| function printStacks() { | ||
| console.log("a: " + stacks.a); | ||
| console.log("b: " + stacks.b); | ||
| console.log("c: " + stacks.c); | ||
| } | ||
|
|
||
| function movePiece() { | ||
| // Your code here | ||
|
|
||
| // At this point in the code, the user data has been vetted and the move has been confirmed to be legal. | ||
| // This function simply pops the top "disc" off of start Stack and pushes in on end Stack | ||
| function movePiece(startStack,endStack) { | ||
| // const disktoMove = stacks[startStack].pop(); | ||
| // console.log(disktoMove); | ||
| stacks[endStack].push(stacks[startStack].pop()); | ||
| } | ||
|
|
||
| function isLegal() { | ||
| // Your code here | ||
|
|
||
| // The user data has been tested to make sure they are valid values. Now we need to test a bit further to see if the | ||
| // move from source to end is valid. This function does that. | ||
| function isLegal(startStack, endStack) { | ||
| // console.log('Start: ' + stacks[startStack][stacks[startStack].length-1]); | ||
| // console.log(`End: ${stacks[endStack].length}`); | ||
|
|
||
| if (stacks[startStack].length === 0) { | ||
| return false; | ||
| } else if (stacks[endStack].length === 0) { | ||
| return true; | ||
| } else { | ||
| return stacks[startStack][stacks[startStack].length-1] < stacks[endStack][stacks[endStack].length-1]; | ||
| } | ||
| } | ||
|
|
||
| // Function tests to see if all the discs are on the other stacks. If they are, that is a WIN. | ||
| function checkForWin() { | ||
| // Your code here | ||
|
|
||
| // console.log(`win count: ${winCount}`); | ||
| // console.log(`stack a count: ${stacks.a.length}`); | ||
| // console.log(`stack b count: ${stacks.b.length}`); | ||
| // console.log(`stack c count: ${stacks.c.length}`); | ||
| return stacks.b.length === winCount || stacks.c.length === winCount; | ||
| } | ||
|
|
||
| // this function is the main driver function. | ||
| function towersOfHanoi(startStack, endStack) { | ||
| // Your code here | ||
|
|
||
| /****************************** WHITE BOARD NOTES ******************************* | ||
| // first, make sure the user typed in either stack a, b, or c. User must enter a, b, c. Nothing else. | ||
| // the source stack selected must not be empty. There should be a disk there. | ||
| // (isLegal): If the last value (selected disk) on source stack is smaller than the last value on the target stack | ||
| // then call: (movePiece). Otherwise, prompt the user to try again. Clearly, the user is making an illegal move. | ||
| // (checkForWin) If stack b or stack c has all the disks, WIN. | ||
|
|
||
| // If valid entries then | ||
| // if isLegal() then | ||
| // movePiece() | ||
| // if checkForWin() then | ||
| // Announce winner! | ||
| // else | ||
| // Continue playing game | ||
| // end if | ||
| // else | ||
| // Tell user move cannot be made. Make another selection | ||
| // end if | ||
| // else | ||
| // Tell user to enter correct stack values | ||
| // end if | ||
|
|
||
| // isLegal(sourceStack, targetStack) // the tests expect this function to take in 2 values. | ||
| // if source stack array.last < target stack array.last then | ||
| // return true | ||
| // end if | ||
| // return false | ||
|
|
||
| // movePiece() | ||
| // target stack array.push(source stack array.pop) | ||
| // return | ||
|
|
||
| // checkForWin() | ||
| // if b.array.length === 4 || c.array.length === 4 then | ||
| // return true | ||
| // end if | ||
| // return false | ||
| *************************************************************************/ | ||
|
|
||
| // Tests to make sure user entries are only a, b, or c. | ||
| const validUserEntry = (myStack) => { | ||
| const validStacksArr = ['a','b','c']; | ||
| return validStacksArr.some(validStack => myStack === validStack); | ||
| } | ||
|
|
||
| startStack = startStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. | ||
| endStack = endStack.toLowerCase().trim(); // ensures user entry will be lower case. Also gets rid of spaces on either end. | ||
|
|
||
| if (validUserEntry(startStack) && validUserEntry(endStack)) { // test to make sure a, b, or c is entered. | ||
| if (isLegal(startStack, endStack)) { // test to make sure move is legal | ||
| movePiece(startStack, endStack); // Confirmed it is a legal move. Go ahead and pop disc from startStack and push to endStack | ||
| if (checkForWin()) { // See if all discs have been moved to stack b or c. If so, then WIN. | ||
| console.log('Congratulations! You won!'); | ||
| return true; // Returning true ends the game. We have a winner. | ||
| } | ||
| } else { | ||
| console.log('That is an illegal move. One of these issues... Try again\n'); | ||
| console.log(' a) Cannot place a larger disk on a smaller one.'); | ||
| console.log(' b) Start stack is empty'); | ||
|
|
||
| } | ||
| } else { | ||
| console.log('Please enter correct stack name... a, b, or c'); | ||
| } | ||
|
|
||
| return false; // No win yet. Returning false keeps the game going. | ||
| } | ||
|
|
||
| function getPrompt() { | ||
| printStacks(); | ||
| rl.question('start stack: ', (startStack) => { | ||
| rl.question('end stack: ', (endStack) => { | ||
| towersOfHanoi(startStack, endStack); | ||
| getPrompt(); | ||
| // I wrapped towersOfHanoi function around a condition so I can "end" the game. towersOfHanoi returns TRUE if someone | ||
| // won the game. It returns FALSE if the game is still going on. | ||
| if (!towersOfHanoi(startStack, endStack)) { | ||
|
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 is a really nice idea |
||
| getPrompt(); | ||
| } else { | ||
| process.exit(0); // this command exits the Program | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice method!