BoebotSimulator.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import java.util.*;
  2. /**
  3. * Write a description of class BoebotSimulator here.
  4. *
  5. * @author Remco Sannen
  6. * @version (a version number or a date)
  7. */
  8. public class BoebotSimulator
  9. {
  10. private int rotation;
  11. private int x;
  12. private int y;
  13. private int xx;
  14. private int xy;
  15. private int ex;
  16. private int ey;
  17. public String coordinaten;
  18. public ArrayList<Integer> ax;
  19. public ArrayList<Integer> ay;
  20. public ArrayList<Integer> arotation;
  21. public BoebotSimulator(int x, int y, int rotation)
  22. {
  23. this.x = x;
  24. this.y = y;
  25. xx = -1;
  26. xy = -1;
  27. rotation = 0;
  28. ax = new ArrayList<Integer>();
  29. ay = new ArrayList<Integer>();
  30. arotation = new ArrayList<Integer>();
  31. }
  32. public void turnleft()
  33. {
  34. rotation = rotation - 90;
  35. if (rotation < 0)
  36. {
  37. rotation = 270;
  38. }
  39. }
  40. public void turnright()
  41. {
  42. rotation = rotation + 90;
  43. if (rotation > 270)
  44. {
  45. rotation = 0;
  46. }
  47. }
  48. public void keren()
  49. {
  50. rotation = rotation + 180;
  51. if (rotation == 360)
  52. {
  53. rotation = 0;
  54. }
  55. if(rotation == 450)
  56. {
  57. rotation = 90;
  58. }
  59. }
  60. public void vooruit()
  61. {
  62. if (rotation == 0)
  63. {
  64. y ++;
  65. }
  66. if (rotation == 90)
  67. {
  68. x ++;
  69. }
  70. if (rotation == 180)
  71. {
  72. y --;
  73. }
  74. if (rotation == 270)
  75. {
  76. x --;
  77. }
  78. }
  79. public String geefCoordinaten()
  80. {
  81. coordinaten = "(" + x + "," + y + ")";
  82. return coordinaten;
  83. }
  84. public boolean voerOpdrachtUit(String[] opdracht, int bx, int by, int lengte, int ex, int ey)
  85. {
  86. this.x = bx;
  87. this.y = by;
  88. this.ey = ey;
  89. this.ex = ex;
  90. int teller = 0;
  91. boolean botsing = false;
  92. ax.add(x);
  93. ay.add(y);
  94. arotation.add(rotation);
  95. while(teller < lengte)
  96. {
  97. if(opdracht[teller].equals("vooruit") && !(x == xx && y == xy))
  98. {
  99. vooruit();
  100. }
  101. if(opdracht[teller].equals("linksaf")&& !(x == xx && y == xy))
  102. {
  103. turnleft();
  104. vooruit();
  105. }
  106. if(opdracht[teller].equals("rechtsaf")&& !(x == xx && y == xy))
  107. {
  108. turnright();
  109. vooruit();
  110. }
  111. if(opdracht[teller].equals("keren")&& !(x == xx && y == xy))
  112. {
  113. keren();
  114. vooruit();
  115. }
  116. if(x == xx && y == xy)
  117. {
  118. teller = lengte - 1;
  119. botsing = true;
  120. keren();
  121. vooruit();
  122. keren();
  123. }
  124. ax.add(x);
  125. ay.add(y);
  126. arotation.add(rotation);
  127. teller ++;
  128. }
  129. if(x == ex && y == ey)
  130. {
  131. System.out.println("Boebot is op juiste bestemming()");
  132. }
  133. else
  134. {
  135. System.out.println("Boebot is gecrasht");
  136. }
  137. return botsing;
  138. }
  139. public String plaatsObstakel(int xx, int xy)
  140. {
  141. this.xx = xx;
  142. this.xy = xy;
  143. return "Obstakel geplaatst op: " + "(" + xx + "," + xy + ")";
  144. }
  145. public int geefX()
  146. {
  147. return x;
  148. }
  149. public int geefY()
  150. {
  151. return y;
  152. }
  153. public int geefRotation()
  154. {
  155. return rotation;
  156. }
  157. public int geefXX()
  158. {
  159. return xx;
  160. }
  161. public int geefXY()
  162. {
  163. return xy;
  164. }
  165. public int geefEX()
  166. {
  167. return ex;
  168. }
  169. public int geefEY()
  170. {
  171. return ey;
  172. }
  173. }