Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. /**
  2. * Moves alphabot forward until there is no wall on the left, then it does a back flip
  3. * By: Alejandro Antorcha
  4. */
  5. package Lesson4;
  6. import kareltherobot.*;
  7.  
  8. public class Activity1Lesson4 extends Robot{
  9.  
  10. public Activity1Lesson4(int x, int y, Direction d, int b) {
  11. super(x, y, d, b);
  12. }
  13.  
  14. public static void main(String[] args) {
  15. World.readWorld("Lesson4World1.kwld");
  16.  
  17. World.setDelay(25);
  18. World.setVisible();
  19.  
  20. Robot alphaBot = new Activity1Lesson4(1, 3, North, 0);
  21.  
  22. //runs move() 4 times
  23. for(int i = 0; i < 4; i++) {
  24. alphaBot.move();
  25. }
  26.  
  27. }
  28.  
  29. /**
  30. * @Override
  31. * Moves normally but if it is at the tower, it turns left
  32. * @param
  33. * @return void
  34. */
  35. public void move() {
  36. //chooses to either move forward or do a back flip
  37. if(leftIsClear()) {
  38. this.backFlip();
  39. } else {
  40. super.move();
  41. }
  42.  
  43. }
  44.  
  45. /**
  46. * checks to see if the left is clear
  47. * @param
  48. * @return boolean
  49. */
  50. public boolean leftIsClear() {
  51. int delay = World.delay();
  52. World.setDelay(0);
  53.  
  54. //initializes boolean variable clear
  55. boolean clear;
  56.  
  57. //turns left to orient and check
  58. this.turnLeft();
  59.  
  60. //assigns clear to
  61. clear = this.frontIsClear();
  62.  
  63. //orients robot back facing north
  64. while(!this.facingNorth()) {
  65. turnLeft();
  66. }
  67. //setting the relay to what it was previously set to
  68. World.setDelay(delay);
  69.  
  70. return clear;
  71. }
  72.  
  73. /**
  74. * makes the robot do a back flip
  75. * @param
  76. * @return void
  77. */
  78. public void backFlip() {
  79. for(int i = 0; i < 4; i++) {
  80. this.turnLeft();
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement