Skip to content

Instantly share code, notes, and snippets.

@variux
Created April 28, 2022 17:56
Show Gist options
  • Save variux/cfa70729d2e97c9b65025e4e8ef34ff5 to your computer and use it in GitHub Desktop.
Save variux/cfa70729d2e97c9b65025e4e8ef34ff5 to your computer and use it in GitHub Desktop.
package automat;
import java.util.Random;
public class Cell {
public char cell_type; // attribute
public static void main(String[] args) {
}
public void change(char[][] field) {
// conditions for changing of cell behavior
// x + y = w
// D (1, w) --- if D <= X --- A, else B
int[][] prueba = new int[5][5];
for (int i = 0; i < 5; i++) { // recorrer la matriz horizontal
for (int j = 0; j < 5; j++) { // recorrer la matriz vertical
// verifying the existence of neighbors
int blue_cells = 0;
int yellow_cells = 0;
int w = 0;
if (field[i][j] != 'C' || field[i][j] != 'D') {
for (int x = -1; x <= 1; x++) { // horizontal position
for (int y = -1; y <= 1; y++) { // vertical position
if ((i + x >= 0 && i + x < 5) && (j + y >= 0 && j + y < 5)) {
if (field[i + x][j + y] == 'A') { // verificar que tipo
// de vecino es "blue"
blue_cells++;
}
if (field[i + x][j + y] == 'B') { // verificar que tipo
// de vecino es
// "yellow"
yellow_cells++;
}
}
}
}
w = blue_cells + yellow_cells; // w = x + y
if (w > 0) {
Random rand = new Random();
int D = rand.nextInt((w - 1) + 1) + 1; // creating a random number
// between 1:w
prueba[i][j] = D;
if (D <= blue_cells) { // condicion si D <= x, x= blue_cells
if (field[i][j] == 'A') { // cambio de A a B
field[i][j] = 'B';
} else if (field[i][j] == 'B') { // cambio de B a A
field[i][j] = 'A';
}
}
}
}
}
System.out.println("Второе поколение");
for (int x = 0; x < field.length; x++) {
System.out.print("|");
for (int y = 0; y < field[x].length; y++) {
System.out.print(field[x][y]);
if (y != field[x].length - 1)
System.out.print("\t");
}
System.out.println("|");
}
System.out.println("Testing random coeficients");
for (int x = 0; x < prueba.length; x++) {
System.out.print("|");
for (int y = 0; y < prueba[x].length; y++) {
System.out.print(prueba[x][y]);
if (y != prueba[x].length - 1)
System.out.print("\t");
}
System.out.println("|");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment