To load this file without formatting, visit http://whoyouknow.co.uk/ants/java/17.1/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 && 
 103		gs.getPheromone().getDirection() == direction && 
 104		gs.getPheromone().laidBy(ant))
 105                	gridVector.add(gs);
 106        return gridVector;
 107    }   
 108    
 109    public GridSquare getGridSquare(Ant ant) {
 110        for(GridSquare gs : this)
 111            if(gs.getPheromoneStrength() > 0 && gs.getPheromone().laidBy(ant))
 112                return gs;
 113        return null;       
 114    }
 115    
 116    public GridVector getPheromoneGridSquares(Ant ant, boolean own) {
 117        GridVector gridVector = new GridVector();
 118        if(own) {
 119            for(GridSquare gs : this)
 120                if(gs.getPheromoneStrength() > 0 && gs.getPheromone().laidBy(ant))
 121                    gridVector.add(gs);
 122        }else{
 123             for(GridSquare gs : this)
 124                if(gs.getPheromoneStrength() > 0 && !gs.getPheromone().laidBy(ant))
 125                    gridVector.add(gs);           
 126            
 127        }
 128        return gridVector;
 129    }
 130    
 131    public GridVector getPheromoneGridSquares(Ant ant) {
 132        return getPheromoneGridSquares(ant, true);
 133    }    
 134    
 135    public GridVector getFreeGridSquares() {
 136        GridVector gridVector = new GridVector();
 137        
 138        for(GridSquare gs : this)
 139            if(!gs.containsObstacle() && !gs.containsAnt())
 140                gridVector.add(gs);
 141        return gridVector;
 142    }
 143
 144    public GridVector getEmptyGridSquares() {
 145        GridVector gridVector = new GridVector();
 146        for(GridSquare gs : this)
 147            if(gs.getObject() == null || gs.isNest())
 148                gridVector.add(gs);
 149        return gridVector;
 150    }
 151    
 152    public GridVector getGridSquaresWithoutNest() {
 153        GridVector gridVector = new GridVector();
 154        for(GridSquare gs : this)
 155            if(!gs.isNest())
 156                gridVector.add(gs);
 157        return gridVector;           
 158    }
 159    
 160    public GridSquare getRandomGridSquare() {
 161        return elementAt((int)(Math.random() * size()));
 162    }
 163    
 164    public GridSquare getRandomGridSquare(boolean bias) {
 165        return elementAt((int)(Math.random() * Math.sqrt(size())) * (int)(Math.random() *Math.sqrt(size())));
 166    }
 167    
 168    public GridSquare getMiddleGridSquare() {
 169        return elementAt((int)Math.floor(size() / 2));
 170    }
 171    
 172    public GridSquare firstElementNot(GridSquare otherGS) {
 173        
 174        for(GridSquare gs : this)
 175            if(!gs.equals(otherGS))
 176                 return gs;       
 177       return firstElement();
 178    }
 179
 180    public void addObstacles() {
 181        for(GridSquare gs : this)
 182            gs.setObject(new Obstacle(gs));    
 183    }
 184    
 185    public void addFood() {
 186        for(GridSquare gs : this)
 187            gs.setObject(new Food(gs));    
 188    }
 189}
 190

· GridVector.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.