Aansturing.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package boebot;
  2. public class Aansturing{
  3. private int rotation, x, y;
  4. private int maxx, maxy;
  5. private int ex, ey;
  6. public Aansturing(int maxx, int maxy){
  7. this.maxx = maxx;
  8. this.maxy = maxy;
  9. rotation = 0;
  10. x = 0;
  11. y = 0;
  12. ex = 0;
  13. ey = 0;
  14. }
  15. public void turnleft(){
  16. rotation = rotation - 90;
  17. if (rotation < 0){
  18. rotation = 270;
  19. }
  20. }
  21. public void turnright(){
  22. rotation = rotation + 90;
  23. if (rotation > 270){
  24. rotation = 0;
  25. }
  26. }
  27. public void vooruit(){
  28. if (rotation == 0 && y < maxy){
  29. y ++;
  30. }
  31. if (rotation == 90 && x < maxx){
  32. x ++;
  33. }
  34. if (rotation == 180 && y > 0){
  35. y --;
  36. }
  37. if (rotation == 270 && x > 0){
  38. x --;
  39. }
  40. }
  41. public char[] berekenRoute(int eindx, int eindy, int beginx, int beginy, int rotatie){
  42. char[] route = new char[40];
  43. int teller = 0;
  44. x = beginx;
  45. y = beginy;
  46. rotation = rotatie;
  47. if(eindx <= maxx && eindy <= maxy && eindx >= 0 && eindy >= 0){
  48. ex = eindx;
  49. ey = eindy;
  50. }else{
  51. return route;
  52. }
  53. if(beginx <= maxx && beginy <= maxy && beginx >= 0 && beginy >= 0){
  54. x = beginx;
  55. y = beginy;
  56. }else{
  57. return route;
  58. }
  59. while(ex != x){
  60. if(x < ex){
  61. if(rotation == 90){
  62. vooruit();
  63. route[teller] = 'v';
  64. System.out.println("vooruit");
  65. teller ++;
  66. }else{
  67. turnleft();
  68. route[teller] = 'l';
  69. System.out.println("turnleft");
  70. teller ++;
  71. }
  72. }else{
  73. if(x > ex){
  74. if(rotation == 270){
  75. vooruit();
  76. route[teller] = 'v';
  77. System.out.println("vooruit");
  78. teller ++;
  79. }else{
  80. turnleft();
  81. route[teller] ='l';
  82. System.out.println("turnleft");
  83. teller ++;
  84. }
  85. }
  86. }
  87. }
  88. while(ey != y){
  89. if(y < ey){
  90. if(rotation == 0){
  91. vooruit();
  92. route[teller] = 'v';
  93. System.out.println("vooruit");
  94. teller ++;
  95. }else{
  96. turnright();
  97. route[teller] = 'r';
  98. System.out.println("turnright");
  99. teller ++;
  100. }
  101. }else{
  102. if(y > ey){
  103. if(rotation == 180){
  104. vooruit();
  105. route[teller] = 'v';
  106. System.out.println("vooruit");
  107. teller ++;
  108. }else{
  109. turnright();
  110. route[teller] = 'r';
  111. System.out.println("turnright");
  112. teller ++;
  113. }
  114. }
  115. }
  116. }
  117. return route;
  118. }
  119. }