A 2-dimensional 3×3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of any row or column or diagonal are equal.

A 2-dimensional 3×3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of any row or column or diagonal are equal.

LANGUAGE: JAVA

CHALLENGE:

A 2-dimensional 3×3 array of ints, has been created and assigned to tictactoe. Write an expression whose value is true if the elements of any row or column or diagonal are equal.

SOLUTION:

((tictactoe[0][0] == tictactoe[0][1] && tictactoe[0][1] == tictactoe[0][2]) ||
(tictactoe[1][0] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[1][2]) ||
(tictactoe[2][0] == tictactoe[2][1] && tictactoe[2][1] == tictactoe[2][2]) ||
(tictactoe[0][0] == tictactoe[1][0] && tictactoe[1][0] == tictactoe[2][0]) ||
(tictactoe[0][1] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][1]) ||
(tictactoe[0][2] == tictactoe[1][2] && tictactoe[1][2] == tictactoe[2][2]) ||
(tictactoe[0][0] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][2]) ||
(tictactoe[0][2] == tictactoe[1][1] && tictactoe[1][1] == tictactoe[2][0]))

Add comment