Java: How to check diagonal Connect Four win in 2D array
<p>I have a Connect Four "board" which is a 6*7 2D char array populated with either spaces, X or O. The win condition is met when there are either four Xs or four Os in a row vertically, horizontally or diagonally. I've managed to get the win conditions checked successfully for vertical and horizontal, where the winning char is returned by some methods as below:</p>

<pre><code>private char CheckVerticalWinner(char[][] currentBoard) {
    // check vertical (move down one row, same column)
    char vWinner = ' ';
    for (int col=0; col&lt;7; col++) {
        int vCount = 0;
        for (int row=0; row&lt;5; row++) {
            if (currentBoard[row][col]!=' ' &amp;&amp;
                currentBoard[row][col] == currentBoard[row+1][col]) {
                vCount++;
                System.out.println("VERT "+vCount); //test
            } else {
                vCount = 1;
            }

            if (vCount&gt;=4) {
                vWinner = currentBoard[row][col];
            }
        }
    }
    return vWinner;
}


private char CheckHorizontalWinner(char[][] currentBoard) {
    // check horizontal (move across one column, same row)
    char hWinner = ' ';
    for (int row=0; row&lt;6; row++) {
        int hCount = 0;
        for (int col=0; col&lt;6; col++) {
            if (currentBoard[row][col]!=' ' &amp;&amp;
                currentBoard[row][col] == currentBoard[row][col+1]) {
                hCount++;
                System.out.println("HORIZ "+hCount); //test
            } else {
                hCount = 1;
            }

            if (hCount&gt;= 4) {
                hWinner = currentBoard[row][col];
            }
        }
    }
    return hWinner;
}
</code></pre>

<p>I'm just stuck on how to check for diagonal wins, without throwing an ArrayIndexOutOfBoundsException. I know I need to iterate through the 2D array twice, once for forward diagonals and once for backward diagonals <strong>that are 4 squares long or more</strong>, like in the below diagram:</p>

<p><a href="http://i.stack.imgur.com/VPqPr.png" rel="nofollow">Diagonals to be checked diagram</a></p>

<p>Basically, how would I fill in this method to return a winning char?</p>

<pre><code>private char CheckDiagonalWinner(char[][] currentBoard) {

    // some iteration here

    return dWinner;
}
</code></pre>

<p>Any help would be much appreciated!</p>