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?