0
我正在嘗試使用6行和7列進行Connect Four遊戲。該板是一個String 2D陣列,板上的每個空槽都是「。」字符串。如果我再調整循環,程序會給我一個indexArrayOutOfBoundsException,所以我無法弄清楚需要對算法進行哪些修改來檢查對角線的勝利。Java Connect Four遊戲無法識別對角線勝利
//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];
}
}
}