我如何查看平局?我设法检查左右两个对角线以及水平和垂直。但我需要知道如何检查平局。我知道我需要检查所有按钮(在我的情况下按钮[6] [7])。
//Checking the Diagonal_right win
for (int i = 0; i < 6 ; i ++){
for (int j = 0; j < 7; j ++){
if ((i+1)==6 || (i+2)==6 || (i+3)==6){
break;
}
if(imageName[i][j] == singleimageName &&
imageName[i+1][j-1] == singleimageName &&
imageName[i+2][j-2] == singleimageName &&
imageName[i+3][j-3] == singleimageName){
JOptionPane.showMessageDialog(null,""
+ "You Have Won! \n"
+ "Player Using " + singleimageName +
" Connected 4");
clearGrid();
}
}
}
//Checking the Horizontal win
for (int i = 0; i < 6; i ++){
for (int j = 0; j < 7; j ++){
if ((i+1)==6 || (i+2)==6 || (i+3)==6){
break;
}
if(imageName[i][j] == singleimageName &&
imageName[i][j+1] == singleimageName &&
imageName[i][j+2] == singleimageName &&
imageName[i][j+3] == singleimageName) {
JOptionPane.showMessageDialog(null,""
+ "You Have Won! \n"
+ "Player Using " + singleimageName +
" Connected 4");
clearGrid();
}
}
}
//Checking the Vertical win
for (int i = 0; i < 6; i ++){
for (int j = 0; j < 7; j ++){
if ((i+1)==6 || (i+2)==6 || (i+3)==6){
break;
}
if(imageName[i][j] == singleimageName &&
imageName[i+1][j] == singleimageName &&
imageName[i+2][j] == singleimageName &&
imageName[i+3][j] == singleimageName) {
JOptionPane.showMessageDialog(null,""
+ "You Have Won! \n"
+ "Player Using " + singleimageName +
" Connected 4");
clearGrid();
}
}
}
答案 0 :(得分:0)
// Check for a draw
boolean draw = true;
for (int i = 0; i < 6; i ++) {
for (int j = 0; j < 7; j ++) {
if (imageName[i][j].getIcon() == null) {
draw = false;
}
if (draw) {
JOptionPane.showMessageDialog(null, "It's a draw!");
clearGrid();
}