mercredi 8 juin 2016

unsure what if-statements i need to have for when a player is forced to go to a won box

so when a player is forced to go to a box that has been won, i want to give them the ability to go to any box not yet won, but i was wonder if there was an easier way than just doing nine different if-statements every time i need that to be checked. right now this is what happens: http://ift.tt/1Y9bTlD and this is the code for the rules:

if(source.equals(a1) || source.equals(b1) || source.equals(c1) || 
     source.equals(d1) || source.equals(e1) || source.equals(f1) || 
     source.equals(g1) || source.equals(h1) || source.equals(i1))
       {
            b1.setEnabled(false);
            b2.setEnabled(false);
            b3.setEnabled(false);
            b4.setEnabled(false);
            b5.setEnabled(false);
            b6.setEnabled(false);
            b7.setEnabled(false);
            b8.setEnabled(false);
            b9.setEnabled(false);
            c1.setEnabled(false);
            c2.setEnabled(false);
            c3.setEnabled(false);
            c4.setEnabled(false);
            c5.setEnabled(false);
            c6.setEnabled(false);
            c7.setEnabled(false);
            c8.setEnabled(false);
            c9.setEnabled(false);
            d1.setEnabled(false);
            d2.setEnabled(false);
            d3.setEnabled(false);
            d4.setEnabled(false);
            d5.setEnabled(false);
            d6.setEnabled(false);
            d7.setEnabled(false);
            d8.setEnabled(false);
            d9.setEnabled(false);
            e1.setEnabled(false);
            e2.setEnabled(false);
            e3.setEnabled(false);
            e4.setEnabled(false);
            e5.setEnabled(false);
            e6.setEnabled(false);
            e7.setEnabled(false);
            e8.setEnabled(false);
            e9.setEnabled(false);
            f1.setEnabled(false);
            f2.setEnabled(false);
            f3.setEnabled(false);
            f4.setEnabled(false);
            f5.setEnabled(false);
            f6.setEnabled(false);
            f7.setEnabled(false);
            f8.setEnabled(false);
            f9.setEnabled(false);
            g1.setEnabled(false);
            g2.setEnabled(false);
            g3.setEnabled(false);
            g4.setEnabled(false);
            g5.setEnabled(false);
            g6.setEnabled(false);
            g7.setEnabled(false);
            g8.setEnabled(false);
            g9.setEnabled(false);
            h1.setEnabled(false);
            h2.setEnabled(false);
            h3.setEnabled(false);
            h4.setEnabled(false);
            h5.setEnabled(false);
            h6.setEnabled(false);
            h7.setEnabled(false);
            h8.setEnabled(false);
            h9.setEnabled(false);
            i1.setEnabled(false);
            i2.setEnabled(false);
            i3.setEnabled(false);
            i4.setEnabled(false);
            i5.setEnabled(false);
            i6.setEnabled(false);
            i7.setEnabled(false);
            i8.setEnabled(false);
            i9.setEnabled(false);
       }

nine times, one for each quadrant, and this is what happens when a button is pushed:

if("disable".equals(actionEvent.getActionCommand()))

   {
       if(PlayerOneTurn % 2 != 0)
       { 
           if(source.equals(a1))
           {
               if(input.equals("X") || (input.equals("x")))
               {
                   a1.setText("x");
                   a1.setActionCommand(" ");
               }
               else     
           {
                   a1.setText("o"); 
                   a1.setActionCommand(" ");
               }
           }
           if(source.equals(a2))
           {
               if(input.equals("X") || (input.equals("x")))
               {
                   a2.setText("x");
                   a2.setActionCommand(" ");
               }
               else
               {
                   a2.setText("o"); 
                   a2.setActionCommand(" ");
               }
           }
           if(source.equals(a3))
           {
               if(input.equals("X") || (input.equals("x")))
               {
                   a3.setText("x");
                   a3.setActionCommand(" ");
               }
               else
               {
                   a3.setText("o"); 
                   a3.setActionCommand(" ");
               }
           }
           if((a1.getText().equals("x") && a2.getText().equals("x") && a3.getText().equals("x")) 
                       || (a1.getText().equals("o") && a2.getText().equals("o") && a3.getText().equals("o")))
           {
               AFalse(true);
           }`

the AFalse() method is this:

public void AFalse(boolean PlayerOne){
    if(PlayerOne == true)
    {
        a1.setActionCommand(" ");
        a2.setActionCommand(" ");
        a3.setActionCommand(" ");
        a4.setActionCommand(" ");
        a5.setActionCommand(" ");
        a6.setActionCommand(" ");
        a7.setActionCommand(" ");
        a8.setActionCommand(" ");
        a9.setActionCommand(" ");
        winnerA = true;     
    }
    else
    {
        a1.setActionCommand(" ");
        a2.setActionCommand(" ");
        a3.setActionCommand(" ");
        a4.setActionCommand(" ");
        a5.setActionCommand(" ");
        a6.setActionCommand(" ");
        a7.setActionCommand(" ");   
        a8.setActionCommand(" ");
        a9.setActionCommand(" ");
        winnerA = false;    
    }   
}

Aucun commentaire:

Enregistrer un commentaire