Routeplanner.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. private int lengte;
  12. public Routeplanner(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[] berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie)
  75. {
  76. String[] route = new String[40];
  77. int teller = 0;
  78. int links = 0;
  79. rotation = rotatie;
  80. lengte = 0;
  81. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0)
  82. {
  83. ex = eindx;
  84. ey = eindy;
  85. }else{
  86. return route;
  87. }
  88. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0)
  89. {
  90. x = beginx;
  91. y = beginy;
  92. }else{
  93. return route;
  94. }
  95. while(ex != x)
  96. {
  97. if(x < ex)
  98. {
  99. if(rotation == 90)
  100. {
  101. vooruit();
  102. route[teller] = "vooruit";
  103. teller ++;
  104. }
  105. if(rotation == 0)
  106. {
  107. turnright();
  108. vooruit();
  109. route[teller] = "rechtsaf";
  110. teller ++;
  111. }
  112. if(rotation == 270)
  113. {
  114. keren();
  115. vooruit();
  116. route[teller] = "keren";
  117. teller ++;
  118. }
  119. if(rotation == 180)
  120. {
  121. turnleft();
  122. vooruit();
  123. route[teller] = "linksaf";
  124. teller ++;
  125. }
  126. }
  127. else
  128. {
  129. if(x > ex)
  130. {
  131. if(rotation == 270)
  132. {
  133. vooruit();
  134. route[teller] = "vooruit";
  135. teller ++;
  136. }
  137. if(rotation == 0)
  138. {
  139. turnleft();
  140. vooruit();
  141. route[teller] = "linksaf";
  142. teller ++;
  143. }
  144. if(rotation == 90)
  145. {
  146. keren();
  147. vooruit();
  148. route[teller] = "keren";
  149. teller ++;
  150. }
  151. if(rotation == 180)
  152. {
  153. turnright();
  154. vooruit();
  155. route[teller] = "rechtsaf";
  156. teller ++;
  157. }
  158. }
  159. }
  160. }
  161. while(ey != y)
  162. {
  163. if(y < ey)
  164. {
  165. if(rotation == 0)
  166. {
  167. vooruit();
  168. route[teller] = "vooruit";
  169. teller ++;
  170. }
  171. if(rotation == 270)
  172. {
  173. turnright();
  174. vooruit();
  175. route[teller] = "rechtsaf";
  176. teller ++;
  177. }
  178. if(rotation == 180)
  179. {
  180. keren();
  181. vooruit();
  182. route[teller] = "keren";
  183. teller ++;
  184. }
  185. if(rotation == 90)
  186. {
  187. turnleft();
  188. vooruit();
  189. route[teller] = "linksaf";
  190. teller ++;
  191. }
  192. }
  193. else
  194. {
  195. if(y > ey)
  196. {
  197. if(rotation == 180)
  198. {
  199. vooruit();
  200. route[teller] = "vooruit";
  201. teller ++;
  202. }
  203. if(rotation == 90)
  204. {
  205. turnright();
  206. vooruit();
  207. route[teller] = "rechtsaf";
  208. teller ++;
  209. }
  210. if(rotation == 0)
  211. {
  212. keren();
  213. vooruit();
  214. route[teller] = "keren";
  215. teller ++;
  216. }
  217. if(rotation == 270)
  218. {
  219. turnleft();
  220. vooruit();
  221. route[teller] = "linksaf";
  222. teller ++;
  223. }
  224. }
  225. }
  226. }
  227. lengte = teller;
  228. teller = 0;
  229. while(teller < lengte)
  230. {
  231. System.out.println(route[teller]);
  232. teller ++;
  233. }
  234. System.out.println("Planning is met succes gemaakt" + geefCoordinaten());
  235. return route;
  236. }
  237. public int geefLengte()
  238. {
  239. return lengte;
  240. }
  241. public String[] ontwijk(int xx, int xy, int x, int y, int rotation)
  242. {
  243. this.x = x;
  244. this.y = y;
  245. String route[] = new String[4];
  246. int teller = 0;
  247. boolean ontwijk = false;
  248. if ((ey > xy && x < maxx && x == ex) || (ex < xx && y < maxy) && ontwijk == false)
  249. {
  250. turnright();
  251. vooruit();
  252. route[teller] = "rechtsaf";
  253. teller ++;
  254. turnleft();
  255. vooruit();
  256. route[teller] = "linksaf";
  257. teller ++;
  258. vooruit();
  259. route[teller] = "vooruit";
  260. teller ++;
  261. turnleft();
  262. vooruit();
  263. route[teller] = "linksaf";
  264. teller ++;
  265. ontwijk = true;
  266. }
  267. if (((ex > xx && y < maxy) || (ey < xy && x < maxx)) && ontwijk == false)
  268. {
  269. turnleft();
  270. vooruit();
  271. route[teller] = "linksaf";
  272. teller ++;
  273. turnright();
  274. vooruit();
  275. route[teller] = "rechtsaf";
  276. teller ++;
  277. vooruit();
  278. route[teller] = "vooruit";
  279. teller ++;
  280. turnright();
  281. vooruit();
  282. route[teller] = "rechtsaf";
  283. teller ++;
  284. ontwijk = true;
  285. }
  286. if (ex == xx && ex == xy)
  287. {
  288. System.out.println("ERROR: obstakel op eindstation");
  289. }
  290. lengte = teller;
  291. teller = 0;
  292. while(teller < lengte)
  293. {
  294. System.out.println(route[teller]);
  295. teller ++;
  296. }
  297. return route;
  298. }
  299. public char[] ontwijkBoebot(int xx, int xy, int x, int y, int rotation)
  300. {
  301. this.x = x;
  302. this.y = y;
  303. char route[] = new char[4];
  304. int teller = 0;
  305. boolean ontwijk = false;
  306. if ((ey > xy && x < maxx && x == ex) || (ex < xx && y < maxy) && ontwijk == false)
  307. {
  308. turnright();
  309. vooruit();
  310. route[teller] = 'r';
  311. teller ++;
  312. turnleft();
  313. vooruit();
  314. route[teller] = 'l';
  315. teller ++;
  316. turnleft();
  317. vooruit();
  318. route[teller] = 'l';
  319. teller ++;
  320. ontwijk = true;
  321. }
  322. if (((ex > xx && y < maxy) || (ey < xy && x < maxx)) && ontwijk == false)
  323. {
  324. turnleft();
  325. vooruit();
  326. route[teller] = 'l';
  327. teller ++;
  328. turnright();
  329. vooruit();
  330. route[teller] = 'r';
  331. teller ++;
  332. turnright();
  333. vooruit();
  334. route[teller] = 'r';
  335. teller ++;
  336. ontwijk = true;
  337. }
  338. if (ex == xx && ex == xy)
  339. {
  340. System.out.println("ERROR: obstakel op eindstation");
  341. }
  342. teller = 0;
  343. return route;
  344. }
  345. }