Check if a game of Tic-Tac-Toe has a winner 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 &lt;size ; i++) {
            for (int j = 0; j &lt;size ; j++) {
                board[i][j] = -1;
            }
        }

    }

    public void markCross(int row, int col){
        if(row &gt;= size || col &gt;= size) {
            throw new IllegalStateException("Invalid grid point");
        }
        board[row][col] = 1;

    }

    public void markZero(int row, int col){
        if(row &gt;= size || col &gt;= 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 &lt; size; i++) {
            for (int j = 0; j &lt; size; j++) {

                if(board[i][j] == 0) countZeros++;
                else if(board[i][j] == 1)countOnes++;
            }
            if(countZeros == size || countOnes == size) return true;
            break;
        }
        countOnes = 0;
        countZeros = 0;

        //check diagonal right

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

        //check diagonal left
        for (int i = size-1, j= size-1; i &gt;= 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;


    }
}
I would suggest this instead :

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