Better methods for vertical and diagonal win in ticTacToe?


In ticTacToe.js, I got verticalWin() and diagonalWin() working using something like:

let verticalWin = () => {
(board[0][0] == playerTurn && board[1][0] == playerTurn && board[2][0] == playerTurn)
(board[0][1] == playerTurn && board[1][1] == playerTurn && board[2][1] == playerTurn)
(board[0][2] == playerTurn && board[1][2] == playerTurn && board[2][2] == playerTurn)
) {
    return true;
  else return false;

i.e. this function explicitly checks for each vertical win state using a bunch of logical operators. It does work; however it seems kind of clunky/brute forced. I’m curious if anyone is pursuing a better or more streamlined solution!

1 Like

I have not, my method isn’t working at all. I am also looking for an answer/help here! Thanks for the post!


Sorry for posting so late, but I got the verticalWin() by using a loop nested inside of a loop, e.g.

for i between 0-3:
—for j between 0-3:
------do something based on board[j][i]

So, for vertical wins, you want [0][i], [1][i], [2][i] to match. It can take some trial and error to visualize it, but I think it’s more elegant than writing out each situation individually. Hope this helps!