Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.55 KB | None | 0 0
  1. package model;
  2.  
  3. /**
  4.  * @author: Charlie Gorrill
  5.  * Organization: NHCC OOP Class
  6.  * Description: A model.Bot is a sumulation of a robot
  7.  * that will be used on a map.  Robots can move and turn.
  8.  * The intention is to explore OOP concepts through working
  9.  * with Bots and Maps together
  10.  * */
  11. public abstract class Bot extends Entity implements Movable{
  12.  
  13.     /**
  14.      * SLOW_SPEED will be used to set the speed of a
  15.      * model.Bot to one space per call to the move method
  16.      * */
  17.     /**
  18.      * direction repesents the direction that
  19.      * the model.Bot is facing.  direction can hold
  20.      * 0-3 inclusive.
  21.      * */
  22.     private Directions direction;
  23.     /**
  24.      * moveSpeed indicates how many spaces a
  25.      * model.Bot will move when the move() method is called
  26.      * */
  27.     private int moveSpeed;
  28.  
  29.  
  30.     /**
  31.      * initialize all instance variables.
  32.      * The model.Bot has a model.Location, id, direction, and moveSpeed
  33.      * */
  34.     public Bot(Location l, int id, Directions dir){
  35.         super(l, id);
  36.         this.direction=dir;
  37.         moveSpeed=SLOW_SPEED;
  38.     }
  39.  
  40.     /**
  41.      * move the model.Bot in the direction
  42.      * that the model.Bot is facing.  The model.Bot
  43.      * will move moveSpeed spaces on the
  44.      * map
  45.      * */
  46.     public boolean move(Map m){
  47.         boolean botMoved=false;
  48.         if(validBotMove(m)) {
  49.             moveNumSpaces(moveSpeed,m);
  50.             botMoved=true;
  51.         } else {
  52.             System.out.println("Invalid move!");
  53.         }
  54.         return botMoved;
  55.     }
  56.  
  57.     public void moveNumSpaces(int spaces, Map m){
  58.         if (direction == Directions.UP) {
  59.             getLoc().setRow(getLoc().getRow() - spaces);
  60.         } else if (direction == Directions.RIGHT) {
  61.             getLoc().setCol(getLoc().getCol() + spaces);
  62.         } else if (direction == Directions.DOWN) {
  63.             getLoc().setRow(getLoc().getRow() + spaces);
  64.         } else if (direction == Directions.LEFT) {
  65.             getLoc().setCol(getLoc().getCol() - spaces);
  66.         }
  67.     }
  68.  
  69.     public boolean validBotMove(Map m){
  70.         boolean valid;
  71.         if(direction==Directions.UP){
  72.             valid= validCell(m, getLoc().getRow()-moveSpeed, getLoc().getCol())&&movePathClear(m);
  73.         }
  74.         else if(direction==Directions.DOWN){
  75.             valid= validCell(m, getLoc().getRow()+moveSpeed, getLoc().getCol())&&movePathClear(m);
  76.         }
  77.         else if(direction==Directions.RIGHT){
  78.             valid= validCell(m, getLoc().getRow(), getLoc().getCol()+moveSpeed)&&movePathClear(m);
  79.         }
  80.         else{
  81.             valid= validCell(m, getLoc().getRow(), getLoc().getCol()-moveSpeed)&&movePathClear(m);
  82.         }
  83.         return valid;
  84.     }
  85.  
  86.     public boolean validCell(Map m, int row, int col){
  87.         boolean validRow=validRow(m, row);
  88.         boolean validCol=validCol(m, col);
  89.         boolean cellNotOccupied=false;
  90.         if(validCol&&validRow){
  91.             cellNotOccupied=m.getBotMap()[row][col]==null;
  92.         }
  93.         return validCol&&validRow&&cellNotOccupied;
  94.     }
  95.  
  96.     public boolean validCol(Map m,int col){
  97.         return col>=0&&col<Map.NUM_COLS;
  98.     }
  99.     public boolean validRow(Map m,int row){
  100.         return row>=0&&row<Map.NUM_ROWS;
  101.     }
  102.  
  103.     public boolean movePathClear(Map m){
  104.         int distance=moveSpeed;
  105.  
  106.         int count=1;
  107.         boolean pathClear=true;
  108.  
  109.         if(distance>botDistanceFromEdge())
  110.             pathClear=false;
  111.  
  112.         while(count<=distance&&pathClear){
  113.  
  114.             if(direction==Directions.UP){
  115.                 pathClear=m.getBotMap()[getLoc().getRow()-count][getLoc().getCol()] ==null;
  116.                 count++;
  117.             }
  118.             else if(direction==Directions.DOWN){
  119.                 pathClear=m.getBotMap()[getLoc().getRow()+count][getLoc().getCol()] ==null;
  120.                 count++;
  121.             }
  122.             else if(direction==Directions.LEFT){
  123.                 pathClear=m.getBotMap()[getLoc().getRow()][getLoc().getCol()-count] ==null;
  124.                 count++;
  125.             }
  126.             else if(direction==Directions.RIGHT){
  127.                 pathClear=m.getBotMap()[getLoc().getRow()][getLoc().getCol()+count] ==null;
  128.                 count++;
  129.             }
  130.  
  131.         }
  132.  
  133.         return pathClear;
  134.     }
  135.  
  136.     public int botDistanceFromEdge(){
  137.         int distance=0;
  138.  
  139.         switch(getDirection()){
  140.             case UP:
  141.                 distance=getLoc().getRow();
  142.                 break;
  143.             case LEFT:
  144.                 distance=getLoc().getCol();
  145.                 break;
  146.             case RIGHT:
  147.                 distance=Map.NUM_COLS-1-getLoc().getCol();
  148.                 break;
  149.             case DOWN:
  150.                 distance=Map.NUM_ROWS-1-getLoc().getRow();
  151.                 break;
  152.         }
  153.         return distance;
  154.     }
  155.  
  156.     /**
  157.      * Turn the model.Bot 90 degrees clockwise
  158.      * */
  159.     public void turn(){
  160.         if(direction.compareTo(Directions.LEFT)<0){
  161.             direction= Movable.getNextDirection(direction);
  162.         }
  163.         else{
  164.             direction=Directions.UP;
  165.         }
  166.     }
  167.  
  168.     public void setMoveSpeed(int moveSpeed) {
  169.         this.moveSpeed = moveSpeed;
  170.     }
  171.  
  172.     public int getMoveSpeed() {
  173.         return moveSpeed;
  174.     }
  175.  
  176.     public Directions getDirection() {
  177.         return direction;
  178.     }
  179.  
  180.     public void setDirection(Directions direction) {
  181.         this.direction = direction;
  182.     }
  183.  
  184.     @Override
  185.     public String toString(){
  186.         return super.toString()+" model.Bot ["+
  187.                 " Direction: "+direction+" Speed: "+moveSpeed+"]";
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement