To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.0/src/ants/GridVector.java. This is a spam-protection measure; sorry for the inconvenience.
· GridVector.java ·
1/* 2 * GridVector.java 3 * 4 * Created on 24 November 2006, 17:55 5 * 6 * To change this template, choose Tools | Template Manager 7 * and open the template in the editor. 8 */ 9 10package ants; 11 12/** 13 * 14 * @author James Hamilton 15 */ 16public class GridVector extends java.util.Vector<GridSquare> { 17 18 /** Creates a new instance of GridVector */ 19 public GridVector() { 20 super(); 21 } 22 23 public double getFoodStrength() { 24 double sum = 0; 25 for(GridSquare gs : this) 26 sum += gs.getFoodStrength(); 27 return sum; 28 } 29 30 public double getPheromoneStrength() { 31 double sum = 0; 32 for(GridSquare gs : this) 33 sum += gs.getPheromoneStrength(); 34 return sum; 35 } 36 37 public int getPheromoneStrength(int direction) { 38 int sum = 0; 39 for(GridSquare gs : this) 40 if(gs.getPheromone().getDirection() == direction) 41 sum += gs.getPheromoneStrength(); 42 return sum; 43 } 44 45 public boolean containsFood() { 46 return getFoodGridSquares().size() != 0; 47 } 48 49 public GridVector getGridSquaresInDirection(GridSquare gs1, int direction) { 50 GridVector gridVector = new GridVector(); 51 for(GridSquare gs : this) 52 if(gs1.getDirectionOf(gs) == direction || 53 gs1.getDirectionOf(gs) == GridSquare.getPreviousDirection(direction) || 54 gs1.getDirectionOf(gs) == GridSquare.getNextDirection(direction)) 55 gridVector.add(gs); 56 return gridVector; 57 } 58 59 public GridVector getFoodGridSquares() { 60 GridVector gridVector = new GridVector(); 61 for(GridSquare gs : this) 62 if(gs.containsFood()) 63 gridVector.add(gs); 64 return gridVector; 65 } 66 67 public GridVector getFoodStrengthGridSquares() { 68 GridVector gridVector = new GridVector(); 69 for(GridSquare gs : this) 70 if(gs.getFoodStrength() > 0) 71 gridVector.add(gs); 72 return gridVector; 73 } 74 75 public boolean containsNest() { 76 GridVector gridVector = new GridVector(); 77 for(GridSquare gs : this) 78 if(gs.isNest()) 79 return true; 80 return false; 81 } 82 83 public GridVector getPheromoneGridSquares() { 84 GridVector gridVector = new GridVector(); 85 for(GridSquare gs : this) 86 if(gs.getPheromoneStrength() > 0) 87 gridVector.add(gs); 88 return gridVector; 89 } 90 91 public GridVector getPheromoneGridSquares(int direction) { 92 GridVector gridVector = new GridVector(); 93 for(GridSquare gs : this) 94 if(gs.getPheromoneStrength() > 0 && gs.getPheromone().getDirection() == direction) 95 gridVector.add(gs); 96 return gridVector; 97 } 98 99 public GridVector getPheromoneGridSquares(int direction, Ant ant) { 100 GridVector gridVector = new GridVector(); 101 for(GridSquare gs : this) 102 if(gs.getPheromoneStrength() > 0 && gs.getPheromone().getDirection() == direction && gs.getPheromone().laidBy(ant)) 103 gridVector.add(gs); 104 return gridVector; 105 } 106 107 public GridSquare getGridSquare(Ant ant) { 108 for(GridSquare gs : this) 109 if(gs.getPheromoneStrength() > 0 && gs.getPheromone().laidBy(ant)) 110 return gs; 111 return null; 112 } 113 114 public GridVector getPheromoneGridSquares(Ant ant, boolean own) { 115 GridVector gridVector = new GridVector(); 116 if(own) { 117 for(GridSquare gs : this) 118 if(gs.getPheromoneStrength() > 0 && gs.getPheromone().laidBy(ant)) 119 gridVector.add(gs); 120 }else{ 121 for(GridSquare gs : this) 122 if(gs.getPheromoneStrength() > 0 && !gs.getPheromone().laidBy(ant)) 123 gridVector.add(gs); 124 125 } 126 return gridVector; 127 } 128 129 public GridVector getPheromoneGridSquares(Ant ant) { 130 return getPheromoneGridSquares(ant, true); 131 } 132 133 public GridVector getFreeGridSquares() { 134 GridVector gridVector = new GridVector(); 135 136 for(GridSquare gs : this) 137 if(!gs.containsObstacle() && !gs.containsAnt()) 138 gridVector.add(gs); 139 return gridVector; 140 } 141 142 public GridVector getEmptyGridSquares() { 143 GridVector gridVector = new GridVector(); 144 for(GridSquare gs : this) 145 if(gs.getObject() == null || gs.isNest()) 146 gridVector.add(gs); 147 return gridVector; 148 } 149 150 public GridVector getGridSquaresWithoutNest() { 151 GridVector gridVector = new GridVector(); 152 for(GridSquare gs : this) 153 if(!gs.isNest()) 154 gridVector.add(gs); 155 return gridVector; 156 } 157 158 public GridSquare getRandomGridSquare() { 159 return elementAt((int)(Math.random() * size())); 160 } 161 162 public GridSquare getRandomGridSquare(boolean bias) { 163 return elementAt((int)(Math.random() * Math.sqrt(size())) * (int)(Math.random() *Math.sqrt(size()))); 164 } 165 166 public GridSquare getMiddleGridSquare() { 167 return elementAt((int)Math.floor(size() / 2)); 168 } 169 170 public GridSquare firstElementNot(GridSquare otherGS) { 171 172 for(GridSquare gs : this) 173 if(!gs.equals(otherGS)) 174 return gs; 175 return firstElement(); 176 } 177 178 public void addObstacles() { 179 for(GridSquare gs : this) 180 gs.setObject(new Obstacle(gs)); 181 } 182 183 public void addFood() { 184 for(GridSquare gs : this) 185 gs.setObject(new Food(gs)); 186 } 187} 188
· GridVector.java ends ·
To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.0/src/ants/GridVector.java. This is a spam-protection measure; sorry for the inconvenience.