Home Button.setEnabled doesn't work in the first loop
Reply: 0

Button.setEnabled doesn't work in the first loop

D.Weltrowski
1#
D.Weltrowski Published in 2018-01-13 03:27:03Z

I am working on a little game which I am coding using JFrame. I created a JPanel in which I have a board created with JButtons. I had to check if any letter is the same in a column/line and diagonally. So I created a method which does that for me. The problem is the first statement I put in method won't disable the button even though the program actually reaches that part of code. So I tried a few things to fix it and I just don't understand why the first loop will not disable the Button while following loops will.

 private void checkRepetitions()
{

    Boolean check = false;
    int i, j;
    //check in line
    i = lastI;

        for (j = 0; j < 8; j++) {
            if (tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != j && !tab[lastI][lastJ].getText().equals("")) {
                confirm.setEnabled(false);
                check = true;
                break;
            } else {
                confirm.setEnabled(true);

            }
        }


    //checkin column
    j = lastJ;

        for (i=0; i<8; i++){
            if(tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != i && !tab[lastI][lastJ].getText().equals("")){
                confirm.setEnabled(false);
                check = true;
                break;
            }
            else{
                confirm.setEnabled(true);
            }
        }


    //scheck diagonally
    i = 0;
    j = 0;

    if (lastJ == lastI) {
        while (i<7 && j<7) {

            if(tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != i && !tab[lastI][lastJ].getText().equals("")){
                confirm.setEnabled(false);
                check = true;
                break;
            }

            else if(i<8&& j<8){
                i++;
                j++;
            }

            else{
                confirm.setEnabled(true);
            }
        }
    }
    //check diagonally
    i = 0;
    j = 7;
    if (lastI + lastJ == 7) {
        while (true){

            if(tab[lastI][lastJ].getText().equals(tab[i][j].getText()) && lastI != i && lastJ != j && !tab[lastI][lastJ].getText().equals("")){
                confirm.setEnabled(false);
                check = true;
                break;
            }
            else{
                confirm.setEnabled(true);
            }
            if (i == 7 && j == 0){
                break;
            }
            else{
                i++;
                j--;
            }


        }
    }

}

lastI and lastJ are variables that holds the position of the last button user clicked on and for that button I check then if there are repetitions. confirm button is supposed to get disabled and it does with every loop but the first for loop. Boolean is not used right now so it doesn't impact the code.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.433754 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO