How do I make it so that it only displays the Game Over panel after all buttons are disabled?
public void actionPerformed(ActionEvent e)
{
boolean hasProblemsleft = true;
Object source = e.getSource();
if(source == quit)
{
cards.show(c, "Introduction");
for(int row = 0; row < 5; row++)
for(int col = 0; col < 5; col++)
buttons[row][col].setEnabled(true);
}
for(int row = 0; row < 5; row++)
for(int col = 0; col < 5; col++)
{
if(source == buttons[row][col])
{
开发者_JS百科 questions.showTimer(row, col);
buttons[row][col].setEnabled(false); // disables button after user / //clicks on it
}
else if(buttons[row][col].isEnabled())
{
hasProblemsleft = false;
}
}
if(hasProblemsleft)
{
cards.show(c, "Game Over!");
}
}
Use a separate set of nested for loops (after the nested loops above) to check if every button is disabled, and if so, display your game over panel.
edit 1:
You use one set of nested loops to disable the button after pressing it, and the second set to check of all the buttons are disabled. e.g.,
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if (source == quit) {
cards.show(c, "Introduction");
for (int row = 0; row < 5; row++)
for (int col = 0; col < 5; col++)
buttons[row][col].setEnabled(true);
} else { // don't forget this important else!
for (int row = 0; row < 5; row++) { // use curly braces for *ALL*
// loops/blocks
for (int col = 0; col < 5; col++) {
if (source == buttons[row][col]) {
questions.showTimer(row, col);
buttons[row][col].setEnabled(false);
}
}
}
boolean done = true;
for (int row = 0; row < 5; row++) {
for (int col = 0; col < 5; col++) {
if (buttons[row][col].isEnabled()) {
done = false;
break;
}
}
}
if (done) {
cards.show(c, "Game Over!");
}
}
}
精彩评论