/*	evo4.java  evolution with reproduction, crossover, mutation*/import java.awt.*;import java.applet.Applet;import java.util.Date;import java.awt.event.*;import java.awt.geom.*;import java.lang.Math.*;import java.lang.StringBuffer;import Randomizer;import evoOrganism;import simpleOrg;public class evo6 extends Applet 			implements MouseListener, MouseMotionListener, Runnable { public static evo6 instance;	//for mouse dragging:boolean moving = false;int mousedown;int mouseup;	//for double buffering:Dimension size;Image buffer;Graphics bufferGraphics;Thread animator;boolean please_stop;	//randomizer with time-based seed:Date time = new Date();long seed = time.getTime();Randomizer r = new Randomizer(seed);	//global colors:int co = 0;Color c3 = new Color(235, 240, 230);Color c4 = new Color(168, 168, 139);		Color grey = new Color(196, 196, 196);Color white = new Color(255, 255, 255);Color lime = new Color(209, 245, 99);	Color block = new Color(117, 117, 95);Color band = new Color(246, 246, 204);Color bound = new Color(101, 101, 75);	//pause for runtime:int pause_factor = 15;int special_clock = 0;	//general framework for evoltion:int number_living = 0;int number_simple = 0;		//specific variables for applet:evoOrganism[] burp = new evoOrganism[300];simpleOrg[] sim = new simpleOrg[300];int temp_x, temp_y, temp_size, temp_mob, temp_col, temp_amp;int temp_width, temp_height;int initial_blocks = 12;//int initial_barriers = 3;//int initial_attract = 2;int bX, bY, bD, bW, bH, bA, bM, bL, bS;dataSpace data = new dataSpace();	//testing and expanding barriers:boolean open_space;boolean expanding;int which_simple;	//selection of mates:int temp_sexy = 0;int possible_mates = 0;int sexy_one = 0;int sexy_two = 0;boolean mating = false;int[] best = new int[2];int[] second_best = new int[2];int male;int female;int danger_x, danger_y, dense_x, dense_y;	//genetic reproduction:int number_genes = 6;int[] male_genes = new int[6];int[] female_genes = new int[6];int[] genes_one = new int[6];int[] genes_two = new int[6];	//life_cycle:int fill_here = 0;	//occupancy rules:int regions = data.regions;int width = data.grid_width;int height = data.grid_height;//init method is similar to the main method in a java application		public void init() {		instance = this;		size = this.size();		buffer = this.createImage(size.width, size.height);		bufferGraphics = buffer.getGraphics();						addMouseListener(this);		addMouseMotionListener(this);				//dataSpace data = new dataSpace();					//start with 10 blocks on their own:		for (int i=0; i<initial_blocks; i++)  {			temp_x = r.randomInt(500)+50;			temp_y = r.randomInt(240)+30;			temp_size = r.randomInt(40)+10;			temp_mob = r.randomInt(6)+5;			temp_col = r.randomInt(31);			temp_amp = r.randomInt(4)+2;							burp[i] = new evoOrganism(temp_x, temp_y, temp_size, temp_mob, temp_col, temp_amp);			data.addOrg(burp[i]);			number_living += 1;						}		//System.out.println("initialized" + number_living + "organisms");				}		public void paint(Graphics g)  {				//draw background		this.setBackground(c4);		bufferGraphics.setColor(this.getBackground());		bufferGraphics.fillRect(0, 0, size.width, size.height);				//draw grid		bufferGraphics.setColor(white);		for(int gX=0; gX<data.grid_width; gX++) {			int wave = r.randomInt(3);			int grid_x = data.grid_x[gX];			//bufferGraphics.fillRect(grid_x, 0, wave, 300);			bufferGraphics.drawLine(grid_x, 0, grid_x, 300);		}		for(int gY=0; gY<data.grid_height; gY++) {			int wave2 = r.randomInt(3);			int grid_y = data.grid_y[gY];			//bufferGraphics.fillRect(0, grid_y, 600, wave2);			bufferGraphics.drawLine(0, grid_y, 600, grid_y);		}				//draw barriers		bufferGraphics.setColor(lime);		for (int j=0; j<number_simple; j++)  {			bX = sim[j].getX();			bY = sim[j].getY();			bW = sim[j].getSize();						bufferGraphics.fillRect(bX, bY, bW, bW);				}		if(expanding) {			sim[which_simple].increaseSize();			//try { Thread.sleep(5); } catch (InterruptedException e) { ; }		}					//draw organisms		for (int i=0; i<number_living; i++)  {			co = burp[i].getColor();			bX = burp[i].getX();			bY = burp[i].getY();			bD = burp[i].getSize();			int draw_size = bD;			bA = burp[i].fixedAmp();			bM = burp[i].getMob();			bS = burp[i].getSexy();			bL = burp[i].getLife();			int jump = burp[i].getAmp()/2;						bufferGraphics.setColor(block);			bufferGraphics.fillRect(bX, bY, draw_size-1, draw_size-1);						bufferGraphics.setColor(band);			int band_height = draw_size/4;			//a band for all of it's characteristics:			bufferGraphics.fillRect(bX, bY, bA*10, band_height);			bufferGraphics.fillRect(bX+bM, bY+band_height, draw_size-bM, band_height);			bufferGraphics.fillRect(bX, bY+(2*band_height), bS/bD, band_height);			bufferGraphics.fillRect(bX+co*2, bY+(3*band_height), draw_size-co*2, band_height+1);						bufferGraphics.setColor(bound);			bufferGraphics.drawRect(bX-1*bD+jump, bY-3*bD+jump, 9*bD, 5*bD);		}				//draw info buffer:		bufferGraphics.setColor(white);		bufferGraphics.fillRect(0, 299, 600, 340);				bufferGraphics.setColor(lime);		bufferGraphics.fillRect(0, 290, special_clock/10, 4);		//put an overwraped bar of timeline here.						String living = new String("// " +String.valueOf(number_living) + " system organisms");		bufferGraphics.setColor(bound);		bufferGraphics.drawString(living, 10, 330);				String inputs = new String("// " +String.valueOf(number_simple) + " user inputs");		//bufferGraphics.setColor(lime);		bufferGraphics.drawString(inputs, 10, 318);							g.drawImage(buffer, 0, 0, this);	}		public void update(Graphics g) { paint(g); }			//method for runnable:	public void run()  {		while(!please_stop) {					if (special_clock%10 == 0) {				this.moveSims();				this.killSims();			}						if (special_clock%50 == 0) {					this.calculateOccupants();				this.gridResponse();			}						if (special_clock%250 == 0)	{				this.selectMates();				this.reproduce();				this.selectKills();				this.kill();			}									special_clock++;				//change to 10(?) before posting to web			try { Thread.sleep(20); } catch (InterruptedException e) { ; }			repaint();		}		animator = null;	}		public void calculateOccupants() {				int org_x;		int org_y;		int left;		int right;		int top;		int bottom;				//zero the number of occupants in each region:		data.zeroOccupants();				//check each organism and assign to the dataSpace:		for (int i=0; i<number_living; i++) {			org_x = burp[i].getX();			org_y = burp[i].getY();			for (int w=0; w<(width-1); w++) {				left = data.grid_x[w];				right = data.grid_x[w+1];				//keep the on the edge vague - let organisms exploit it				if ((left < org_x) && (right > org_x)) {					for(int h=0; h<(height-1); h++) {						top = data.grid_y[h];						bottom = data.grid_y[h+1];						if ((top < org_y) && (bottom > org_y)) {							data.gridAdd(w, h, i);						}					}				}			}		}	}		public void gridResponse() {		int occ;		for(int i=0; i<(data.grid_width-1); i++) {			for(int j=0; j<(data.grid_height-1); j++) {				occ = data.occupants[i][j][0];								//region of 2 blocks				if (occ == 2) {					int last_in = data.grid_org[i][j][occ-1];										int mobile = burp[last_in].getMob();					int x_move = (r.randomInt(2000)%mobile)*2 - mobile/2;					int y_move = (r.randomInt(2000)%mobile)*2 - mobile/2;						//makes a random move					burp[last_in].move(x_move, y_move);												// gets a color change - more life ability					int color_change = r.randomInt(2000)%4 -2;					burp[last_in].changeColor(1);				}								//region of more				if (occ > 2) {					int last_in = data.grid_org[i][j][occ-1];										while(occ>3) {						burp[last_in].getKilled();						occ = data.occupants[i][j][0] -= 1;						last_in = data.grid_org[i][j][occ-1];					}					//now last_in should be 3:					int mobile = burp[last_in].getMob();					int x_move = (r.randomInt(2000)%mobile)*4 - mobile/4;					int y_move = (r.randomInt(2000)%mobile)*4 - mobile/4;										burp[last_in].move(x_move, y_move);				}			}		}	}		public void killSims() {		if (number_simple == 1) {			if (sim[0].life < 1) number_simple = 0;		}		else for (int i=0; i<number_simple-1; i++) {			if (sim[i].life < 1) {				sim[i] = sim[i+1];				number_simple -= 1;			}		}	}		int line_x;	int line_y;	public void moveSims() {		for (int i=0; i<number_simple; i++) {			temp_x = sim[i].getX();			temp_y = sim[i].getY();						//check to see if the x-point lies on a gird line:			for (int j=0; j<width; j++) {				line_x = data.grid_x[j];				//hiting a verticle:				if (Math.abs(line_x-temp_x) < 2) {					if(sim[i].left) data.grid_x[j] -= sim[i].getSize();					else data.grid_x[j] += sim[i].getSize();					sim[i].left =! sim[i].left;				}			}						for (int k=0; k<height; k++) {				line_y = data.grid_y[k];				if (Math.abs(line_y-temp_y) < 2) {					if(sim[i].down) data.grid_y[k] += sim[i].getSize();					else data.grid_y[k] -= sim[i].getSize();					//System.out.println("vert");					sim[i].down =! sim[i].down;				}			}						sim[i].move();		}	}			//tally sexiness and 2 most sexy that aren't stranded will mate:	public void selectMates()  {				best[0] = 0;			//highest sexy value		best[1] = 0;			//corresponding org index		second_best[0] = 0;				second_best[0] = 0;		//possible_mates = 0;				int danger_x = data.simple_X();		int danger_y = data.simple_Y();		int dense_x = data.average_X();		int dense_y = data.average_Y();				if(number_living>1) {			this.selectOne();			this.selectTwo();			male = best[1];			female = second_best[1];			mating = true;			//System.out.println("sexiest" + best[1] + "mating" + second_best[1]);		}			}		public void selectOne() {				//checking distance from danger		//including color as an attractive quality				for (int i=0; i<number_living; i++)  {			temp_sexy = 60;			temp_x = burp[i].getX();			if (number_simple != 1) {				temp_sexy = Math.abs(temp_x - danger_x);				temp_sexy += Math.abs(temp_y - danger_y);			}			temp_sexy -= (burp[i].getSize());			temp_sexy += (burp[i].getColor());					burp[i].setSexy(temp_sexy);			//System.out.println("org" + i + "barrier/size" + temp_sexy);		}				//selecting the most fit:		for (int j=0; j<number_living-1; j++)  {			int sexy_current = burp[j].getSexy();			if (sexy_current > best[0]) {				best[0] = sexy_current;				best[1] = j;				burp[j].selected = true;			}		}	}			public void selectTwo() {				//checking distance from others		//including Age as an attractive quality		for (int i=0; i<number_living; i++)  {			temp_x = burp[i].getX();			temp_sexy = Math.abs(temp_x - dense_x);			temp_sexy -= Math.abs(temp_y - dense_y);			temp_sexy += (burp[i].getAge());					burp[i].setSexy(temp_sexy);			//System.out.println("org" + i + "diversity/color" + temp_sexy);		}				//selecting the most fit:		//ignore the already seleceted		for (int j=0; j<number_living-1; j++)  {			if (!burp[j].selected) {				int sexy_current = burp[j].getSexy();				if (sexy_current > second_best[0]) {					second_best[0] = sexy_current;					second_best[1] = j;				}			}		}	}		public void reproduce() {		if(mating) {					int crosspoint = r.randomInt(2000)%6;			int one_mutation = r.randomInt(2000)%6;			int two_mutation = r.randomInt(2000)%6;					male_genes[0] = burp[male].getX();			male_genes[1] = burp[male].getY();			male_genes[2] = burp[male].getSize();			male_genes[3] = burp[male].getMob();			male_genes[4] = burp[male].getColor();			male_genes[5] = burp[male].getAmp();						female_genes[0] = burp[female].getX();			female_genes[1] = burp[female].getY();			female_genes[2] = burp[female].getSize();			female_genes[3] = burp[female].getMob();			female_genes[4] = burp[female].getColor();			female_genes[5] = burp[female].getAmp();						//crossover			for (int i=0; i<crosspoint; i++) {				genes_one[i] = male_genes[i];				genes_two[i] = female_genes[i];			}			for (int j=crosspoint; j<6; j++) {				genes_one[j] = female_genes[j];				genes_two[j] = male_genes[j];			}						//mutation						genes_one[one_mutation] += (r.randomInt(2000)%12 - 5);			//System.out.println("mutating gene" + one_mutation);			genes_two[two_mutation] += (r.randomInt(2000)%12 - 5);						burp[number_living] = new evoOrganism(genes_one[0], genes_one[1], genes_one[2], genes_one[3], genes_one[4], genes_one[5]);			data.addOrg(burp[number_living]);			number_living += 1;			burp[number_living] = new evoOrganism(genes_two[0], genes_two[1], genes_two[2], genes_two[3], genes_two[4], genes_two[5]);			data.addOrg(burp[number_living]);			number_living += 1;						mating = false;					}	}		public void selectKills() {		// give everyone a year of age, and calculate their life_ability		for (int i=0; i<number_living; i++) {			burp[i].addAge();			burp[i].tallyLife();		}	}		public void kill()  {		//System.out.println("alive" + number_living);		fill_here = 0;		for(int i=0; i<(number_living); i++)  {				if(burp[i].getAlive()) {					burp[fill_here] = burp[i];					fill_here += 1;				}		}			number_living = fill_here;		//System.out.println("alive" + number_living);	}			public void start()  {		if (animator == null)  {			please_stop = false;			animator = new Thread(this);			animator.start();		}	}		public void stop() { please_stop = true; }  		// will he notice this - yes						//methods for mouse press, clicked, entered, exited -- MouseListener	public void mousePressed(MouseEvent event) {		open_space = true;		temp_x = event.getX();		temp_y = event.getY();		if (number_simple == 0) {			temp_size = 8;			temp_x = temp_x - temp_width/2;			temp_y = temp_y - temp_width/2;						sim[number_simple] = new simpleOrg(temp_x, temp_y, temp_size);			data.addSim(sim[number_simple]);			number_simple += 1;			open_space = false;		}		//check to see if you are already on a void space - make new or expand existing.		else {			for (int i=0; i<number_simple; i++)  {				int block_x = sim[i].getX();				int block_y = sim[i].getY();				int size = sim[i].getSize();								if( temp_x>block_x && temp_x<(block_x+size) )  {					if( temp_y>block_y && temp_y<(block_y+size) )  {						//bar[i].increaseSize();						which_simple = i;						expanding = true;						//try { Thread.sleep(5); } catch (InterruptedException e) { ; }						open_space = false;					}				}				}		}		if(open_space) {			temp_size = 8;			temp_x = temp_x - temp_size/2;			temp_y = temp_y - temp_size/2;						sim[number_simple] = new simpleOrg(temp_x, temp_y, temp_size);			data.addSim(sim[number_simple]);			number_simple += 1;		}	}	public void mouseReleased(MouseEvent event) {		expanding = false;	}		public void mouseClicked(MouseEvent event) {}	public void mouseEntered(MouseEvent event) {}	public void mouseExited(MouseEvent event) {}				//methods for mouse moved, dragged -- MouseMotionListener	public void mouseMoved(MouseEvent event) {}	public void mouseDragged(MouseEvent event) {		//if(moving)->do something 	}			}