Advertisement
cgorrillaha

Bot

Mar 26th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.23 KB | None | 0 0
  1. //package model;
  2. ///**
  3. //* @author: Charlie Gorrill
  4. //* Organization: NHCC OOP Class
  5. //* Description: A Bot is a sumulation of a robot
  6. //* that will be used on a map.  Robots can move and turn.
  7. //* The intention is to explore OOP concepts through working
  8. //* with Bots and Maps together
  9. //* */
  10. //public abstract class Bot extends Entity implements Movable{
  11. //
  12. //    /**
  13. //     * SLOW_SPEED will be used to set the speed of a
  14. //     * Bot to one space per call to the move method
  15. //     * */
  16. //    /**
  17. //     * direction repesents the direction that
  18. //     * the Bot is facing.  direction can hold
  19. //     * 0-3 inclusive.
  20. //     * */
  21. //    private Directions direction;
  22. //    /**
  23. //     * moveSpeed indicates how many spaces a
  24. //     * Bot will move when the move() method is called
  25. //     * */
  26. //    private int moveSpeed;
  27. //
  28. //
  29. //    /**
  30. //     * initialize all instance variables.
  31. //     * The Bot has a Location, id, direction, and moveSpeed
  32. //     * */
  33. //    public Bot(Location l, int id, Directions dir){
  34. //        super(l, id);
  35. //        this.direction=dir;
  36. //        moveSpeed=SLOW_SPEED;
  37. //    }
  38. //
  39. //    /**
  40. //     * move the Bot in the direction
  41. //     * that the Bot is facing.  The Bot
  42. //     * will move moveSpeed spaces on the
  43. //     * map
  44. //     * */
  45. //    public boolean move(Map m){
  46. //        boolean validUp=validMove(m, getLoc().getRow()-moveSpeed, getLoc().getCol());
  47. //        boolean validDown=validMove(m, getLoc().getRow()+moveSpeed, getLoc().getCol());
  48. //        boolean validRight=validMove(m, getLoc().getRow(), getLoc().getCol()+moveSpeed);
  49. //        boolean validLeft=validMove(m, getLoc().getRow(), getLoc().getCol()-moveSpeed);
  50. //        boolean botMoved=false;
  51. //
  52. //        if(direction==Directions.UP&&validUp){
  53. //            getLoc().setRow(getLoc().getRow()-moveSpeed);
  54. //            botMoved=true;
  55. //        }
  56. //        else if(direction==Directions.RIGHT&&validRight){
  57. //            getLoc().setCol(getLoc().getCol()+moveSpeed);
  58. //            botMoved=true;
  59. //        }
  60. //        else if(direction==Directions.DOWN&&validDown){
  61. //            getLoc().setRow(getLoc().getRow()+moveSpeed);
  62. //            botMoved=true;
  63. //        }
  64. //        else if(direction==Directions.LEFT&&validLeft){
  65. //            getLoc().setCol(getLoc().getCol()-moveSpeed);
  66. //            botMoved=true;
  67. //        }
  68. //        else{
  69. //            System.out.println("Invalid move!");
  70. //
  71. //        }
  72. //        return botMoved;
  73. //    }
  74. //
  75. //
  76. //    public boolean validMove(Map m, int row, int col){
  77. //        boolean validRow=validRow(m, row);
  78. //        boolean validCol=validCol(m, col);
  79. //        boolean cellNotOccupied=false;
  80. //        if(validCol&&validRow){
  81. //            cellNotOccupied=m.getBotMap()[row][col]==null;
  82. //        }
  83. //        return validCol&&validRow&&cellNotOccupied&&movePathClear(m);
  84. //    }
  85. //
  86. //    public boolean validCol(Map m,int col){
  87. //        return col>=0&&col<Map.NUM_COLS;
  88. //    }
  89. //    public boolean validRow(Map m,int row){
  90. //        return row>=0&&row<Map.NUM_ROWS;
  91. //    }
  92. //    public boolean movePathClear(Map m){
  93. //        int distance=moveSpeed;
  94. //
  95. //        int count=1;
  96. //        boolean pathClear=true;
  97. //
  98. //        if(distance>botDistanceFromEdge())
  99. //            pathClear=false;
  100. //
  101. //        while(count<=distance&&pathClear){
  102. //
  103. //            if(direction==Directions.UP){
  104. //                pathClear=m.getBotMap()[getLoc().getRow()-count][getLoc().getCol()] ==null;
  105. //                count++;
  106. //            }
  107. //            else if(direction==Directions.DOWN){
  108. //                pathClear=m.getBotMap()[getLoc().getRow()+count][getLoc().getCol()] ==null;
  109. //                count++;
  110. //            }
  111. //            else if(direction==Directions.LEFT){
  112. //                pathClear=m.getBotMap()[getLoc().getRow()][getLoc().getCol()-count] ==null;
  113. //                count++;
  114. //            }
  115. //            else if(direction==Directions.RIGHT){
  116. //                pathClear=m.getBotMap()[getLoc().getRow()][getLoc().getCol()+count] ==null;
  117. //                count++;
  118. //            }
  119. //
  120. //        }
  121. //
  122. //        return pathClear;
  123. //    }
  124. //
  125. //    public int botDistanceFromEdge(){
  126. //        int distance=0;
  127. //
  128. //        switch(getDirection()){
  129. //            case UP:
  130. //                distance=getLoc().getRow();
  131. //                break;
  132. //            case LEFT:
  133. //                distance=getLoc().getCol();
  134. //                break;
  135. //            case RIGHT:
  136. //                distance=Map.NUM_COLS-1-getLoc().getCol();
  137. //                break;
  138. //            case DOWN:
  139. //                distance=Map.NUM_ROWS-1-getLoc().getRow();
  140. //                break;
  141. //        }
  142. //        return distance;
  143. //    }
  144. //
  145. //    /**
  146. //     * Turn the Bot 90 degrees clockwise
  147. //     * */
  148. //    public void turn(){
  149. //        if(direction.compareTo(Directions.LEFT)<0){
  150. //            direction= Movable.getNextDirection(direction);
  151. //        }
  152. //        else{
  153. //            direction=Directions.UP;
  154. //        }
  155. //    }
  156. //
  157. //    public void setMoveSpeed(int moveSpeed) {
  158. //        this.moveSpeed = moveSpeed;
  159. //    }
  160. //
  161. //    public int getMoveSpeed() {
  162. //        return moveSpeed;
  163. //    }
  164. //
  165. //    public Directions getDirection() {
  166. //        return direction;
  167. //    }
  168. //
  169. //    public void setDirection(Directions direction) {
  170. //        this.direction = direction;
  171. //    }
  172. //
  173. //    @Override
  174. //    public String toString(){
  175. //        return super.toString()+" Bot ["+
  176. //                " Direction: "+direction+" Speed: "+moveSpeed+"]";
  177. //    }
  178. //}
  179. package model;
  180.  
  181. /**
  182.  * @author: Charlie Gorrill
  183.  * Organization: NHCC OOP Class
  184.  * Description: A model.Bot is a sumulation of a robot
  185.  * that will be used on a map.  Robots can move and turn.
  186.  * The intention is to explore OOP concepts through working
  187.  * with Bots and Maps together
  188.  * */
  189. public abstract class Bot extends Entity implements Movable{
  190.  
  191.     /**
  192.      * SLOW_SPEED will be used to set the speed of a
  193.      * model.Bot to one space per call to the move method
  194.      * */
  195.     /**
  196.      * direction repesents the direction that
  197.      * the model.Bot is facing.  direction can hold
  198.      * 0-3 inclusive.
  199.      * */
  200.     private Directions direction;
  201.     /**
  202.      * moveSpeed indicates how many spaces a
  203.      * model.Bot will move when the move() method is called
  204.      * */
  205.     private int moveSpeed;
  206.  
  207.  
  208.     /**
  209.      * initialize all instance variables.
  210.      * The model.Bot has a model.Location, id, direction, and moveSpeed
  211.      * */
  212.     public Bot(Location l, int id, Directions dir){
  213.         super(l, id);
  214.         this.direction=dir;
  215.         moveSpeed=SLOW_SPEED;
  216.     }
  217.  
  218.     /**
  219.      * move the model.Bot in the direction
  220.      * that the model.Bot is facing.  The model.Bot
  221.      * will move moveSpeed spaces on the
  222.      * map
  223.      * */
  224.     public boolean move(Map m){
  225.         boolean botMoved=false;
  226.         if(validBotMove(m)) {
  227.             moveNumSpaces(moveSpeed);
  228.             botMoved=true;
  229.         } else {
  230.             System.out.println("Invalid move!");
  231.         }
  232.         return botMoved;
  233.     }
  234.  
  235.     public void moveNumSpaces(int spaces){
  236.         if (direction == Directions.UP) {
  237.             getLoc().setRow(getLoc().getRow() - spaces);
  238.         } else if (direction == Directions.RIGHT) {
  239.             getLoc().setCol(getLoc().getCol() + spaces);
  240.         } else if (direction == Directions.DOWN) {
  241.             getLoc().setRow(getLoc().getRow() + spaces);
  242.         } else if (direction == Directions.LEFT) {
  243.             getLoc().setCol(getLoc().getCol() - spaces);
  244.         }
  245.     }
  246.  
  247.     public boolean validBotMove(Map m){
  248.         boolean valid;
  249.         if(direction==Directions.UP){
  250.             valid= validCell(m, getLoc().getRow()-moveSpeed, getLoc().getCol())&&movePathClear(m);
  251.         }
  252.         else if(direction==Directions.DOWN){
  253.             valid= validCell(m, getLoc().getRow()+moveSpeed, getLoc().getCol())&&movePathClear(m);
  254.         }
  255.         else if(direction==Directions.RIGHT){
  256.             valid= validCell(m, getLoc().getRow(), getLoc().getCol()+moveSpeed)&&movePathClear(m);
  257.         }
  258.         else{
  259.             valid= validCell(m, getLoc().getRow(), getLoc().getCol()-moveSpeed)&&movePathClear(m);
  260.         }
  261.         return valid;
  262.     }
  263.  
  264.     public boolean validCell(Map m, int row, int col){
  265.         boolean validRow=validRow(m, row);
  266.         boolean validCol=validCol(m, col);
  267.         boolean cellNotOccupied=false;
  268.         if(validCol&&validRow){
  269.             cellNotOccupied=m.getBotMap()[row][col]==null;
  270.         }
  271.         return validCol&&validRow&&cellNotOccupied;
  272.     }
  273.  
  274.     public boolean validCol(Map m,int col){
  275.         return col>=0&&col<Map.NUM_COLS;
  276.     }
  277.     public boolean validRow(Map m,int row){
  278.         return row>=0&&row<Map.NUM_ROWS;
  279.     }
  280.  
  281.     public boolean movePathClear(Map m){
  282.         int distance=moveSpeed;
  283.  
  284.         int count=1;
  285.         boolean pathClear=true;
  286.  
  287.         if(distance>botDistanceFromEdge())
  288.             pathClear=false;
  289.  
  290.         while(count<=distance&&pathClear){
  291.  
  292.             if(direction==Directions.UP){
  293.                 pathClear=m.getBotMap()[getLoc().getRow()-count][getLoc().getCol()] ==null;
  294.                 count++;
  295.             }
  296.             else if(direction==Directions.DOWN){
  297.                 pathClear=m.getBotMap()[getLoc().getRow()+count][getLoc().getCol()] ==null;
  298.                 count++;
  299.             }
  300.             else if(direction==Directions.LEFT){
  301.                 pathClear=m.getBotMap()[getLoc().getRow()][getLoc().getCol()-count] ==null;
  302.                 count++;
  303.             }
  304.             else if(direction==Directions.RIGHT){
  305.                 pathClear=m.getBotMap()[getLoc().getRow()][getLoc().getCol()+count] ==null;
  306.                 count++;
  307.             }
  308.  
  309.         }
  310.  
  311.         return pathClear;
  312.     }
  313.  
  314.     /*
  315.     * @return the distance to the nearest entity in this BOt's direction
  316.     * if no bots are in the path return -1
  317.     * */
  318.     public int botDistanceToEntity(Map m){
  319.         int distance=-1;
  320.         int closest=botDistanceFromEdge();
  321.         int row=getLoc().getRow();
  322.         int col=getLoc().getCol();
  323.         for(Entity e:m.getBots()) {
  324.             System.out.println(e);
  325.             int eRow=e.getLoc().getRow();
  326.             int eCol=e.getLoc().getCol();
  327.             if(this!=e) {
  328.                 if ((direction == Directions.UP || direction == Directions.DOWN) && (col == eCol)) {
  329.                     distance = Math.abs(row - eRow);
  330.                     System.out.println("distance: "+distance);
  331.                 } else if ((direction == Directions.LEFT || direction == Directions.RIGHT) && (row == eRow)) {
  332.                     distance = Math.abs(col - eCol);
  333.                     System.out.println("distance: "+distance);
  334.                 }
  335.                 if(distance<=closest){
  336.                     closest=distance;
  337.                 }
  338.             }
  339.         }
  340.         System.out.println("return closest:"+closest);
  341.         return closest;
  342.     }
  343.  
  344.     public int botDistanceFromEdge(){
  345.         int distance=0;
  346.  
  347.         switch(getDirection()){
  348.             case UP:
  349.                 distance=getLoc().getRow();
  350.                 break;
  351.             case LEFT:
  352.                 distance=getLoc().getCol();
  353.                 break;
  354.             case RIGHT:
  355.                 distance=Map.NUM_COLS-1-getLoc().getCol();
  356.                 break;
  357.             case DOWN:
  358.                 distance=Map.NUM_ROWS-1-getLoc().getRow();
  359.                 break;
  360.         }
  361.         return distance;
  362.     }
  363.  
  364.     /**
  365.      * Turn the model.Bot 90 degrees clockwise
  366.      * */
  367.     public void turn(){
  368.         if(direction.compareTo(Directions.LEFT)<0){
  369.             direction= Movable.getNextDirection(direction);
  370.         }
  371.         else{
  372.             direction=Directions.UP;
  373.         }
  374.     }
  375.  
  376.     public void setMoveSpeed(int moveSpeed) {
  377.         this.moveSpeed = moveSpeed;
  378.     }
  379.  
  380.     public int getMoveSpeed() {
  381.         return moveSpeed;
  382.     }
  383.  
  384.     public Directions getDirection() {
  385.         return direction;
  386.     }
  387.  
  388.     public void setDirection(Directions direction) {
  389.         this.direction = direction;
  390.     }
  391.  
  392.     @Override
  393.     public String toString(){
  394.         return super.toString()+" model.Bot ["+
  395.                 " Direction: "+direction+" Speed: "+moveSpeed+"]";
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement