-
Notifications
You must be signed in to change notification settings - Fork 0
Week2b tic tac toe #4
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?
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.
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++
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.
Yea, sure. no prob.
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.
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. |
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.
Great use of a conditional on line 73!
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.
Thanks!
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.
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.
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.
Ok, will do.
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.
Did you ever get around to doing this? Not seeing it in your pull requests..
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.
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'); |
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.
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); |
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.
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.
Appreciate it!
…ay to check for tie.
| } else { | ||
| playerTurn = 'X'; | ||
| } | ||
| playerTurn === 'X'? playerTurn = 'O' : playerTurn = 'X'; |
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 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!