Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package model;
  2.  
  3. public class FlexibleBot extends Bot{
  4.     public static int UP_RIGHT=4, DOWN_RIGHT=5, DOWN_LEFT=6, UP_LEFT=7;
  5.  
  6.     /**
  7.      * initialize all instance variables.
  8.      * The Bot has a Location, id, direction, and moveSpeed
  9.      *
  10.      * @param l
  11.      * @param id
  12.      * @param dir
  13.      * @param speed
  14.      */
  15.     public FlexibleBot(Location l, int id, int dir, int speed) {
  16.         super(l, id, dir, speed);
  17.     }
  18.  
  19.     public void counterTurn(){
  20.         if(getDirection()==0){
  21.             setDirection(LEFT);
  22.         }
  23.         else{
  24.             setDirection(getDirection()-1);
  25.         }
  26.     }
  27.  
  28.     public void speedUp(){
  29.         setMoveSpeed(getMoveSpeed()+1);
  30.     }
  31.  
  32.     public void speedDown(){
  33.         setMoveSpeed(getMoveSpeed()-1);
  34.     }
  35.  
  36.     @Override
  37.     public boolean move(Map m){
  38.         boolean moveMade=super.move(m);
  39.  
  40.         while(!moveMade){
  41.             if(botDistanceFromEdge()<getMoveSpeed()){
  42.                 putBotOnEdge();
  43.                 moveMade=true;
  44.             }
  45.             else{
  46.                 turn();
  47.                 moveMade=super.move(m);
  48.             }
  49.         }
  50.         return moveMade;
  51.     }
  52.  
  53.     public boolean teleport(Map m, int row, int col){
  54.         boolean moveMade=validMove(m, row, col);
  55.         if(moveMade){
  56.             getLoc().setRow(row);
  57.             getLoc().setCol(col);
  58.         }
  59.         return moveMade;
  60.     }
  61.  
  62.     private int botDistanceFromEdge(){
  63.         int distance=0;
  64.  
  65.         switch(getDirection()){
  66.             case UP:
  67.                 distance=getLoc().getRow();
  68.                 break;
  69.             case LEFT:
  70.                 distance=getLoc().getCol();
  71.                 break;
  72.             case RIGHT:
  73.                 distance=Map.NUM_COLS-1-getLoc().getCol();
  74.                 break;
  75.             case DOWN:
  76.                 distance=Map.NUM_ROWS-1-getLoc().getRow();
  77.             break;
  78.         }
  79.         return distance;
  80.     }
  81.  
  82.     private void putBotOnEdge(){
  83.         switch(getDirection()){
  84.             case UP:
  85.                 getLoc().setRow(0);
  86.                 break;
  87.             case LEFT:
  88.                 getLoc().setCol(0);
  89.                 break;
  90.             case RIGHT:
  91.                 getLoc().setCol(Map.NUM_COLS-1);
  92.                 break;
  93.             case DOWN:
  94.                 getLoc().setRow(Map.NUM_ROWS-1);
  95.                 break;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement