今、私はスイングでコネクト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;
}