Routeplanner.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. public class Routeplanner
  2. {
  3. private int rotation;
  4. private int x;
  5. private int y;
  6. private int maxx;
  7. private int maxy;
  8. private int ex;
  9. private int ey;
  10. public Routeplanner(int rotation, int maxx, int maxy)
  11. {
  12. this.rotation = rotation;
  13. this.maxx = maxx;
  14. this.maxy = maxy;
  15. x = 0;
  16. y = 0;
  17. ex = 0;
  18. ey = 0;
  19. }
  20. public void turnleft()
  21. {
  22. rotation = rotation - 90;
  23. if (rotation < 0)
  24. {
  25. rotation = 270;
  26. }
  27. }
  28. public void turnright()
  29. {
  30. rotation = rotation + 90;
  31. if (rotation > 270)
  32. {
  33. rotation = 0;
  34. }
  35. }
  36. public void vooruit()
  37. {
  38. if (rotation == 0 && y < maxy)
  39. {
  40. y ++;
  41. }
  42. if (rotation == 90 && x < maxx)
  43. {
  44. x ++;
  45. }
  46. if (rotation == 180 && y > 0)
  47. {
  48. y --;
  49. }
  50. if (rotation == 270 && x > 0)
  51. {
  52. x --;
  53. }
  54. }
  55. public void geefCoordinaten()
  56. {
  57. System.out.print("(" + x + "," + y + ")");
  58. }
  59. public void gaNaarPunt(int eindx, int eindy)
  60. {
  61. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  62. {
  63. ex = eindx;
  64. ey = eindy;
  65. }
  66. while(ex != x)
  67. {
  68. if(x < ex)
  69. {
  70. if(rotation == 90)
  71. {
  72. vooruit();
  73. System.out.print("vooruit");
  74. }
  75. else
  76. {
  77. turnleft();
  78. System.out.print("turnleft");
  79. }
  80. }
  81. else
  82. {
  83. if(x > ex)
  84. {
  85. if(rotation == 270)
  86. {
  87. vooruit();
  88. System.out.print("vooruit");
  89. }
  90. else
  91. {
  92. turnleft();
  93. System.out.print("turnleft");
  94. }
  95. }
  96. }
  97. }
  98. while(ey != y)
  99. {
  100. if(y < ey)
  101. {
  102. if(rotation == 0)
  103. {
  104. vooruit();
  105. System.out.print("vooruit");
  106. }
  107. else
  108. {
  109. turnright();
  110. System.out.print("turnright");
  111. }
  112. }
  113. else
  114. {
  115. if(y > ey)
  116. {
  117. if(rotation == 180)
  118. {
  119. vooruit();
  120. System.out.print("vooruit");
  121. }
  122. else
  123. {
  124. turnright();
  125. System.out.print("turnright");
  126. }
  127. }
  128. }
  129. }
  130. System.out.print("bestemming bereikt " + " (" + x + "," + y + ")");
  131. }
  132. }