/*	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 Randomizer;import evoOrganism;public class evo4 extends Applet 			implements MouseListener, MouseMotionListener, Runnable { public static evo4 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 c1 = new Color(45, 45, 31); 				// green greyColor c2 = new Color(247, 252, 242);			// greenish whiteColor c3 = new Color(235, 240, 230);			Color net = new Color(35, 20, 20);				// dark redColor grey = new Color(196, 196, 196);Color white = new Color(255, 255, 255);		//pause for runtime:int pause_factor = 15;int special_clock = 0;	//general framework for evoltion:int number_living = 0;int number_barriers = 0;int number_attractors = 0;		//specific variables for applet:evoOrganism[] burp = new evoOrganism[300];evoBarrier[] bar = new evoBarrier[200];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;dataSpace data = new dataSpace();	//testing and expanding barriers:boolean open_space;boolean expanding;int which_barrier;	//action/response:boolean go_right, go_down;	//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;//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(20)+5;			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)  {				this.setBackground(c3);				bufferGraphics.setColor(this.getBackground());		bufferGraphics.fillRect(0, 0, size.width, size.height);				//draw barriers		bufferGraphics.setColor(white);		for (int j=0; j<number_barriers; j++)  {			bX = bar[j].getX();			bY = bar[j].getY();			bW = bar[j].getWidth();			bH = bar[j].getHeight();						bufferGraphics.fillRect(bX, bY, bW, bH);				}		if(expanding) {			bar[which_barrier].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();						Color dyn1 = new Color(co*2, co*8, co*4);			bufferGraphics.setColor(dyn1);			bufferGraphics.fillRect(bX+burp[i].getAmp(), bY+burp[i].getAmp(), bD, bD);						Color dyn2 = new Color(co*8, co*8, co*8);						bufferGraphics.setColor(grey);			bufferGraphics.drawRect(bX-1*bD, bY-3*bD, 9*bD, 5*bD);		}												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%50 == 0) {					this.action_response();			}			if (special_clock%250 == 0)	{				this.select_mates();				this.reproduce();				this.select_kills();				this.kill();			}			if (special_clock%150 == 0) {				this.clearDense();				this.kill();			}									special_clock++;				//change to 10(?) before posting to web			try { Thread.sleep(8); } catch (InterruptedException e) { ; }			repaint();		}		animator = null;	}		public void action_response()  {		//need to intensify action/response to increase diversity of organisms:		for(int i=0; i<number_living; i++)  {						temp_x = burp[i].getX();			temp_y = burp[i].getY();			temp_mob = burp[i].getMob();			burp[i].stranded = false;			for (int j=0; j<number_barriers; j++)  {				int block_x = bar[j].getX();				int block_y = bar[j].getY();				int width = bar[j].getWidth();				int height = bar[j].getHeight();							if( temp_x>block_x && temp_x<(block_x+width) )  {					if( temp_y>block_y && temp_y<(block_y+height) )  {						go_right = true;						go_down = true;						burp[i].stranded = true;											int mobile = burp[i].getMob();						int x_move = (r.randomInt(2000)%mobile)*2 - mobile/2;						int y_move = (r.randomInt(2000)%mobile)*2 - mobile/2;						//moves to region of less barriers						burp[i].move(x_move, y_move);												// gets a color change - more life ability						int color_change = r.randomInt(2000)%4 -2;						burp[i].changeColor(1);					}				}			}				//not stranded blocks move to region of less density			if(!burp[i].stranded)  {					int x_move = (burp[i].getAmp()/2);					int y_move = (burp[i].getAmp()/2);					//moves to region of less organisms					int dense_x = data.average_X();					int dense_y = data.average_Y();										if(temp_x - dense_x <= 0) go_right = false;					else go_right = true;					if(temp_y - dense_y <= 0) go_down = true;					else go_down = false;										if(go_right) burp[i].move(x_move, 0);					else burp[i].move(-x_move, 0);					if(go_down) burp[i].move(0, y_move);					else burp[i].move(0, -y_move);			}		}	}		//tally sexiness and 2 most sexy that aren't stranded will mate:	public void select_mates()  {				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.barrier_X();		int danger_y = data.barrier_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 size as an attractive quality				for (int i=0; i<number_living; i++)  {			temp_x = burp[i].getX();			temp_sexy = Math.abs(temp_x - danger_x);			temp_sexy += Math.abs(temp_y - danger_y);			temp_sexy += (burp[i].getSize())*3;					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())*20;					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)%6 - 2);			//System.out.println("mutating gene" + one_mutation);			genes_two[two_mutation] += (r.randomInt(2000)%6 - 2);						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 select_kills() {		// 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 clearDense() {		//checking distance from others		for (int i=1; i<number_living; i+=2)  {			temp_x = burp[i].getX();			int neighbor_x = burp[(i-1)].getX();			if (Math.abs(temp_x - neighbor_x) < 20) {				burp[i].getKilled();				System.out.println("you got bucked");			}		}	}		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	evoBarrier temp_bar = new evoBarrier(0,0,1,1);					//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_barriers == 0) {			temp_width = r.randomInt(20)+15;			temp_height = r.randomInt(50)+15;			temp_x = temp_x - temp_width/2;			temp_y = temp_y - temp_width/2;						bar[number_barriers] = new evoBarrier(temp_x, temp_y, temp_width, temp_height);			data.addBar(bar[number_barriers]);			number_barriers += 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_barriers; i++)  {				int block_x = bar[i].getX();				int block_y = bar[i].getY();				int width = bar[i].getWidth();				int height = bar[i].getHeight();							if( temp_x>block_x && temp_x<(block_x+width) )  {					if( temp_y>block_y && temp_y<(block_y+height) )  {						//bar[i].increaseSize();						which_barrier = i;						expanding = true;						//try { Thread.sleep(5); } catch (InterruptedException e) { ; }						open_space = false;					}				}				}		}		if(open_space) {			temp_width = r.randomInt(20)+15;			temp_height = r.randomInt(50)+15;			temp_x = temp_x - temp_width/2;			temp_y = temp_y - temp_width/2;						bar[number_barriers] = new evoBarrier(temp_x, temp_y, temp_width, temp_height);			data.addBar(bar[number_barriers]);			number_barriers += 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 	}			}