Advertisement
SuperJedi224

Bot Battle Controller

Apr 11th, 2015
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class BotBattleController {
  5.     public static void main(String[] args){
  6.         //Uncomment the below line to redirect the output to a text file
  7.         //try{System.setOut(new PrintStream("battle.txt"));}catch(FileNotFoundException e){}
  8.         Random r=new Random(System.currentTimeMillis()%1000);
  9.         int turns;
  10.         final int maxturns=turns=1000;
  11.         List<Bot> bots=new ArrayList<Bot>(){
  12.             private static final long serialVersionUID = 1L;
  13.             public String toString(){
  14.                 String s="";
  15.                 for(Bot b:this){
  16.                     if(b!=null)s+=b+",";
  17.                 }
  18.                 return s.substring(0, s.length()-1);
  19.             }
  20.         };
  21.         /*edit and uncomment these lines as needed*/
  22.         //(new BotOneClass()).register(bots);
  23.         //(new BotTwoClass()).register(bots);
  24.         //(new BotThreeClass()).register(bots);
  25.         /*Don't edit after this point*/
  26.         int[][] map=new int[64][64];
  27.         for(int x=0;x<64;x++){
  28.             map[x]=new int[64];
  29.             for(int y=0;y<64;y++){
  30.                 map[x][y]=0;
  31.             }
  32.         }
  33.         int i=5+r.nextInt(100);
  34.         for(int j=0;j<i;j++){
  35.             int x=r.nextInt(64),y=r.nextInt(64);
  36.             map[x][y]=-1;
  37.         }
  38.         for(Bot b:bots){
  39.             boolean a=true;
  40.             while(a){
  41.                 int x=r.nextInt(64),y=r.nextInt(64);
  42.                 if(map[x][y]!=0)continue ;
  43.                 map[x][y]=b.id();
  44.                 b.p=new Bot.Position(x,y);
  45.                 a=false;
  46.             }
  47.         }
  48.         System.out.println("Bots loaded.");
  49.         boolean running=true;
  50.         while(running&&turns>0){
  51.             turns--;
  52.             for(Bot b:bots){
  53.                 if(b==null)continue ;
  54.                 Bot.Action s;
  55.                 long k=System.nanoTime();
  56.                 try{s=b.action(copy(map));}catch(Exception e){
  57.                     e.printStackTrace(System.out);
  58.                     bots.set(bots.indexOf(b), null);
  59.                     System.out.println(b+" threw an exception, and has been eliminated on turn "+(maxturns-turns)+".");
  60.                     break ;
  61.                 }
  62.                 k=(System.nanoTime()-k);
  63.                 System.out.println(b+"("+b.p.x+","+b.p.y+"):"+s+" ("+k/1000+"us)");
  64.                 k/=1000000;
  65.                 Bot.Position p=b.p;
  66.                 int x=p.x;
  67.                 int y=p.y;
  68.                 map[x][y]=0;
  69.                 if(k>50){
  70.                     bots.set(bots.indexOf(b), null);
  71.                     System.out.println(b+" took too long to respond, and has been eliminated on turn "+(maxturns-turns)+".");
  72.                     break ;
  73.                 }
  74.                 if(s==Bot.Action.MINE){
  75.                     s=(new Bot.Action[]{Bot.Action.DOWN,Bot.Action.LEFT,Bot.Action.RIGHT,Bot.Action.UP})[r.nextInt(4)];
  76.                     map[x][y]=-1;
  77.                 }
  78.                 if(s==Bot.Action.LEFT){
  79.                     x--;
  80.                 }
  81.                 if(s==Bot.Action.RIGHT){
  82.                     x++;
  83.                 }
  84.                 if(s==Bot.Action.UP){
  85.                     y--;
  86.                 }
  87.                 if(s==Bot.Action.DOWN){
  88.                     y++;
  89.                 }
  90.                 if(y<0||y>=64||x<0||x>=64){
  91.                     bots.set(bots.indexOf(b), null);
  92.                     System.out.println(b+" left the map on turn "+(maxturns-turns)+".");
  93.                     break ;
  94.                 }
  95.                 if(map[x][y]==-1){
  96.                     bots.set(bots.indexOf(b), null);
  97.                     map[x][y]=0;
  98.                     System.out.println(b+" was killed by a mine on turn "+(maxturns-turns)+".");
  99.                     break ;
  100.                 }
  101.                 if(map[x][y]!=0&&map[x][y]!=b.id()){
  102.                     Bot c=bots.get(map[x][y]-1);
  103.                     bots.set(bots.indexOf(c), null);
  104.                     System.out.println(c+" was killed by "+b+" on turn "+(maxturns-turns)+".");
  105.                     map[x][y]=b.id();
  106.                     p.x=x;p.y=y;
  107.                     break;
  108.                 }
  109.                 p.x=x;p.y=y;
  110.                 map[x][y]=b.id();
  111.             }
  112.             int c=0;
  113.             Bot w=null;
  114.             for(Bot b:bots){
  115.                 if(b!=null){
  116.                     c++;
  117.                     w=b;
  118.                 }
  119.             }
  120.             if(c<=1){
  121.                 running=false;
  122.                 if(w==null)System.out.println("All bots have lost.");
  123.                 if(w!=null)System.out.println(w+" has won in "+(maxturns-turns)+" steps.");
  124.             }
  125.         }
  126.         if(turns==0)System.out.println("Time ran out. "+bots+" are still standing.");
  127.         System.out.close();
  128.     }
  129.     public static int[][] copy(int[][] a){
  130.         int[][] o=new int[64][64];
  131.         for(int x=0;x<64;x++){
  132.             for(int y=0;y<64;y++){
  133.                 o[x][y]=a[x][y];
  134.             }
  135.         }
  136.         return o;
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement