-
Notifications
You must be signed in to change notification settings - Fork 0
checkpoint1 #8
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?
checkpoint1 #8
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 |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| 'use strict'; | ||
|
|
||
| /* Function planning | ||
| function movePiece will move from start stack to end stack | ||
| function isLegal will test whether the move is legal- will return true or false | ||
| function checkForWin will check each move for a win- will return true or false | ||
| function reset will reset the game is checkForWin is true | ||
| 2 new tests - test if the startStack is empty | ||
| - small block onto larger block | ||
| */ | ||
|
|
||
|
|
||
| const assert = require('assert'); | ||
| const readline = require('readline'); | ||
| const rl = readline.createInterface({ | ||
|
|
@@ -19,24 +29,66 @@ function printStacks() { | |
| console.log("c: " + stacks.c); | ||
| } | ||
|
|
||
| function movePiece() { | ||
| // Your code here | ||
|
|
||
| function movePiece(startStack, endStack) { | ||
| if (startStack === 'a' && endStack === 'b') { | ||
| let mover = stacks.a.pop(); | ||
|
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. should be const |
||
| stacks.b.push(mover); | ||
| } else if (startStack === 'a' && endStack === 'c') { | ||
| let mover = stacks.a.pop(); | ||
|
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. should be const |
||
| stacks.c.push(mover); | ||
| } else if (startStack === 'b' && endStack === 'a') { | ||
| let mover = stacks.b.pop(); | ||
|
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. should be const |
||
| stacks.a.push(mover); | ||
| } else if (startStack === 'b' && endStack === 'c') { | ||
| let mover = stacks.b.pop(); | ||
|
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. should be const |
||
| stacks.c.push(mover); | ||
| } else if (startStack === 'c' && endStack === 'a') { | ||
| let mover = stacks.c.pop(); | ||
|
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. should be const |
||
| stacks.a.push(mover); | ||
| } else { | ||
| let mover = stacks.c.pop(); | ||
|
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. should be const |
||
| stacks.b.push(mover); | ||
| } | ||
| } | ||
|
|
||
| function isLegal() { | ||
| // Your code here | ||
|
|
||
| function isLegal(start, end) { | ||
| const startStack = stacks[start]; | ||
| const endStack = stacks[end]; | ||
| if (startStack.length === 0) { | ||
|
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. anytime that you see that you're returning true or false, return the evaluation |
||
| return false; | ||
| } | ||
| if (endStack.length === 0) { | ||
| return true; | ||
| } else if (startStack[startStack.length - 1] > endStack[endStack.length - 1]) { | ||
| return false; | ||
| } else { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| function checkForWin() { | ||
| // Your code here | ||
| if (stacks.b.length === 4 || stacks.c.length === 4) { | ||
|
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. anytime that you see that you're returning true or false, return the evaluation |
||
| console.log('You win!') | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| function reset() { | ||
| stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [] | ||
| }; | ||
| } | ||
|
|
||
| function towersOfHanoi(startStack, endStack) { | ||
| // Your code here | ||
|
|
||
| movePiece(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. you should only move the piece if your function is legal, and you should only check for win if your function is legal |
||
| isLegal(startStack, endStack); | ||
| if (checkForWin()) { | ||
| reset(); | ||
| } | ||
| } | ||
|
|
||
| function getPrompt() { | ||
|
|
@@ -69,6 +121,14 @@ if (typeof describe === 'function') { | |
| }; | ||
| assert.equal(isLegal('a', 'b'), false); | ||
| }); | ||
| it('should allow a small block on a big block', () => { | ||
| stacks = { | ||
| a: [4, 3], | ||
| b: [1], | ||
| c: [2], | ||
| }; | ||
| assert.equal(isLegal('b', 'c'), true); | ||
| }); | ||
| it('should allow a legal move', () => { | ||
| stacks = { | ||
| a: [4, 3, 2, 1], | ||
|
|
@@ -77,6 +137,14 @@ if (typeof describe === 'function') { | |
| }; | ||
| assert.equal(isLegal('a', 'c'), true); | ||
| }); | ||
| it('should only move if startStack is not empty', () => { | ||
| stacks = { | ||
| a: [4, 3, 2, 1], | ||
| b: [], | ||
| c: [] | ||
| }; | ||
| assert.equal(isLegal('b', 'c'), false); | ||
| }); | ||
| }); | ||
| describe('#checkForWin()', () => { | ||
| it('should detect a win', () => { | ||
|
|
||
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.
for every stack and letter, you're checking if the stack is a,b, or c, twice, that's a lot of repetition that you can condense.