package bot1; import robocode.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; import java.util.Random; public class CooperBot1 extends AdvancedRobot { static double mutationProbability = 0.05; static int StartingPop = 10; String currentGenome; String NextGenPop[]= new String[StartingPop]; int currentBotRound; int A, B, C, D, E, F, G; int counterHits, counterFired; int AMax=1000, BMax=360,CMax=1000,DMax=360,EMax=4,FMax=1000,GMax=360; static int ScoreArray[][] = new int[StartingPop][2]; public void run() { counterHits = 0; counterFired = 0; currentBotRound = getRoundNum(); try { BufferedReader r = new BufferedReader(new FileReader( getDataFile("Populations.txt"))); } catch (IOException e) { createStartPopulations(); } importCurrentGene(); out.print(currentGenome); String[] temp; String delimiter = "-"; temp = currentGenome.split(delimiter); A = Integer.parseInt(temp[0]); B = Integer.parseInt(temp[1]); C = Integer.parseInt(temp[2]); D = Integer.parseInt(temp[3]); E = Integer.parseInt(temp[4]); F = Integer.parseInt(temp[5]); G = Integer.parseInt(temp[6]); while (true) { // Replace the next 4 lines with any behavior you would like ahead(A); turnGunLeft(B); back(C); turnGunRight(D); } } /** * onScannedRobot: What to do when you see another robot */ public void onScannedRobot(ScannedRobotEvent e) { // Replace the next line with any behavior you would like stop(); setFire(E); stop(); } /** * onHitByBullet: What to do when you're hit by a bullet */ /** * onHitWall: What to do when you hit a wall */ public void onHitWall(HitWallEvent e) { // Replace the next line with any behavior you would like back(F); turnRight(G); } public void onBattleEnded(BattleEndedEvent e) { int temp[] = new int[2]; for (int i = 0; i < ScoreArray.length - 1; i++) { for (int j = 0; j < ScoreArray.length - 1; j++) if (ScoreArray[j][0] > ScoreArray[j + 1][0]) { temp[0] = ScoreArray[j][0]; temp[1] = ScoreArray[j][1]; ScoreArray[j][0] = ScoreArray[j + 1][0]; ScoreArray[j][1] = ScoreArray[j + 1][1]; ScoreArray[j + 1][0] = temp[0]; ScoreArray[j + 1][1] = temp[1]; } } for (int i = 0; i < ScoreArray.length; i++) out.print("Score:" + ScoreArray[i][0] + "Round:" + ScoreArray[i][1] + "\n "); PrintStream x = null; try { x = new PrintStream(new RobocodeFileOutputStream( getDataFile("Scores.txt"))); for (int i = 0; i < ScoreArray.length; i++) x.println(ScoreArray[i][0] + "-" + ScoreArray[i][1]); x.close(); } catch (IOException f) { out.print("Exception at printing the array of scores"); } for(int i=0;i=0;i--)// chooses two random (ranked) bot genomes and stores them for use in crossover. { selectionArray[i]=ScoreArray[limiter-1][1]; if(counter==0) { limiter--; counter=limiter; } counter--; } Random rand = new Random(); for (int i=0;i<2;i++) { int selector = rand.nextInt(selectionarraysize); selectedPairRound[i]=selectionArray[selector];//Populates the array with 2 coresponding bot round numbers. } for(int i=0;i<2;i++) { try { BufferedReader r = new BufferedReader(new FileReader(getDataFile("Populations.txt"))); for(int j=0;j50) { A3=A1; } else { A3=A2; } if(rand1.nextInt(100)>50) { B3=B1; } else { B3=B2; } if(rand1.nextInt(100)>50) { C3=C1; } else { C3=C2; } if(rand1.nextInt(100)>50) { D3=D1; } else { D3=D2; } if(rand1.nextInt(100)>50) { E3=E1; } else { E3=E2; } if(rand1.nextInt(100)>50) { F3=F1; } else { F3=F2; } if(rand1.nextInt(100)>50) { G3=G1; } else { G3=G2; } mutate(A3,AMax); mutate(B3,BMax); mutate(C3,CMax); mutate(D3,DMax); mutate(E3,EMax); mutate(F3,FMax); mutate(G3,GMax); String child=null; child=(A3+"-"+B3+"-"+C3+"-"+D3+"-"+E3+"-"+F3+"-"+G3); return child; } public int mutate(int phenotype,int eeff1) { int mutatedphenotype; Random randmutate=new Random(); if(randmutate.nextInt(100)