Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import vacuumcleaner.base.Direction;
- import vacuumcleaner.base.EnvironmentBase;
- public class MyEnvironment extends EnvironmentBase {
- private int getHeight = MAXROOMSIZE;
- private int getWidth = MAXROOMSIZE;
- private static final int MAXROOMSIZE = 50;
- private boolean[][] roomMatrix;
- private boolean[][] dirtMatrix;
- private Direction[] directions = new Direction[] { Direction.Right,
- Direction.Down, Direction.Left, Direction.Up };
- public MyEnvironment() {
- super("MyEnvironment");
- dirtMatrix = new boolean[this.getWidth()][this.getHeight()];
- initRandomRoom();
- int x = 0;
- int y = 0;
- 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
- x = random();
- y = random();
- }
- this.setInitialAgentLocation(new Point((int) x, (int) y)); // startpos
- x = 0;
- y = 0;
- while ( x >= getWidth || y >= getHeight || !isRoom(x, y) ) { // homebase zufall
- x = random();
- y = random();
- }
- this.initAgentHome(new Point((int) x, (int) y)); // homebase
- double directionRandom = Math.random();
- directionRandom = ((int) (directionRandom * 10)) % directions.length;
- this.setInitialDirection(directions[(int) directionRandom]); // blickrichtung zufall
- System.out.println("Blickrichtung: " + directions[(int) directionRandom]);
- try {
- this.save(new File("map.txt"));
- } catch (IOException ex) {
- Logger.getLogger(MyEnvironment.class.getName()).log(Level.SEVERE,
- null, ex);
- }
- initRandomDirt();
- }
- protected void cleanDirt(int x, int y) { // einfach nur auf gecleant setzten
- if (dirtMatrix[x][y]) {
- dirtMatrix[x][y] = false;
- }
- }
- public boolean containsDirt(int x, int y) { // abfrage ob dirt oder nicht auf diesem block
- if (x == 0 && y == 0) {
- try {
- this.save(new File("map.txt"));
- } catch (IOException ex) {
- Logger.getLogger(MyEnvironment.class.getName()).log(
- Level.SEVERE, null, ex);
- }
- }
- //System.out.println(getHeight + " " + y + " " + getWidth + " " + x);
- return dirtMatrix[x][y];
- }
- public int getHeight() {
- if (getHeight == MAXROOMSIZE) {
- getHeight = -1;
- while ((int) getHeight < 4) { // muss mindestens 4 hoch sein
- getHeight = random();
- }
- }
- return getHeight;
- }
- public int getWidth() {
- if (getWidth == MAXROOMSIZE) {
- getWidth = -1;
- while ((int) getWidth < 4) { // muss mindestens 4 breit sein
- getWidth = random();
- }
- }
- return getWidth;
- }
- public boolean isRoom(int x, int y) { // Hindernisse, L basteln
- return roomMatrix[x][y];
- }
- private int random() {
- return new Random().nextInt(MAXROOMSIZE+1);//(int) Math.floor((Math.random() * MAXROOMSIZE) + 1);
- }
- private void initRandomRoom() {
- roomMatrix = new boolean[getWidth][getHeight];
- int lX = getWidth + 1;
- int lY = getHeight + 1;
- while ((lX > getWidth - 1 - 2 - 1)) { //-1 ansosnten outOfBounce, -2 die beiden r�nder, -1 mindestens eins kleiner als der Raum
- lX = random();
- }
- while ((lY > getHeight - 1 - 2 - 1)) { //-1 ansosnten outOfBounce, -2 die beiden r�nder, -1 mindestens eins kleiner als der Raum
- lY = random();
- }
- int quadrant = random() % 4; //rechts oben 0; links oben 1; links unten 2; rechts unten 3
- for (int x = 0; x < getWidth - 1; x++) {
- for (int y = 0; y < getHeight - 1; y++) {
- roomMatrix[x][y] = true;
- switch (quadrant) {
- case 0:
- if ((x > (getWidth - 2 - lX)) && (y < (1 + lY))) {
- roomMatrix[x][y] = false;
- }
- break;
- case 1:
- if ((x < (1 + lX)) && (y < (1 + lY))) {
- roomMatrix[x][y] = false;
- }
- break;
- case 2:
- if ((x < (1 + lX)) && (y > getHeight - 2 - lY)) {
- roomMatrix[x][y] = false;
- }
- break;
- case 3:
- if ((x > (getWidth - 2 - lX)) && (y > getHeight - 2 - lY)) {
- roomMatrix[x][y] = false;
- }
- break;
- }
- if (x == 0 || y == 0) { //rand oben und links
- roomMatrix[x][y] = false;
- }
- if (x == getWidth - 1 || y == getHeight - 1) { //rand rechts und unten
- roomMatrix[x][y] = false;
- }
- }
- }
- }
- private void initRandomDirt() { // random dirt erstellen auf der map
- double dirt;
- for (int x = 0; x < getWidth; x++) {
- for (int y = 0; y < getHeight; y++) {
- if (isRoom(x, y)) {
- dirt = random() % 2;
- if ((int) dirt == 1) {
- dirtMatrix[x][y] = true;
- } else {
- dirtMatrix[x][y] = false;
- }
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment