HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Check if a game of Tic-Tac-Toe has a winner

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
toetictachasgamewinnercheck

Problem

I've implemented a Tic Tac Toe game with a public method checkWin() to check if the game has a winner. This is my code.

public class TicTacToe {
    int size;
    int [][] board;

    public TicTacToe(int size){
        this.size = size;
        board = new int[size][size];
        for (int i = 0; i = size || col >= size) {
            throw new IllegalStateException("Invalid grid point");
        }
        board[row][col] = 1;

    }

    public void markZero(int row, int col){
        if(row >= size || col >= size){
            throw new IllegalStateException("Invalid grid location");
        }
        board[row][col] = 0;
    }

    public boolean checkWin(){
       int countOnes = 0;
        int countZeros = 0;
        for (int i = 0; i = 0 ; i--, j--) {
            if (board[i][j] == 0) countZeros++;
            else if(board[i][j] == 1) countOnes++;
        }
        if(countOnes == size || countZeros == size) return true;

        return false;

    }
}

Solution

I would suggest this instead :

public boolean checkWin(){
    boolean diagLTRHasWinner = true;
    boolean diagRTLHasWinner = true;
    for(int j = 1; j < this.size; j++){
        boolean columHasWinner = true;
        boolean rowHasWinner = true;
        for(int i =0; i < this.size; i++){
            columHasWinner = columHasWinner && (board[i][j] == board[i][j-1]);
            rowHasWinner = rowHasWinner && (board[j][i] == board[j-1][i]);
        }
        if ((rowHasWinner && board[j][0] != -1) || (columHasWinner && board[0][j] != -1)){
            return true;
        }
        diagLTRHasWinner = diagLTRHasWinner  && (board[j][j] == board[j-1][j-1]);
        diagRTLHasWinner = diagLTRHasWinner  && (board[this.size - j][j] == board[this.size - j+1][j-1]);
    }
    return (diagRTLHasWinner && board[0][0] != -1) || (diagLTRHasWinner && board[this.size-1][0] != -1);
}


In order to reduce the number of loops, and objects allocated.
Besides the new code does not even count the element in the board, it just checks if we have a valid sequence of X or O, no matter which one, neither how many! It simply detects the end of the game !

Code Snippets

public boolean checkWin(){
    boolean diagLTRHasWinner = true;
    boolean diagRTLHasWinner = true;
    for(int j = 1; j < this.size; j++){
        boolean columHasWinner = true;
        boolean rowHasWinner = true;
        for(int i =0; i < this.size; i++){
            columHasWinner = columHasWinner && (board[i][j] == board[i][j-1]);
            rowHasWinner = rowHasWinner && (board[j][i] == board[j-1][i]);
        }
        if ((rowHasWinner && board[j][0] != -1) || (columHasWinner && board[0][j] != -1)){
            return true;
        }
        diagLTRHasWinner = diagLTRHasWinner  && (board[j][j] == board[j-1][j-1]);
        diagRTLHasWinner = diagLTRHasWinner  && (board[this.size - j][j] == board[this.size - j+1][j-1]);
    }
    return (diagRTLHasWinner && board[0][0] != -1) || (diagLTRHasWinner && board[this.size-1][0] != -1);
}

Context

StackExchange Code Review Q#116830, answer score: 2

Revisions (0)

No revisions yet.