//
// Discrete Time CA using probabilities and unit time
//
import java.util.*;
import java.io.*;
import java.awt.*;

public class CAP extends CA {

    public CAP(int n,int m,boolean draw){
	super(n,m,draw);
    }
    //
    // n x n grid with m species
    //

    void doBirths(int[][]X,int[][]Y){
	int species = -1;
	for (int i=0;i<n;i++)
	    for (int j=0;j<n;j++){
		species = X[i][j];
		if (species != empty && gen.nextDouble() <= pBirth[species])
		    birth(species,Y,i,j);
	    }
    }
    //
    // do births in X onto Y
    //

    public void doGeneration(){
	copy(A,B);
	doBirths(A,B);
	doDeaths(A,B);
	copy(B,A);
	if (draw){plot(); pause(100);}
	time = time + 1.0;
	show();
    }

    public static void main(String[] args) {

	int n        = Integer.parseInt(args[0]);          // size of grid
	int maxTime  = Integer.parseInt(args[1]);          // number of iterations
	int m        = (args.length - 1)/3;                // m species
	boolean draw = args[args.length-1].equals("draw"); // do we draw?
	CAP cap      = new CAP(n,m,draw);

	for (int i=1;i<=m;i++){
	    int species = i-1;
	    int pop  = Integer.parseInt(args[3*i-1]);
	    cap.setPBirth(species,Double.parseDouble(args[3*i+0]));
	    cap.setPDeath(species,Double.parseDouble(args[3*i+1]));
	    for (int j=0;j<pop;j++) cap.add(species);
	}

	cap.show();
	while (cap.time < maxTime) cap.doGeneration();
	System.exit(0);
    }
}