-
Notifications
You must be signed in to change notification settings - Fork 234
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
Parsity Eval #239
base: master
Are you sure you want to change the base?
Parsity Eval #239
Conversation
@@ -0,0 +1,110 @@ | |||
// Initial game board | |||
var towersBoard = { |
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.
Dont use var, either const or let
console.log('Invalid move, large discs may not be place on smaller ones. Try again'); | ||
}; | ||
}; | ||
function checkWinner() { |
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.
check your spacing
function checkWinner() { | |
function checkWinner() { |
|
||
Array.prototype.myForEach = function(callback) { | ||
for (let i = 0; i < this.length; i++) { | ||
var element = this[i]; |
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.
using var, just made this available to the whole scope
] | ||
}; | ||
|
||
Array.prototype.myForEach = function(callback) { |
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.
is this a recreation of the already existing forEach helper function?
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 is not recommended for larger applications. Using a normal function is better and more maintainable.
function customForEach(arr, callback) {
for (let i = 0; i < arr.length; i++) {
callback(arr[i]);
}
}
function moveDisc(fromPeg, toPeg) { | ||
if (towersBoard.board[fromPeg].length === 0) { | ||
console.log('No disc to move'); | ||
return; | ||
}; | ||
|
||
var disc = towersBoard.board[fromPeg][towersBoard.board[fromPeg].length - 1]; | ||
var destinationPeg = towersBoard.board[toPeg]; | ||
|
||
// Make sure that the move is valid (smaller disc on top of larger disc) | ||
if (destinationPeg.length === 0 || disc < destinationPeg[destinationPeg.length - 1]) { | ||
towersBoard.board[toPeg].push(towersBoard.board[fromPeg].pop()); | ||
console.log(`Moved disc ${disc} from Peg ${fromPeg + 1} to Peg ${toPeg + 1}`); | ||
displayBoard(); | ||
checkWinner(); | ||
} else { | ||
console.log('Invalid move, large discs may not be place on smaller ones. Try again'); | ||
}; | ||
}; |
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.
your code alignment makes the code not really readable. Hard to understand where the function ends.
return; | ||
}; |
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.
instead of this, just do an else and call a function.
}; | ||
|
||
function moveDisc(fromPeg, toPeg) { | ||
if (towersBoard.board[fromPeg].length === 0) { |
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.
to help with readability this check could have been a const
if (towersBoard.board[fromPeg].length === 0) { | |
const isPegEmpty = towersBoard.board[fromPeg].length === 0; | |
if (isPegEmpty) { |
var destinationPeg = towersBoard.board[toPeg]; | ||
|
||
// Make sure that the move is valid (smaller disc on top of larger disc) | ||
if (destinationPeg.length === 0 || disc < destinationPeg[destinationPeg.length - 1]) { |
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.
you are missing validation for moving a disc from a peg X to the same X, that is an invalid move
function displayBoard() { | ||
towersBoard.board.myForEach(function(peg) { | ||
console.log(peg); | ||
}); | ||
}; |
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.
function displayBoard() { | |
towersBoard.board.myForEach(function(peg) { | |
console.log(peg); | |
}); | |
}; | |
function displayBoard() { | |
towersBoard.board.forEach((peg) => console.log(peg)); | |
} |
}; | ||
}; | ||
function checkWinner() { | ||
if (towersBoard.board[1, 2].length === 5) { |
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 is not really scalable nor dynamic, if we wanted to add a new peg, will need a lot of refactor.
function isWinningTower(tower) {
return tower.length === 5 && tower.every((disc, index) => disc === 5 - index);
}
had a lot of fun and also a little bit of trouble with this Eval. My mentor was a huge help with pushing me over the hump! had a little extra fun at the end trying to create a cheat code "cheatCode()" executable in the chrome console.