Guest User

Untitled

a guest
Feb 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. import java.awt.Point;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.Random;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import vacuumcleaner.base.Direction;
  9. import vacuumcleaner.base.EnvironmentBase;
  10.  
  11. public class MyEnvironment extends EnvironmentBase {
  12.  
  13. private int getHeight = MAXROOMSIZE;
  14. private int getWidth = MAXROOMSIZE;
  15. private static final int MAXROOMSIZE = 50;
  16. private boolean[][] roomMatrix;
  17. private boolean[][] dirtMatrix;
  18. private Direction[] directions = new Direction[] { Direction.Right,
  19. Direction.Down, Direction.Left, Direction.Up };
  20.  
  21. public MyEnvironment() {
  22. super("MyEnvironment");
  23. dirtMatrix = new boolean[this.getWidth()][this.getHeight()];
  24. initRandomRoom();
  25.  
  26. int x = 0;
  27. int y = 0;
  28.  
  29. while (x >= getWidth || y >= getHeight || !isRoom(x, y)) { // startpos zufall //TODO darf nat�rl noch nicht room pr�fen wenn x und y nicht kleiner sind
  30. x = random();
  31. y = random();
  32. }
  33.  
  34. this.setInitialAgentLocation(new Point((int) x, (int) y)); // startpos
  35.  
  36. x = 0;
  37. y = 0;
  38. while ( x >= getWidth || y >= getHeight || !isRoom(x, y) ) { // homebase zufall
  39. x = random();
  40. y = random();
  41. }
  42. this.initAgentHome(new Point((int) x, (int) y)); // homebase
  43.  
  44. double directionRandom = Math.random();
  45. directionRandom = ((int) (directionRandom * 10)) % directions.length;
  46. this.setInitialDirection(directions[(int) directionRandom]); // blickrichtung zufall
  47. System.out.println("Blickrichtung: " + directions[(int) directionRandom]);
  48.  
  49. try {
  50. this.save(new File("map.txt"));
  51. } catch (IOException ex) {
  52. Logger.getLogger(MyEnvironment.class.getName()).log(Level.SEVERE,
  53. null, ex);
  54. }
  55.  
  56. initRandomDirt();
  57. }
  58.  
  59. protected void cleanDirt(int x, int y) { // einfach nur auf gecleant setzten
  60. if (dirtMatrix[x][y]) {
  61. dirtMatrix[x][y] = false;
  62. }
  63. }
  64.  
  65. public boolean containsDirt(int x, int y) { // abfrage ob dirt oder nicht auf diesem block
  66. if (x == 0 && y == 0) {
  67. try {
  68. this.save(new File("map.txt"));
  69. } catch (IOException ex) {
  70. Logger.getLogger(MyEnvironment.class.getName()).log(
  71. Level.SEVERE, null, ex);
  72. }
  73. }
  74. //System.out.println(getHeight + " " + y + " " + getWidth + " " + x);
  75. return dirtMatrix[x][y];
  76. }
  77.  
  78. public int getHeight() {
  79. if (getHeight == MAXROOMSIZE) {
  80. getHeight = -1;
  81. while ((int) getHeight < 4) { // muss mindestens 4 hoch sein
  82. getHeight = random();
  83. }
  84. }
  85. return getHeight;
  86. }
  87.  
  88. public int getWidth() {
  89. if (getWidth == MAXROOMSIZE) {
  90. getWidth = -1;
  91. while ((int) getWidth < 4) { // muss mindestens 4 breit sein
  92. getWidth = random();
  93. }
  94. }
  95. return getWidth;
  96. }
  97.  
  98. public boolean isRoom(int x, int y) { // Hindernisse, L basteln
  99. return roomMatrix[x][y];
  100. }
  101.  
  102. private int random() {
  103. return new Random().nextInt(MAXROOMSIZE+1);//(int) Math.floor((Math.random() * MAXROOMSIZE) + 1);
  104. }
  105.  
  106. private void initRandomRoom() {
  107. roomMatrix = new boolean[getWidth][getHeight];
  108.  
  109. int lX = getWidth + 1;
  110. int lY = getHeight + 1;
  111. while ((lX > getWidth - 1 - 2 - 1)) { //-1 ansosnten outOfBounce, -2 die beiden r�nder, -1 mindestens eins kleiner als der Raum
  112. lX = random();
  113. }
  114. while ((lY > getHeight - 1 - 2 - 1)) { //-1 ansosnten outOfBounce, -2 die beiden r�nder, -1 mindestens eins kleiner als der Raum
  115. lY = random();
  116. }
  117.  
  118. int quadrant = random() % 4; //rechts oben 0; links oben 1; links unten 2; rechts unten 3
  119.  
  120. for (int x = 0; x < getWidth - 1; x++) {
  121. for (int y = 0; y < getHeight - 1; y++) {
  122. roomMatrix[x][y] = true;
  123. switch (quadrant) {
  124. case 0:
  125. if ((x > (getWidth - 2 - lX)) && (y < (1 + lY))) {
  126. roomMatrix[x][y] = false;
  127. }
  128. break;
  129. case 1:
  130. if ((x < (1 + lX)) && (y < (1 + lY))) {
  131. roomMatrix[x][y] = false;
  132. }
  133. break;
  134. case 2:
  135. if ((x < (1 + lX)) && (y > getHeight - 2 - lY)) {
  136. roomMatrix[x][y] = false;
  137. }
  138. break;
  139. case 3:
  140. if ((x > (getWidth - 2 - lX)) && (y > getHeight - 2 - lY)) {
  141. roomMatrix[x][y] = false;
  142. }
  143. break;
  144.  
  145. }
  146. if (x == 0 || y == 0) { //rand oben und links
  147. roomMatrix[x][y] = false;
  148. }
  149. if (x == getWidth - 1 || y == getHeight - 1) { //rand rechts und unten
  150. roomMatrix[x][y] = false;
  151. }
  152. }
  153. }
  154. }
  155.  
  156. private void initRandomDirt() { // random dirt erstellen auf der map
  157. double dirt;
  158. for (int x = 0; x < getWidth; x++) {
  159. for (int y = 0; y < getHeight; y++) {
  160. if (isRoom(x, y)) {
  161. dirt = random() % 2;
  162. if ((int) dirt == 1) {
  163. dirtMatrix[x][y] = true;
  164. } else {
  165. dirtMatrix[x][y] = false;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
Add Comment
Please, Sign In to add comment