I have a 3x3 two-dimensional array TicTac toe game, and I am having trouble in a method that should return the winner. The number 1 was assigned to player 1 and -1 to player 2, I need to sum the lines, the columns and the diagonals to see if there is a winner.
public static int checkWinner (int[][] board) {
for(int i=0; i<3;i++) {
for(int j=0;j<3;j++) {
if(board[i][j]+ board[i][j]+ board[i][j] == 3) {
return 1;
} else if (board[i][j] + board[i][j] + board[i][j] == -3) {
return -1;
}
}
}
return -10; //placeholder
}
How do I do this, and in a way that is scalable (if I change let's say to a 4x4 grid).
edit: I already saw Algorithm for Determining Tic Tac Toe Game Over, but didn't understand it. That's why I made this question. If anyone can explain what I doing wrong instead of downvoting, I would be very grateful, and would actually learn something instead of copying.