I am trying to make a Connect Four game with 6 rows and 7 columns. The board is a String 2D array with each empty slot on the board being a ". " String. If I adjust the loops any more the program will give me an indexArrayOutOfBoundsException, so I can't figure out what changes need to be made to the algorithm to check a diagonal win.
//tests for an upper-left-to-lower-right diagonal line made by four of the same color checker
for(int row = 0; row <= LOWEST_ROW_INDEX - 3; row++)
{
for(int col = 0; col <= RIGHTMOST_COLUMN_INDEX - 3; col++)
{
if((gb[row][col] != ". ")
&& (gb[row + 1][col + 1] != ". ")
&& (gb[row + 2][col + 2] != ". ")
&& (gb[row + 3][col + 3] != ". ")
&& (gb[row][col] == gb[row + 1][col + 1])
&& (gb[row + 1][col + 1] == gb[row + 2][col + 2])
&& (gb[row + 2][col + 2] == gb[row + 3][col + 3]))
{
return gb[row][col];
}
}
}
//tests for a lower-left-to-upper-right diagonal line made by four of the same color checker
for(int row = 0; row <= LOWEST_ROW_INDEX - 3; row++)
{
for(int col = RIGHTMOST_COLUMN_INDEX - 3; col >= 0; col--)
{
if((gb[row][col] != ". ")
&& (gb[row + 1][col + 1] != ". ")
&& (gb[row + 2][col + 2] != ". ")
&& (gb[row + 3][col + 3] != ". ")
&& (gb[row][col] == gb[row + 1][col + 1])
&& (gb[row + 1][col + 1] == gb[row + 2][col + 2])
&& (gb[row + 2][col + 2] == gb[row + 3][col + 3]))
{
return gb[row][col];
}
}
}