Compare several elements of a matrix

4

I would like to know if you could help me with this little problem that I have.

I'm doing a basic variation of the game tetris in console as part of a task, I'm quite new to Java, I just started and I was stuck in the system of punctuation / elimination of matches.

I have a matrix full of spaces (game board) that is filled with the figures of tetris, then, at a certain moment of the game I have:

As you can see, I have 4 similar elements that should have been eliminated. The only solution I can think of is to go through the whole matrix and check if there are 4 equal characters in x column, eliminate them and add points ... of this way:

for (int y=1;y<Tablero.length-1;y++){

            for(int x=1;x<Tablero[y].length-4;x++){
                if(Tablero[y][x].equals(Tablero[y][x+1])&&Tablero[y][x+1].equals(Tablero[y][x+2])&&Tablero[y][x].equals(Tablero[y][x+3])&&Tablero[y][x]!=" "){
                    puntos=puntos+1;
                    Tablero[y][x]=" ";
                    Tablero[y][x+1]=" ";
                    Tablero[y][x+2]=" ";
                    Tablero[y][x+3]=" ";
                }
            }

But this solution, besides being quite ugly, only applies to the specific case of 4 equals, if there are more than 4, it does not take them into account.

So in itself, the question would be: How can I go through a matrix and check if 4 or more elements are the same?

Thank you in advance for any suggestions

    
asked by Juan Antonio 03.03.2017 в 05:24
source

1 answer

3

Taking the code to put and this question:

So in itself, the question would be: How can I go through an array and check if 4 or more elements are the same?

is already checking the four now just follow.

for (int y=1;y<Tablero.length-1;y++){

            for(int x=1;x<Tablero[y].length-4;x++){ // podria ser que fuera Tablero[x]?

                if(Tablero[y][x].equals(Tablero[y][x+1]) &&
                   Tablero[y][x+1].equals(Tablero[y][x+2]) &&
                   Tablero[y][x].equals(Tablero[y][x+3]) &&
                   Tablero[y][x]!=" "){

                    puntos=puntos+1;

                    Tablero[y][x]=" ";
                    Tablero[y][x+1]=" ";
                    Tablero[y][x+2]=" ";
                    Tablero[y][x+3]=" ";

                    // 18 creo mide su pantalla a ojo de buen cubero
                    // 18 - 4 ajustar esto

                    for (int a = 1; a < (18 - 4); a++){

                        if(Tablero[y][x+3 + a] != null){ /*este null para que no 
                                                         este fuera del array 
                                                         en cuanto exista un null esta fuera de la pantalla asi que puede poner un else, para que deje de mirar en el for. algo asi como else{ a = 20;*/

                            if(Tablero[y][x].equals(Tablero[y][x+3 + a]){

                                Tablero[y][x+3 + a] = " ";
                                //no se si se tendria que sumar mas puntos por cada uno de mas, de ser asi podria hacerlo aqui

                            }else{


                            //en el momento que deja de ser igual salimos
                            a = 20;
                            }

                        }else{

                            //salimos
                            a = 20;
                        }
                    }
                }
            }

This is an idea, I have not been able to prove it, but I think it is very likely to work for you.

    
answered by 03.03.2017 / 11:41
source