-1

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.

Community
  • 1
  • 1
Rafa_Asp1
  • 89
  • 2
  • 8
  • 1
    Try looking at: http://stackoverflow.com/questions/1056316/algorithm-for-determining-tic-tac-toe-game-over – sshashank124 May 14 '16 at 03:04
  • @sshashank124 I already did. But didn't understand it. – Rafa_Asp1 May 14 '16 at 03:05
  • The way they've done it (and I prefer that approach) is that they pass in the coordinates for the newly added token. That way, they only have to check the possible rows and columns that contain that location. This is better than checking the whole board everytime – sshashank124 May 14 '16 at 03:07
  • So, which part of this code is summing a row? A column? A diagonal? All I see is a calculation of `3 * cell value` (written as `val + val + val`, but that's just long for `3 * val`). – Andreas May 14 '16 at 03:39

2 Answers2

1

Assuming that board contains 0's in spaces that haven't been played in yet, and you have a square board, this should work on any size board. I changed your method to return 0 if there was a tie instead of -10. Also note, this is not the most efficient implementation, but it is good. You could add counters and only use one forloop for a runtime of BigTheta(n) but this way is less confusing and BigTheta(n^2) which is any reasonable game of tick-tack-toe (meaning any game a human would ever finish) is more than fast enough.

/**
 * Checks the board to see if there is a winner
 *
 * @param board The board to check
 * @return Returns (1 if player1 wins) (-1 if player2 wins) and (0 if no one won)
 */
public static int checkWinner (int[][] board) {
    int row, col, diagDown=0, diagUp=0;
    for(int i=0; i<board[0].length; i++) { 
        row=0;
        col=0;
        for(int j=0; j<board.length; j++) {
            row+=board[i][j];
            col+=board[j][i];
        }
        //Player1 wins!
        if(row==board.length||col==board.length)
            return 1;
        //Player2 wins!
        if(row==-1*board.length||col==-1*board.length)
            return -1;

        diagDown+=board[i][i];
        diagUp+=board[i][board.length-1-i];
    }
    //Player1 wins!
    if(diagUp==board.length||diagDown==board.length)
        return 1;
    //Player2 wins!
    if(diagUp==-1*board.length||diagDown==-1*board.length)
        return -1;
    //No winner
    return 0;
}
Sage Smith
  • 441
  • 3
  • 10
0
public class TicTacToe {

    enum State {
        NO_WINNER, WINNER_1, WINNER_2
    };

    public static State checkWinner(int[][] board) {

        // Check LINES
        for (int i = 0; i < board.length; i++) { // for each line
            int sum = 0;
            for (int j = 0; j < board[i].length; j++) {
                sum += board[i][j];
            }
            if (sum == board.length) {
                return State.WINNER_1;
            }
            if (sum == -board.length) {
                return State.WINNER_2;
            }
        }

        // Check COLUMNS
        for (int i = 0; i < board.length; i++) { // for each column
            int sum = 0;
            for (int j = 0; j < board.length; j++) {
                sum += board[j][i];
            }
            if (sum == board.length) {
                return State.WINNER_1;
            }
            if (sum == -board.length) {
                return State.WINNER_2;
            }
        }

        // Check first DIAGONAL
        int sum = 0;
        for (int i = 0; i < board.length; i++) {
            sum += board[i][i];
            if (sum == board.length) {
                return State.WINNER_1;
            }
            if (sum == -board.length) {
                return State.WINNER_2;
            }
        }

        // Check second DIAGONAL
        sum = 0;
        for (int i = 0; i < board.length; i++) {
            sum += board[i][board.length - i - 1];
            if (sum == board.length) {
                return State.WINNER_1;
            }
            if (sum == -board.length) {
                return State.WINNER_2;
            }
        }

        return State.NO_WINNER;

    }

    public static void main(String[] args) {
        System.out.println(checkWinner(new int[][] { { 1, 1, 1 }, { 0, 0, 0 },{ 0, 0, 0 } }));
        System.out.println(checkWinner(new int[][] { { -1, 0, 0 },{ -1, 0, 0 }, { -1, 0, 0 } }));
        System.out.println(checkWinner(new int[][] { { 1, 0, 0 }, { 0, 1, 0 },{ 0, 0, 1 } }));
        System.out.println(checkWinner(new int[][] { { 0, 0, -1 }, { 0, -1, 0 },{ -1, 0, 0 } }));
        System.out.println(checkWinner(new int[][] { { 1, 0, 0 }, { 0, 0, 0 },{ 0, 0, 1 } }));
    }

}
Daniel Barral
  • 3,896
  • 2
  • 35
  • 47