Method in java does not work

0

I have a method in which I go through a char array, what I need is to go through the array and when I find the character I'm looking for, add a counter, the method enters but the condition of when the counter has a certain number is not met print a message and exit the method.

Here is my code:

public class Tablero {

 public static void validarColumna(char array[][]) {
            int contx = 0;
            int conty = 0;

        for (int i = 0; i < 6; i++) {

            for (int j = 0; j < 7; j++) {
                if (array[i][j] == 'X') {

                    contx++;
                    conty =0;
                    if (contx == 4) {
                        System.out.println("Linea!! Ganador Jugador 1");
                        return;
                    }
                }else if (array[i][j] == 'Y') {

                    conty++;
                    contx=0;
                    if (conty == 4) {
                        System.out.println("Linea!! Ganador Jugador 1");
                        return;
                    }
                }
            }
        }
}
    
asked by Maxi Hernandez 21.10.2017 в 16:32
source

1 answer

0

I would recommend leaving the code as follows and below I will try to explain it to my way of seeing:

public class Tablero {

public static void validarColumna(char array[][]) {
        int contx = 0;
        int conty = 0;

    for (int i = 0; i < 6; i++) {

        for (int j = 0; j < 7; j++) {
            if (array[i][j] == 'X') {

                contx++;
                if (contx == 4) {
                    System.out.println("Linea!! Ganador Jugador 1");
                    return;
                }
            }else if (array[i][j] == 'Y') {

                conty++;
                if (conty == 4) {
                    System.out.println("Linea!! Ganador Jugador 1");
                    return;
                }
            }
        }
    }
}

I have eliminated the lines of      contx=0;      conty=0;

Because with these lines what you are doing is that each time this player makes a point the other is set to zero, that is to win a player has to make 4 points in a row without the other score, since each Once you do the contx=0 or conty=0 you reset the scores without having finished the method.

as for the return you can use a break if you prefer, but it would be the same since it is a function of type void and will never return anything, it is as you prefer.

    
answered by 21.10.2017 в 21:53