class World { ArrayList ants = new ArrayList(); int initNumberOfAnts; int[][][] fieldMem; // // layer 0: ammount of ants // layer 1: obstacles ArrayList fields = new ArrayList(); final int rows; final int cols; int nestX; int nestY; int nestSize =0; int layers; World(int c, int r, int l, int numOfAnts) { rows = r; cols = c; layers = l; initNumberOfAnts = numOfAnts; fieldMem = new int[c][r][l]; setupField(c, r, l); createNest(4, 7); // printField(1); //DEBUGGING } //METHODS ################################################################################### void initializeFields(PImage foodImage) { int[][] foodArray; foodArray = initFoodByPicture(foodImage); boolean obstacle = false; for (int j=0; j< getHeight(); j++) { for (int i=0; i< getWidth(); i++) { if (fieldMem[i][j][1] == 9) obstacle = true; else obstacle = false; int nestSmell = fieldMem[i][j][2]; addField(new Field(i, j, obstacle, foodArray[i][j], nestSmell, 0)); } } } Object getField(int x, int y) { int index = x + y*getWidth(); Field currField = (Field) fields.get(index); return currField; } void initializeAnts() { for (int i=0; i0 && !currField.isAnObstacle()) { neighbours.add(currField); } } } // bubbleSortOnFoodSmell(neighbours); return neighbours; } ArrayList getBestSmellNeighbours(int currX, int currY) { ArrayList neighbours = new ArrayList(); for (int i=-1; i<=1; i++) { for (int j=-1; j<=1; j++) { Field currField = (Field) getField(currX+i,currY+j); if (!currField.isAnObstacle() && currField != (Field) getField(currX, currY) && currField.foodSmell>0.1) { neighbours.add(currField); } } } bubbleSortOnFoodSmell(neighbours); return neighbours; } void bubbleSortOnFoodSmell(ArrayList array) { int i; // Field tmpField; boolean getauscht; // println("--NEUER AGENT"); do { getauscht = false; // println("NEUE RUNDE von mgl. "+array.size()); for (i=0; i< array.size()-1;i++) { Field currField = (Field) array.get(i); Field nextField = (Field) array.get(i+1); if (currField.getNestSmell() < nextField.getNestSmell()) { Field tmpField; tmpField = currField; array.set(i, nextField); array.set(i+1, tmpField); getauscht = true; // println("tausche No" +i +" gegen No" +(i+1)); } } } while (getauscht); } ArrayList getValidNeighbours(int currX, int currY, int checkLayer) { ArrayList neighbours = new ArrayList(); for (int i=-1; i<=1; i++) { for (int j=-1; j<=1; j++) { Field currField = (Field) getField(currX+i,currY+j); if (!currField.isAnObstacle() && currField != (Field) getField(currX, currY)) { neighbours.add(currField); } } } // println("nachbarn: "+neighbours.size()); bubbleSort(neighbours); return neighbours; } void bubbleSort(ArrayList array) { int i; // Field tmpField; boolean getauscht; // println("--NEUER AGENT"); do { getauscht = false; // println("NEUE RUNDE von mgl. "+array.size()); for (i=0; i< array.size()-1;i++) { Field currField = (Field) array.get(i); Field nextField = (Field) array.get(i+1); if (currField.getNestSmell() > nextField.getNestSmell()) { Field tmpField; tmpField = currField; array.set(i, nextField); array.set(i+1, tmpField); getauscht = true; // println("tausche No" +i +" gegen No" +(i+1)); } } } while (getauscht); } void setupField (int c, int r, int l) { for (int i=0; i=6)&&(i<=20) && j>=4 && j<=20) fieldMem[i][j][k] = 9; //set obstacles in center to test TEST } } } } } void createNest(int centerX, int centerY) { nestX = centerX; nestY = centerY; for (int x=0; x