Problem checking for an array

Asked

Viewed 88 times

-2

I was doing a very simple old game, and the same done, I wanted to leave the automatic win check, because it was very "hard-code". The way I found to do this was by running a for, and whenever it reached the max-length of the array, it would change lines, but it’s not working as I predicted. Note: the "K" print was just to check if the if was working.

    /*Verificação de vitória em vertical*/
                int k = 0;
                vitoriaX = 0;
                for(int i = 0 ; i <= tabuleiro.length  ; i++) {



                    if(tabuleiro[i][k].equalsIgnoreCase("X")) {

                        vitoriaX++;

                        if(vitoriaX == tabuleiro.length) {
                            validacao = true;
                            i = 2 + (tabuleiro.length);
                            }   
                        }   
                    if(i == tabuleiro.length) {
                        i = 0;
                        k++;    
                        vitoriaX = 0;
                        System.out.print(k);

                    }
                }

1 answer

1


From what we can infer, your board is an array of strings (String[][]). And since it’s an old game, then it’s 3x3.

That is, it is an array of 3 elements, and each element is an array with 3 strings.

One of the problems with your code is that you’re not considering that arrays are indexed to zero. That is, the first position is 0, the second position is 1, etc. If your arrays have 3 elements, then the indices go from 0 to 2.

So do i <= tabuleiro.length in the for is wrong. The right thing would be i < tabuleiro.length, for length gives the array size (which in case is 3), then i can’t be 3, otherwise an error will occur when accessing this index.


Anyway, one way to check is to have an array of winning positions. Then just check that all elements of these positions are equal - and if they are, we have a winner.

Remembering that if the board is an array of arrays, the positions would be like this:

 (0, 0) | (0, 1) | (0, 2)
--------|--------|-------
 (1, 0) | (1, 1) | (1, 2)
--------|--------|-------
 (2, 0) | (2, 1) | (2, 2)

To know if someone won, I need to check the rows, columns and diagonals. It would look like this:

String[][] tabuleiro = {
    { "x", "o", "x" },
    { "x", "o", "o" },
    { "x", "", "" }
};
int[][][] posicoesVencedoras = {
  // horizontais
  { { 0, 0 }, { 0, 1 }, { 0, 2 } }, { { 1, 0 }, { 1, 1 }, { 1, 2 } }, { { 2, 0 }, { 2, 1 }, { 2, 2 } },
  // verticais
  { { 0, 0 }, { 1, 0 }, { 2, 0 } }, { { 0, 1 }, { 1, 1 }, { 2, 1 } }, { { 0, 2 }, { 1, 2 }, { 2, 2 } },
  // diagonais
  { { 0, 0 }, { 1, 1 }, { 2, 2 } }, { { 0, 2 }, { 1, 1 }, { 2, 0 } }
};
String vencedor = null;
for (int[][] posicoes : posicoesVencedoras) {
    int p1[] = posicoes[0], p2[] = posicoes[1], p3[] = posicoes[2];
    if (tabuleiro[p1[0]][p1[1]].equalsIgnoreCase(tabuleiro[p2[0]][p2[1]]) && tabuleiro[p1[0]][p1[1]].equalsIgnoreCase(tabuleiro[p3[0]][p3[1]])) {
        vencedor = tabuleiro[p1[0]][p1[1]];
        break; // se achou um vencedor, não precisa verificar o resto
    }
}
if (vencedor != null)
    System.out.println("Vencedor: " + vencedor);

The winning positions are an array, each element being another array, which in turn contains 3 arrays (and each of these contains the positions of the row and column to be checked).

For each of these combinations, I check if the 3 positions are equal. If they are, we have a winner.

  • Ah, got it. Thank you so much! It was very enlightening and intuitive your code.

  • @Junior3874 I updated the answer, there were some errors in some posicoesVencedoras (I’ve corrected the code)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.