// // Discrete Time CA // import java.util.*; import java.io.*; import java.awt.*; public class DTCA { private int[][] A; private int[][] B; private int[] neighbourhood; // Moore neighbourhood private int n; // n X n grid, as a torus private int m; // m species private int totalPopulation; // sum of all populations of species private double pointSize; private boolean trace; private int[] population; // m population sizes private double[] pBirth; // m birth probabilities private double[] pDeath; // m death probabilities private RandomSet freeSpace; // initially all points on the grid private Random gen; private boolean draw; // true <-> plot private static int empty = -1; private static Color background = Color.LIGHT_GRAY; private static Color[] color = {Color.BLUE,Color.RED,Color.YELLOW,Color.GREEN,Color.CYAN,Color.MAGENTA}; public DTCA(int n,int m,boolean draw){ this.n = n; this.m = m; totalPopulation = 0; pointSize = 0.4; gen = new Random(); A = new int[n][n]; B = new int[n][n]; population = new int[m]; pBirth = new double[m]; pDeath = new double[m]; freeSpace = new RandomSet(n*n); gen = new Random(); this.draw = draw; neighbourhood = new int [9]; // see getNeighbourhood() for explanation for (int i=0;i 0){ int k = gen.nextInt(neighbourhood[0]) + 1; int y = neighbourhood[k]; X[y/n][y%n] = species; population[species]++; totalPopulation++; } } public void death(int species,int[][] X,int i,int j) { X[i][j] = empty; population[species]--; totalPopulation--; } private void getToroidalNeighbourhood(int[][]X,int x,int y){ int k = 0; int xt,yt; // x and y coords on torus for (int i=x-1;i<=x+1;i++) for (int j=y-1;j<=y+1;j++){ xt = i; if (i<0) xt = i + n; if (i>n-1) xt = n - i; yt = j; if (j<0) yt = j + n; if (j>n-1) yt = n - j; if (X[xt][yt] == empty){ k++; neighbourhood[k] = xt*n + yt; } } neighbourhood[0] = k; } public void plot() { StdDraw.clear(background); StdDraw.setXscale(-2,n); StdDraw.setYscale(-2,n); StdDraw.show(0); for (int i=0;i