forked from AustinCodingAcademy/javascript-workbook
-
Notifications
You must be signed in to change notification settings - Fork 0
checkpoint1 #6
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
anniebaker
wants to merge
1
commit into
gh-pages
Choose a base branch
from
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
checkpoint1 #6
Changes from all commits
Commits
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,150 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const readline = require('readline'); | ||
| const rl = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
| let stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [], | ||
| }; | ||
|
|
||
| function printStacks() { | ||
| console.log("a: " + stacks.a); | ||
| console.log("b: " + stacks.b); | ||
| console.log("c: " + stacks.c); | ||
| } | ||
|
|
||
| // Take the last element of the starting array and add it to the last part of the ending array | ||
| function movePiece(startStack, endStack) { | ||
| stacks[endStack].push(stacks[startStack].pop()); | ||
| } | ||
|
|
||
|
|
||
| // Compare the size of the last element of the starting and ending array | ||
| function isLegal(startStack, endStack) { | ||
| // Allow a move if the array is empty as a special condition | ||
| if (stacks[endStack].slice(-1)==""){ | ||
| return true; | ||
| // Allow a move if the number of the ending array is smaller than the size of the starting array | ||
| } else if (stacks[endStack].slice(-1) > stacks[startStack].slice(-1)) { | ||
| moves = moves + 1; | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| // Check to see if the b or c array has the winning arrangement. Array a does not count towards a win. | ||
| //loop thru object array | ||
| let winStack = [4, 3, 2, 1]; | ||
| let gameOver = false; | ||
| //check to see if the object property values equal a winning array of [4,3,2,1] | ||
| function arraysEqual(arr1, arr2) { | ||
| if(arr1.length !== arr2.length) | ||
| return false; | ||
| for(var i = arr1.length; i--;) { | ||
| if(arr1[i] !== arr2[i]) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function checkForWin() { | ||
| //if the function that determined whether the array contents are equal is true, comparing it to winStack, proceed | ||
| if (arraysEqual(stacks.b, winStack) || arraysEqual(stacks.c, winStack)) { | ||
| console.log("you're a winner!"); | ||
| gameOver = true; | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function towersOfHanoi(startStack, endStack) { | ||
| //first check for win | ||
| checkForWin(); | ||
| //if check for win is false, continue with moves | ||
| if (!gameOver) { | ||
| //make sure it's a legal move | ||
| if (isLegal(startStack, endStack) == true) { | ||
| movePiece(startStack, endStack); | ||
| } | ||
| } else { | ||
| //if you won, don't allow a move and instead log a winning sentence | ||
| console.log("You win!"); | ||
| } | ||
| } | ||
|
|
||
| function getPrompt() { | ||
| printStacks(); | ||
| rl.question('start stack: ', (startStack) => { | ||
| rl.question('end stack: ', (endStack) => { | ||
| towersOfHanoi(startStack, endStack); | ||
| getPrompt(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // Tests | ||
| if (typeof describe === 'function') { | ||
| describe('#towersOfHanoi()', () => { | ||
| it('should be able to move a block', () => { | ||
| towersOfHanoi('a', 'b'); | ||
| assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#isLegal()', () => { | ||
| it('should not allow an illegal move', () => { | ||
| stacks = { | ||
| a: [4, 3, 2], | ||
| b: [1], | ||
| c: [] | ||
| }; | ||
| assert.equal(isLegal('a', 'b'), false); | ||
| }); | ||
| it('should allow a legal move', () => { | ||
| stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [] | ||
| }; | ||
| assert.equal(isLegal('a', 'c'), true); | ||
| }); | ||
| }); | ||
| describe('#checkForWin()', () => { | ||
| it('should detect a win in stack b', () => { | ||
| stacks = { a: [], b: [4, 3, 2, 1], c: [] }; | ||
| assert.equal(checkForWin(), true); | ||
| stacks = { a: [1], b: [4, 3, 2], c: [] }; | ||
| assert.equal(checkForWin(), false); | ||
| }); | ||
| //sepcifically check if stack c is a winner | ||
| it('should detect a win in stack c', () => { | ||
| stacks = { a: [], b: [], c: [4, 3, 2, 1] }; | ||
| assert.equal(checkForWin(), true); | ||
| stacks = { a: [1], b: [4, 3, 2], c: [] }; | ||
| assert.equal(checkForWin(), false); | ||
| }); | ||
| //specifically check to see that stack a cannot trigger a win | ||
| it('should not detect a win in stack a', () => { | ||
| stacks = { a: [4, 3, 2, 1], b: [], c: [] }; | ||
| assert.equal(checkForWin(), false); | ||
| stacks = { a: [], b: [4, 3, 2, 1], c: [] }; | ||
| assert.equal(checkForWin(), true); | ||
| }); | ||
| //make sure the game is over once a winner is chosen | ||
| it('should end game once won, ()', () => { | ||
| stacks = { a: [], b: [4, 3, 2, 1], c: [] }; | ||
| assert.equal(gameOver, true); | ||
| }); | ||
| }); | ||
| } else { | ||
| getPrompt(); | ||
| } | ||
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,141 @@ | ||
| ** | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const readline = require('readline'); | ||
| const rl = readline.createInterface({ | ||
| input: process.stdin, | ||
| output: process.stdout | ||
| }); | ||
|
|
||
| let stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [], | ||
| }; | ||
|
|
||
| function printStacks() { | ||
| console.log("a: " + stacks.a); | ||
| console.log("b: " + stacks.b); | ||
| console.log("c: " + stacks.c); | ||
| } | ||
|
|
||
| // Take the last element of the starting array and append it to the last of the ending array | ||
| function movePiece(startStack, endStack) { | ||
| stacks[endStack].push(stacks[startStack].pop()); | ||
| } | ||
|
|
||
|
|
||
| // Compare the size of the last element of the starting and ending array | ||
| function isLegal(startStack, endStack) { | ||
| // Allow a move if the array is empty as a special condition | ||
| if (stacks[endStack].slice(-1)==""){ | ||
| return true; | ||
| // Allow a move if the number of the ending array is smaller than the size of the starting array | ||
| } else if (stacks[endStack].slice(-1) > stacks[startStack].slice(-1)) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| // Check to see if the b or c array has the winning arrangement. Array a does not count towards a win. | ||
| //loop thru object array | ||
| let winStack = [4, 3, 2, 1]; | ||
| let gameOver = false; | ||
| function arraysEqual(arr1, arr2) { | ||
| if(arr1.length !== arr2.length) | ||
| return false; | ||
| for(var i = arr1.length; i--;) { | ||
| if(arr1[i] !== arr2[i]) | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function checkForWin() { | ||
| if (arraysEqual(stacks.b, winStack) || arraysEqual(stacks.c, winStack)) { | ||
| console.log("you're a winner!"); | ||
| gameOver = true; | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function towersOfHanoi(startStack, endStack) { | ||
| checkForWin(); | ||
| if (!gameOver) { | ||
| if (isLegal(startStack, endStack) == true) { | ||
| movePiece(startStack, endStack); | ||
| } | ||
| } else { | ||
| console.log("You win!"); | ||
| } | ||
| } | ||
|
|
||
| function getPrompt() { | ||
| printStacks(); | ||
| rl.question('start stack: ', (startStack) => { | ||
| rl.question('end stack: ', (endStack) => { | ||
| towersOfHanoi(startStack, endStack); | ||
| getPrompt(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| // Tests | ||
| if (typeof describe === 'function') { | ||
| describe('#towersOfHanoi()', () => { | ||
| it('should be able to move a block', () => { | ||
| towersOfHanoi('a', 'b'); | ||
| assert.deepEqual(stacks, { a: [4, 3, 2], b: [1], c: [] }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('#isLegal()', () => { | ||
| it('should not allow an illegal move', () => { | ||
| stacks = { | ||
| a: [4, 3, 2], | ||
| b: [1], | ||
| c: [] | ||
| }; | ||
| assert.equal(isLegal('a', 'b'), false); | ||
| }); | ||
| it('should allow a legal move', () => { | ||
| stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [] | ||
| }; | ||
| assert.equal(isLegal('a', 'c'), true); | ||
| }); | ||
| }); | ||
| describe('#checkForWin()', () => { | ||
| it('should detect a win in stack b', () => { | ||
| stacks = { a: [], b: [4, 3, 2, 1], c: [] }; | ||
| assert.equal(checkForWin(), true); | ||
| stacks = { a: [1], b: [4, 3, 2], c: [] }; | ||
| assert.equal(checkForWin(), false); | ||
| }); | ||
| it('should detect a win in stack c', () => { | ||
| stacks = { a: [], b: [], c: [4, 3, 2, 1] }; | ||
| assert.equal(checkForWin(), true); | ||
| stacks = { a: [1], b: [4, 3, 2], c: [] }; | ||
| assert.equal(checkForWin(), false); | ||
| }); | ||
| it('should not detect a win in stack a', () => { | ||
| stacks = { a: [4, 3, 2, 1], b: [], c: [] }; | ||
| assert.equal(checkForWin(), false); | ||
| stacks = { a: [], b: [4, 3, 2, 1], c: [] }; | ||
| assert.equal(checkForWin(), true); | ||
| }); | ||
| it('should end game once won, ()', () => { | ||
| stacks = { a: [], b: [4, 3, 2, 1], c: [] }; | ||
| assert.equal(gameOver, true); | ||
| }); | ||
| }); | ||
| } else { | ||
| getPrompt(); | ||
| } |
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.
This isn't really a code plan, lol..