/* * Paul Garrett, 29 Sept 98 * * To simulate Spatial Ecology [sic] evolution of populations * generalizing "game of life" type things * * ... but maybe different model ....? * * For now, wrap-around geometry... * * Try three different populations... */ import java.awt.*; import java.util.*; public class Life1 extends java.applet.Applet implements Runnable { boolean running = false; Stack newly_drawn = new Stack(); boolean drawing = false; final int cell_size = 8; // in pixels: should be divisor of width and height final int cell_padding = 0; // interstices thickness... int nwx = 0; // current nw corner of box to paint int ix = 0; // current i-index in array int nwy = 0; // current nw corner of box to paint int iy = 0; // current j-index in array final int big_i_inc = 7; // horizontal increment in outer loop final int big_j_inc = 11; // vertical increment in outer loop final int small_i_inc = 13; // horizontal increment in outer loop final int small_j_inc = 17; // vertical increment in outer loop final int big_x_inc = big_i_inc * cell_size; final int big_y_inc = big_j_inc * cell_size; final int small_x_inc = small_i_inc * cell_size; // within paint() inner loop final int small_y_inc = small_j_inc * cell_size; // within paint() inner loop final static int width = 536; // of whole picture final static int height = 320; // of whole picture final int horiz = width/cell_size; // number of cells horizontally final int vert = height/cell_size; // number of cells vertically final int number_of_cells = horiz * vert; // number of cells to color in a single paint() int tick_length = 5 * horiz; // should be multiple of "horiz" final int number_of_populations = 7; // including the "void" // transition probabilities final double[] death_cutoff = {.98, .97, .99, .995, .95, .9995}; final double[] birth_cutoff = {.08, .11, .06, .04, .15, .02}; Thread th; // to run the animation int time = 0; // counts generations int[][] states = new int[horiz][vert]; // states of cells // colors to draw populations final Color[] colors = {Color.gray, new Color(0xff0000), new Color(0x00ff00), new Color(0x0000ff), new Color(0x00ffff), new Color(0xffff00), new Color(0xff00ff)}; Random r = new Random(0); // object to generate transitions // tells which color/population it will draw in int mouseState = 0; // clicking should change it... /**********************************************************************/ final public static void main(String[] argv) { Frame fr = new Frame(); fr.addNotify(); fr.setLayout(null); // Insets insets = fr.getInsets(); Insets insets = fr.insets(); // 1.0 api // fr.setSize(insets.left+insets.right+width, insets.top+insets.bottom+height); fr.resize(insets.left+insets.right+width, insets.top+insets.bottom+height); fr.show(); fr.setBackground(new Color(0x999999)); Life1 se = new Life1(); fr.add(se); se.init(); // have to initialize some stuff before drawing it... // se.setLocation(insets.left, insets.top); // 1.1 api se.move(insets.left, insets.top); // 1.0 api se.resize(width, height); // 1.0 api } /**********************************************************************/ final public void give_birth(int i, int j, int pop) { // randomly choose in counter-clockwise, starting at E position... int n = ((r.nextInt() % 4) + 4) % 4; switch (n) { case 0: // east if ( states[(i+1) % horiz][j] == 0 ) { states[(i+1) % horiz][j] = pop; break; } else { n++; } case 1: // north if ( states[i][(j-1+vert) % vert] == 0 ) { states[i][(j-1+vert) % vert] = pop; break; } else { n++; } case 2: // west if ( states[(i-1+horiz) % horiz][j] == 0 ) { states[(i-1+horiz) % horiz][j] = pop; break; } else { n++; } case 3: // south if ( states[i][(j+1) % vert] == 0 ) { states[i][(j+1) % vert] = pop; break; } } } final public void generation() { for (int i=0; i 0 ) { double rand = r.nextDouble(); if ( rand > death_cutoff[k-1] ) { // death case states[i][j] = 0; } else if ( rand < birth_cutoff[k-1] ) { // birth case give_birth(i,j,k); } else { // do nothing } } } } } /**********************************************************************/ public void init() { for (int i=0; i 0 && x < width && y > 0 && y < height ) { int i = x/cell_size; int j = y/cell_size; states[i][j] = mouseState; int[] indices = {i, j}; newly_drawn.push( indices ); drawing = true; repaint(); } return true; } public boolean mouseDown(Event e, int x, int y) { mouseState = (mouseState + 1) % number_of_populations; return true; } public boolean keyDown(Event e, int k) { if ( k == 103 ) { repaint(); running = true; _start(); } else if ( k == 115 ) { running = false; _stop(); repaint(); } return true; } } // end of class Life1