Routeplanner.java 2.9 KB

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