# Go through adjacent positions of a matrix

3

I'm making a game that aims to fill all the squares of the same color (in this case the numbers will be the colors selected by the user who is playing, and in a matrix with dimensions of 9x9). The fact is that I have already done how to generate this random matrix:

``````public static void generar (int tamano , int colores, int tablero [][]){
for(int i=0; i<tamano; i++) {
for(int j=0; j<tamano; j++) {          // recorro la matriz
tablero[i][j]=(int)Math.floor(Math.random()*colores);
}
}

}
``````

Once the matrix is created, a color is requested and therefore if it is the same as in that box, it does not do anything, if it is different, it will change the value for the one entered and at the same time look at its adjacencies to know if they had the same initial value to exchange it for the selected color. Therefore if the color has been changed in said adjacency, check also their respective adjacencies. I have started in the position (x, y) = (1,1) which is the command to start and therefore the user does not enter the position to play.

``````public static void jugada (int tablero[][], int color,int tamano ,int colores){
int x= 1 ; int y = 1 ;
if (tablero [x][y] == color){
tablero [x][y] = color ;

}
else{
if (tablero [x][y] != color) {
adyacentes (tablero, color, x , y);
tablero [x][y] = color ;

}
}
}

public static void adyacentes (int tablero [][] , int color, int x , int y){

if (tablero [x+1][y] == color){
tablero [x+1][y] = color ;
}
else if (tablero [x+1][y] == tablero[x][y]){
tablero [x+1][y] = color ;

}
if (tablero [x-1][y] == color){
tablero [x-1][y] = color ;
}
else    if (tablero [x-1][y] == tablero[x][y]){
tablero [x-1][y] = color ;

}
if (tablero [x][y+1] == color){
tablero [x][y+1] = color ;
}
else    if (tablero [x][y+1] == tablero[x][y]){
tablero [x][y+1] = color ;
}

if (tablero [x][y-1] == color){
tablero [x][y-1] = color ;
}
else if (tablero [x][y-1] == tablero[x][y]){
tablero [x][y-1] = color ;
}
``````

My problem comes when I go through the adjacent boxes, by logical deduction I know that I have to go through the matrix with a loop but my results are not as expected. Could someone tell me what code would be like to look at the time to change the value in an adjacency their respective adjacencies?

asked by oxsaulxo 04.01.2017 в 14:12
source

4

What you want to do is a flood filled with adjacency in 4 directions. Given some coordinates (in the illustration is the center) and a color (orange in the illustration) you want to fill in that color all the points of the original color (white in the illustration) that are adjacent.

A typical algorithm for doing this is recursively. The `jugada` method only checks if the data point has a color different from the past as a parameter; if it is not like that, you do not have to do anything. If so, call `pintarRecursivo` who does all the work.

``````  public static void jugada (int tablero[][], int color,int tamano ,int colores){
int x= 1 ; int y = 1 ;
if (tablero [x][y] != color) {
int colorARellenar = tablero[x][y];
pintarRecursivo (tablero, color, colorARellenar, x , y);
}
}
``````

`pintarRecursivo` checks if it is in a point with color that matches the `colorARellenar` , if so, it changes the color and calls recursively for the 4 adjacent coordinates. Checking before we left the array.

``````  public static void pintarRecursivo (int tablero [][] , int color, int colorARellenar, int x , int y){
if ( x<0 || y<0 || x>=tablero.length || y>=tablero[x].length )
return;
if ( tablero[x][y]!=colorARellenar )
return;
tablero[x][y] = color;
pintarRecursivo( tablero, color, colorARellenar, x+1, y);
pintarRecursivo( tablero, color, colorARellenar, x, y+1);
pintarRecursivo( tablero, color, colorARellenar, x-1, y);
pintarRecursivo( tablero, color, colorARellenar, x, y-1);
}
``````