现在我正在创建一个 connect 4 游戏。我没有发布所有的 GUI 组件,因为它并不重要。游戏检测除底行之外的所有行的水平获胜。这是获胜检测的代码。
boolean CheckForWin()
{
for (int row = 1; row < gameBoard.length; row++) //Plus 1 is added to prepare for dimension swap.
{
//Player 1 horizontal count
int max=0;
//Player 2 horrizontal count
int max2=0;
int count_piece=0;
for(int column=1; column<gameBoard.length; column++)
{
// check for horizontal
if(row==6)
{
break;
}
if(count_piece<max || count_piece<max2)
{
count_piece=max;
count_piece=max2;
}
if(gameBoard[row][column]=='r')
{
max++;
}
else
{
max=0;
}
if(gameBoard[row][column]=='b')
{
max2++;
}
else
{
max2=0;
}
if(max==4 || max2==4)
{
return true;
}
// check for vertical
}
}
// check for diagonal up
// check for diagonal down
return false;
}