Advertisement
cgorrillaha

Untitled

Mar 24th, 2022
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package Entities;
  2.  
  3. import Map.Map;
  4. import Map.Location;
  5.  
  6. import java.util.ArrayList;
  7.  
  8. public class KillBot extends Bot{//Yes, I know that I was supposed to extend FlexBot, but this is better
  9.     private int speed;
  10.     public KillBot(Location l, int id, Directions d, int s) {
  11.         super(l, id, d);
  12.         speed=s;
  13.     }
  14.  
  15.     public boolean move(Map m){
  16.         int movesMade=0;
  17.         int turnCount=0;
  18.         while (movesMade<speed&&turnCount<4){
  19.             if(super.move(m)){//can move
  20.                 movesMade++;
  21.                 turnCount=0;
  22.             }
  23.             else{//can't move
  24.                 Entity e=getEntInPath(m);
  25.                 System.out.println(e);
  26.                 if(e!=null&&e.getID()<getID()&&e instanceof Bot){//try kill
  27.                     m.removeEnt(e);
  28.                     super.move(m);
  29.                     movesMade++;
  30.                     turnCount=0;
  31.                 }else{//no ent or ent too strong so turn
  32.                 turn();
  33.                 turnCount++;
  34.                 }
  35.             }
  36.  
  37.         }
  38.         return movesMade==speed;
  39.     }
  40.  
  41.     public Entity getEntInPath(Map m){
  42.         Entity out=null;
  43.         switch(getDir()){
  44.             case UP:{
  45.                 for(Entity e: m.getEntList()){
  46.                     if(e.getLoc().equals(new Location(getLoc().getRow()-1,
  47.                             getLoc().getCol()))){
  48.                         out=e;
  49.                     }
  50.                 }
  51.                 break;
  52.             }
  53.             case DOWN:{
  54.                 for(Entity e: m.getEntList()){
  55.                     if(e.getLoc().equals(new Location(getLoc().getRow()+1,
  56.                             getLoc().getCol()))){
  57.                         out=e;
  58.                     }
  59.                 }
  60.                 break;
  61.             }
  62.             case LEFT:{
  63.                 for(Entity e: m.getEntList()){
  64.                     if(e.getLoc().equals(new Location(getLoc().getRow(),
  65.                             getLoc().getCol()-1))){
  66.                         out=e;
  67.                     }
  68.                 }
  69.                 break;
  70.             }
  71.             case RIGHT:{
  72.                 for(Entity e: m.getEntList()){
  73.                     if(e.getLoc().equals(new Location(getLoc().getRow(),
  74.                             getLoc().getCol()+1))){
  75.                         out=e;
  76.                     }
  77.                 }
  78.                 break;
  79.             }
  80.         }
  81.         return out;
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement