Aansturing.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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()
  13. {
  14. maxx = 0;
  15. maxy = 0;
  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, int maxxInput, int maxyInput)
  148. {
  149. maxx = maxxInput;
  150. maxy = maxyInput;
  151. char[] route = new char[40];
  152. int teller = 0;
  153. int links = 0;
  154. x = beginx;
  155. y = beginy;
  156. rotation = rotatie;
  157. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  158. {
  159. ex = eindx;
  160. ey = eindy;
  161. }else{
  162. return route;
  163. }
  164. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
  165. {
  166. x = beginx;
  167. y = beginy;
  168. }else{
  169. return route;
  170. }
  171. while(ex != x)
  172. {
  173. if(x < ex)
  174. {
  175. if(rotation == 90)
  176. {
  177. vooruit();
  178. route[teller] = 'v';
  179. teller ++;
  180. }
  181. if(rotation == 0)
  182. {
  183. turnright();
  184. vooruit();
  185. route[teller] = 'r';
  186. teller ++;
  187. }
  188. if(rotation == 270)
  189. {
  190. keren();
  191. vooruit();
  192. route[teller] = 'a';
  193. teller ++;
  194. }
  195. if(rotation == 180)
  196. {
  197. turnleft();
  198. vooruit();
  199. route[teller] = 'l';
  200. teller ++;
  201. }
  202. }
  203. else
  204. {
  205. if(x > ex)
  206. {
  207. if(rotation == 270)
  208. {
  209. vooruit();
  210. route[teller] = 'v';
  211. teller ++;
  212. }
  213. if(rotation == 0)
  214. {
  215. turnright();
  216. vooruit();
  217. route[teller] = 'l';
  218. teller ++;
  219. }
  220. if(rotation == 90)
  221. {
  222. keren();
  223. vooruit();
  224. route[teller] = 'a';
  225. teller ++;
  226. }
  227. if(rotation == 180)
  228. {
  229. turnleft();
  230. vooruit();
  231. route[teller] = 'r';
  232. teller ++;
  233. }
  234. }
  235. }
  236. }
  237. while(ey != y)
  238. {
  239. if(y < ey)
  240. {
  241. if(rotation == 0)
  242. {
  243. vooruit();
  244. route[teller] = 'v';
  245. teller ++;
  246. }
  247. if(rotation == 270)
  248. {
  249. turnright();
  250. vooruit();
  251. route[teller] = 'r';
  252. teller ++;
  253. }
  254. if(rotation == 180)
  255. {
  256. keren();
  257. vooruit();
  258. route[teller] = 'a';
  259. teller ++;
  260. }
  261. if(rotation == 90)
  262. {
  263. turnleft();
  264. vooruit();
  265. route[teller] = 'l';
  266. teller ++;
  267. }
  268. }
  269. else
  270. {
  271. if(y > ey)
  272. {
  273. if(rotation == 180)
  274. {
  275. vooruit();
  276. route[teller] = 'v';
  277. teller ++;
  278. }
  279. if(rotation == 90)
  280. {
  281. turnright();
  282. vooruit();
  283. route[teller] = 'r';
  284. teller ++;
  285. }
  286. if(rotation == 0)
  287. {
  288. keren();
  289. vooruit();
  290. route[teller] = 'a';
  291. teller ++;
  292. }
  293. if(rotation == 270)
  294. {
  295. turnleft();
  296. vooruit();
  297. route[teller] = 'l';
  298. teller ++;
  299. }
  300. }
  301. }
  302. }
  303. System.out.println("bestemming bereikt " + geefCoordinaten());
  304. return route;
  305. }
  306. }