Skip to content

Conversation

@copelandhouse2
Copy link
Owner

Here is the code for my Tic Tac Toe game. Cheers!


function checkForTie() {
return board.every(square => square.trim() !== '');
return board[0].every(square => square.trim() !== '') && board[1].every(square => square.trim() !== '') && board[2].every(square => square.trim() !== '');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the .every() js method but could you increment a moveCount variable and check the count here? This will prevent your app from having to loop through all your board logic twice every turn.

Hint: Only 9 valid moves can be made in the game before a tie is inevitable.

Ex:

\\ code

board[row][column] = playerTurn;
moveCount++

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, sure. no prob.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Piero, I made this change. I am using a turnCount instead.

}

if (validValue(row) && validValue(column)) { // This test makes sure values entered are 0, 1, 2 and nothing else.
if (!board[row][column].trim() ) { // This test makes sure the square is empty.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of a conditional on line 73!

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

} else {
// Ok no one has won yet.
// this logic controls who's turn it is.
if (playerTurn === 'X') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorten lines 86-90 to 1 line using a ternary operator.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you ever get around to doing this? Not seeing it in your pull requests..

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Piero, this is done. turn management now handled by ternary operator.

return true; // returning true ends the game. We have a winner.
}
} else {
console.log('Hey, that square is already filled in. Select another');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good error message.


function diagonalWin() {
// Your code here
return [board[0][0], board[1][1], board[2][2]].every(square => square === playerTurn) || [board[0][2], board[1][1], board[2][0]].every(square => square === playerTurn);
Copy link

@pieroapretto pieroapretto Jul 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of the .every() JavaScript method here.

nicely done

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate it!

} else {
playerTurn = 'X';
}
playerTurn === 'X'? playerTurn = 'O' : playerTurn = 'X';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also write it like this:

playerTurn = (playerTurn === 'X') ? 'O' : 'X';

But the one you wrote should work fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants