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;
int currentBotRound;
int A,B,C,D,E,F,G;
int counterHits, counterFired;
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)
{
}
Breed();
}
public void onBulletHit(BulletHitEvent e){
counterHits++; counterFired++;
}
public void onBulletMiss(BulletMissedEvent e){
counterFired++;
}
public int getFitnessScore(){
double accuracy;
if(counterFired==0)
{
accuracy=0;
}
else{
accuracy=counterHits/counterFired;
}
return (int)((accuracy/(int)getTime())*1000000);
}
public void onDeath(DeathEvent e){
ScoreArray[getRoundNum()][0]=getFitnessScore();
ScoreArray[getRoundNum()][1]=(getRoundNum());
}
public void onRoundEnded(RoundEndedEvent e){
ScoreArray[getRoundNum()][0]=getFitnessScore();
ScoreArray[getRoundNum()][1]=(getRoundNum());
}
public void Breed(){
/*
* Need to select breeding pairs. Robots with higher scores are more likely to breed with one another.
* How to get higher score robots selected? - ScoreArray[i][0] contains the top score...
* let the highest score be the benchmark? Add up individual / all scores is its percentage
* then pick what you are going to mutate. Call random number. If be
*/
double chanceSelection[]=new double[StartingPop];
int sumScores=0;
Random rand= new Random();
double counter=0;
int selectedPairRound[] = new int[2];
String selectedPairs[] =new String[2];
for (int i=0; i<StartingPop; i++ )
{
sumScores+=(ScoreArray[i][0]);
}
for (int i=0; i<StartingPop; i++ )
{
chanceSelection[i]=(double)(ScoreArray[i][0]/sumScores);
}
for(int j=0;j<2;j++){
int q=0;
int selector=rand.nextInt(100);
//Select what rounds we want in selectedPairRound[], then
for (int i=0;i< StartingPop;i++){
if((selector>counter)&&(selector<=(counter+chanceSelection[i])))
{
selectedPairRound[j]=ScoreArray[q][1];
break;
}
else{
q++;
counter+=chanceSelection[i];
}
}
}
try {
BufferedReader r = new BufferedReader(new FileReader(getDataFile("Populations.txt")));
for (int j=0;j<2;j++){
for(int i=0;i<selectedPairRound[j];i++) {r.readLine();}
selectedPairs[j] = r.readLine();
}
}
catch(IOException e){}
for(int i=0;i<2;i++)
{
out.print(selectedPairs[i]+"\n");
}
}
public void createStartPopulations()
{
PrintStream w= null;
try{
w= new PrintStream(new RobocodeFileOutputStream(getDataFile("Populations.txt")));
for(int i=0;i<StartingPop;i++)
{ Random randomgenerator =new Random();
int g1=randomgenerator.nextInt(1000);
int g2=randomgenerator.nextInt(360);
int g3=randomgenerator.nextInt(1000);
int g4=randomgenerator.nextInt(360);
int g5=randomgenerator.nextInt(4);
int g6=randomgenerator.nextInt(1000);
int g7=randomgenerator.nextInt(360);
w.println(+g1 +"-" +g2+ "-"+g3+ "-"+g4+"-" +g5+"-" +g6+"-" +g7);
}
w.close();
}
catch (IOException e)
{
}
}
public void importCurrentGene(){
try {
BufferedReader r = new BufferedReader(new FileReader(getDataFile("Populations.txt")));
for(int i=0;i<currentBotRound;i++) {r.readLine();}
currentGenome = r.readLine();
}
catch(IOException e){}
}
}