Jos Fernando Valenzuela Jos Fernando Valenzuela - 7 months ago 10
Java Question

How to compare all the buttons? Or check if all are disabled?

I'm trying to do a memory game using "cards" all works fine but I need to know when the user win the game to display a message but I can't figure out how to do that? How can I check using a condition if all the buttons are

.setEnabled(false)
? Or should I compare all the buttons using a loop?
I hope you can help me.

Answer

Since you have cartas as an array. You should just iterate through a loop to see if all cards are set to false.

for(int x =0; x< cartas.length;x++)
{
      if(cartas[x].isEnabled()) //enabled
      {
         break; //a button is still enabled so cancel loop
      }
      else if(x == cartas.length-1&& !cartas[x].isEnabled())
       {
            //All cards have been disabled. Do Something.
       } 
}

Therefore, the method

isEnabled();

allows you to check if all buttons are

.setEnabled(false);