Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class BotBattleController {
- public static void main(String[] args){
- //Uncomment the below line to redirect the output to a text file
- //try{System.setOut(new PrintStream("battle.txt"));}catch(FileNotFoundException e){}
- Random r=new Random(System.currentTimeMillis()%1000);
- int turns;
- final int maxturns=turns=1000;
- List<Bot> bots=new ArrayList<Bot>(){
- private static final long serialVersionUID = 1L;
- public String toString(){
- String s="";
- for(Bot b:this){
- if(b!=null)s+=b+",";
- }
- return s.substring(0, s.length()-1);
- }
- };
- /*edit and uncomment these lines as needed*/
- //(new BotOneClass()).register(bots);
- //(new BotTwoClass()).register(bots);
- //(new BotThreeClass()).register(bots);
- /*Don't edit after this point*/
- int[][] map=new int[64][64];
- for(int x=0;x<64;x++){
- map[x]=new int[64];
- for(int y=0;y<64;y++){
- map[x][y]=0;
- }
- }
- int i=5+r.nextInt(100);
- for(int j=0;j<i;j++){
- int x=r.nextInt(64),y=r.nextInt(64);
- map[x][y]=-1;
- }
- for(Bot b:bots){
- boolean a=true;
- while(a){
- int x=r.nextInt(64),y=r.nextInt(64);
- if(map[x][y]!=0)continue ;
- map[x][y]=b.id();
- b.p=new Bot.Position(x,y);
- a=false;
- }
- }
- System.out.println("Bots loaded.");
- boolean running=true;
- while(running&&turns>0){
- turns--;
- for(Bot b:bots){
- if(b==null)continue ;
- Bot.Action s;
- long k=System.nanoTime();
- try{s=b.action(copy(map));}catch(Exception e){
- e.printStackTrace(System.out);
- bots.set(bots.indexOf(b), null);
- System.out.println(b+" threw an exception, and has been eliminated on turn "+(maxturns-turns)+".");
- break ;
- }
- k=(System.nanoTime()-k);
- System.out.println(b+"("+b.p.x+","+b.p.y+"):"+s+" ("+k/1000+"us)");
- k/=1000000;
- Bot.Position p=b.p;
- int x=p.x;
- int y=p.y;
- map[x][y]=0;
- if(k>50){
- bots.set(bots.indexOf(b), null);
- System.out.println(b+" took too long to respond, and has been eliminated on turn "+(maxturns-turns)+".");
- break ;
- }
- if(s==Bot.Action.MINE){
- s=(new Bot.Action[]{Bot.Action.DOWN,Bot.Action.LEFT,Bot.Action.RIGHT,Bot.Action.UP})[r.nextInt(4)];
- map[x][y]=-1;
- }
- if(s==Bot.Action.LEFT){
- x--;
- }
- if(s==Bot.Action.RIGHT){
- x++;
- }
- if(s==Bot.Action.UP){
- y--;
- }
- if(s==Bot.Action.DOWN){
- y++;
- }
- if(y<0||y>=64||x<0||x>=64){
- bots.set(bots.indexOf(b), null);
- System.out.println(b+" left the map on turn "+(maxturns-turns)+".");
- break ;
- }
- if(map[x][y]==-1){
- bots.set(bots.indexOf(b), null);
- map[x][y]=0;
- System.out.println(b+" was killed by a mine on turn "+(maxturns-turns)+".");
- break ;
- }
- if(map[x][y]!=0&&map[x][y]!=b.id()){
- Bot c=bots.get(map[x][y]-1);
- bots.set(bots.indexOf(c), null);
- System.out.println(c+" was killed by "+b+" on turn "+(maxturns-turns)+".");
- map[x][y]=b.id();
- p.x=x;p.y=y;
- break;
- }
- p.x=x;p.y=y;
- map[x][y]=b.id();
- }
- int c=0;
- Bot w=null;
- for(Bot b:bots){
- if(b!=null){
- c++;
- w=b;
- }
- }
- if(c<=1){
- running=false;
- if(w==null)System.out.println("All bots have lost.");
- if(w!=null)System.out.println(w+" has won in "+(maxturns-turns)+" steps.");
- }
- }
- if(turns==0)System.out.println("Time ran out. "+bots+" are still standing.");
- System.out.close();
- }
- public static int[][] copy(int[][] a){
- int[][] o=new int[64][64];
- for(int x=0;x<64;x++){
- for(int y=0;y<64;y++){
- o[x][y]=a[x][y];
- }
- }
- return o;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement