Conversation
02week/ticTacToe.js
Outdated
|
|
||
| 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() !== ''); |
There was a problem hiding this comment.
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++
There was a problem hiding this comment.
Yea, sure. no prob.
There was a problem hiding this comment.
Piero, I made this change. I am using a turnCount instead.
| @@ -73,18 +73,21 @@ function ticTacToe(row, column) { | |||
| if (!board[row][column].trim() ) { // This test makes sure the square is empty. | |||
There was a problem hiding this comment.
Great use of a conditional on line 73!
02week/ticTacToe.js
Outdated
| } else { | ||
| // Ok no one has won yet. | ||
| // this logic controls who's turn it is. | ||
| if (playerTurn === 'X') { |
There was a problem hiding this comment.
Shorten lines 86-90 to 1 line using a ternary operator.
There was a problem hiding this comment.
Did you ever get around to doing this? Not seeing it in your pull requests..
There was a problem hiding this comment.
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'); |
|
|
||
| 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); |
…ay to check for tie.
| } else { | ||
| playerTurn = 'X'; | ||
| } | ||
| playerTurn === 'X'? playerTurn = 'O' : playerTurn = 'X'; |
There was a problem hiding this comment.
You could also write it like this:
playerTurn = (playerTurn === 'X') ? 'O' : 'X';
But the one you wrote should work fine.

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