Advertisement
cgorrillaha

FlexibleBot

Mar 26th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. package model;
  2.  
  3. import java.util.InputMismatchException;
  4.  
  5. public class FlexibleBot extends Bot{
  6.  
  7. public FlexibleBot(Location loc, int id, Directions dir, int moveSpeed){
  8. super(loc, id, dir);
  9. setMoveSpeed(moveSpeed);
  10. }
  11.  
  12. @Override
  13. public boolean move(Map m){
  14. boolean moved=super.move(m);//call on move() from Bot
  15. int spacesToMove=getMoveSpeed();
  16. int turnCount=0;
  17. int distance=distanceToEntityOrEdge(m);
  18. //do flex move
  19. if(!moved){
  20. while(spacesToMove>0&&turnCount<4) {
  21. if(distance==0){
  22. turn();
  23. turnCount++;
  24. }//turned
  25. else{
  26. if(spacesToMove>=distance){
  27. moveNumSpaces(distance);
  28. spacesToMove-=distance;
  29. }
  30. else{
  31. moveNumSpaces(spacesToMove);
  32. spacesToMove-=spacesToMove;
  33. }
  34. turnCount=0;
  35. }//moved
  36. distance=distanceToEntityOrEdge(m);
  37. }//end while
  38. moved=turnCount<4;
  39. }
  40.  
  41. return moved;
  42. }
  43.  
  44.  
  45. /**
  46. * @return the number of cells in the direction that the bot is facing
  47. * between the bot and the closest entity or edge of map
  48. * */
  49. public int distanceToEntityOrEdge(Map m){
  50. int distance=getMoveSpeed();
  51. int count=0;
  52. boolean pathClear=true;
  53.  
  54.  
  55. if(distance>=botDistanceFromEdge()){
  56. distance=botDistanceFromEdge();
  57. }
  58. while (pathClear && count < distance) {
  59. switch(getDirection()){
  60. case UP:{
  61. if(validCell(m,getLoc().getRow() - count - 1,getLoc().getCol())){
  62. count++;
  63. }
  64. else{
  65. pathClear=false;
  66. }
  67. break;
  68. }
  69. case DOWN:{
  70. if(validCell(m,getLoc().getRow() + count + 1,getLoc().getCol())){
  71. count++;
  72. }
  73. else{
  74. pathClear=false;
  75. }
  76. break;
  77. }
  78. case LEFT:{
  79. if(validCell(m, getLoc().getRow(),getLoc().getCol() - count - 1)){
  80. count++;
  81. }
  82. else{
  83. pathClear=false;
  84. }
  85. break;
  86. }
  87. case RIGHT:{
  88. if(validCell(m,getLoc().getRow(),getLoc().getCol() + count + 1)){
  89. count++;
  90. }
  91. else{
  92. pathClear=false;
  93. }
  94. break;
  95. }
  96. }
  97.  
  98. }//end while
  99.  
  100. return count;
  101.  
  102. }
  103.  
  104.  
  105. public void speedUp (int spdVal)throws InputMismatchException {
  106. if(spdVal>=0) {
  107. setMoveSpeed(getMoveSpeed() + spdVal);
  108. }
  109. else{
  110. throw new InputMismatchException("Speed must be greater than or equal to 0");
  111. }
  112. }
  113.  
  114. public void speedDown(int spdVal)throws InputMismatchException{
  115. if(getMoveSpeed()-spdVal>=0){
  116. setMoveSpeed(getMoveSpeed()-spdVal);
  117. }
  118. else {
  119. throw new InputMismatchException("Speed must not go below 0.");
  120. }
  121. }
  122.  
  123. @Override
  124. public void counterTurn() {
  125. if(getDirection().compareTo(Directions.UP)>0){
  126. setDirection(Movable.getPreviousDirection(getDirection()));
  127. }
  128. else{
  129. setDirection(Directions.LEFT);
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement