Aansturing.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package boebot;
  2. public class Aansturing
  3. {
  4. private int rotation;
  5. private int x;
  6. private int y;
  7. private int maxx;
  8. private int maxy;
  9. private int ex;
  10. private int ey;
  11. public String coordinaten;
  12. public Aansturing(int maxx, int maxy)
  13. {
  14. this.maxx = maxx;
  15. this.maxy = maxy;
  16. rotation = 0;
  17. x = 0;
  18. y = 0;
  19. ex = 0;
  20. ey = 0;
  21. }
  22. public void turnleft()
  23. {
  24. rotation = rotation - 90;
  25. if (rotation < 0)
  26. {
  27. rotation = 270;
  28. }
  29. }
  30. public void turnright()
  31. {
  32. rotation = rotation + 90;
  33. if (rotation > 270)
  34. {
  35. rotation = 0;
  36. }
  37. }
  38. public void keren()
  39. {
  40. rotation = rotation + 180;
  41. if (rotation == 360)
  42. {
  43. rotation = 0;
  44. }
  45. if(rotation == 450)
  46. {
  47. rotation = 90;
  48. }
  49. }
  50. public void vooruit()
  51. {
  52. if (rotation == 0 && y < maxy)
  53. {
  54. y ++;
  55. }
  56. if (rotation == 90 && x < maxx)
  57. {
  58. x ++;
  59. }
  60. if (rotation == 180 && y > 0)
  61. {
  62. y --;
  63. }
  64. if (rotation == 270 && x > 0)
  65. {
  66. x --;
  67. }
  68. }
  69. public String geefCoordinaten()
  70. {
  71. coordinaten = "(" + x + "," + y + ")";
  72. return coordinaten;
  73. }
  74. public String gaNaarPunt(int eindx, int eindy)
  75. {
  76. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  77. {
  78. ex = eindx;
  79. ey = eindy;
  80. }
  81. while(ex != x)
  82. {
  83. if(x < ex)
  84. {
  85. if(rotation == 90)
  86. {
  87. vooruit();
  88. System.out.println("vooruit");
  89. }
  90. else
  91. {
  92. turnleft();
  93. System.out.println("turnleft");
  94. }
  95. }
  96. else
  97. {
  98. if(x > ex)
  99. {
  100. if(rotation == 270)
  101. {
  102. vooruit();
  103. System.out.println("vooruit");
  104. }
  105. else
  106. {
  107. turnleft();
  108. System.out.println("turnleft");
  109. }
  110. }
  111. }
  112. }
  113. while(ey != y)
  114. {
  115. if(y < ey)
  116. {
  117. if(rotation == 0)
  118. {
  119. vooruit();
  120. System.out.println("vooruit");
  121. }
  122. else
  123. {
  124. turnright();
  125. System.out.println("turnright");
  126. }
  127. }
  128. else
  129. {
  130. if(y > ey)
  131. {
  132. if(rotation == 180)
  133. {
  134. vooruit();
  135. System.out.println("vooruit");
  136. }
  137. else
  138. {
  139. turnright();
  140. System.out.println("turnright");
  141. }
  142. }
  143. }
  144. }
  145. return "bestemming bereikt " + geefCoordinaten();
  146. }
  147. public char[] berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie)
  148. {
  149. char[] route = new char[40];
  150. int teller = 0;
  151. int links = 0;
  152. x = beginx;
  153. y = beginy;
  154. rotation = rotatie;
  155. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  156. {
  157. ex = eindx;
  158. ey = eindy;
  159. }else{
  160. return route;
  161. }
  162. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
  163. {
  164. x = beginx;
  165. y = beginy;
  166. }else{
  167. return route;
  168. }
  169. while(ex != x)
  170. {
  171. if(x < ex)
  172. {
  173. if(rotation == 90)
  174. {
  175. vooruit();
  176. route[teller] = 'v';
  177. teller ++;
  178. }
  179. if(rotation == 0)
  180. {
  181. turnright();
  182. vooruit();
  183. route[teller] = 'r';
  184. teller ++;
  185. }
  186. if(rotation == 270)
  187. {
  188. keren();
  189. vooruit();
  190. route[teller] = 'a';
  191. teller ++;
  192. }
  193. if(rotation == 180)
  194. {
  195. turnleft();
  196. vooruit();
  197. route[teller] = 'l';
  198. teller ++;
  199. }
  200. }
  201. else
  202. {
  203. if(x > ex)
  204. {
  205. if(rotation == 270)
  206. {
  207. vooruit();
  208. route[teller] = 'v';
  209. teller ++;
  210. }
  211. if(rotation == 0)
  212. {
  213. turnright();
  214. vooruit();
  215. route[teller] = 'l';
  216. teller ++;
  217. }
  218. if(rotation == 90)
  219. {
  220. keren();
  221. vooruit();
  222. route[teller] = 'a';
  223. teller ++;
  224. }
  225. if(rotation == 180)
  226. {
  227. turnleft();
  228. vooruit();
  229. route[teller] = 'r';
  230. teller ++;
  231. }
  232. }
  233. }
  234. }
  235. while(ey != y)
  236. {
  237. if(y < ey)
  238. {
  239. if(rotation == 0)
  240. {
  241. vooruit();
  242. route[teller] = 'v';
  243. teller ++;
  244. }
  245. if(rotation == 270)
  246. {
  247. turnright();
  248. vooruit();
  249. route[teller] = 'r';
  250. teller ++;
  251. }
  252. if(rotation == 180)
  253. {
  254. keren();
  255. vooruit();
  256. route[teller] = 'a';
  257. teller ++;
  258. }
  259. if(rotation == 90)
  260. {
  261. turnleft();
  262. vooruit();
  263. route[teller] = 'l';
  264. teller ++;
  265. }
  266. }
  267. else
  268. {
  269. if(y > ey)
  270. {
  271. if(rotation == 180)
  272. {
  273. vooruit();
  274. route[teller] = 'v';
  275. teller ++;
  276. }
  277. if(rotation == 90)
  278. {
  279. turnright();
  280. vooruit();
  281. route[teller] = 'r';
  282. teller ++;
  283. }
  284. if(rotation == 0)
  285. {
  286. keren();
  287. vooruit();
  288. route[teller] = 'a';
  289. teller ++;
  290. }
  291. if(rotation == 270)
  292. {
  293. turnleft();
  294. vooruit();
  295. route[teller] = 'l';
  296. teller ++;
  297. }
  298. }
  299. }
  300. }
  301. System.out.println("bestemming bereikt " + geefCoordinaten());
  302. return route;
  303. }
  304. }