import java.util.*;

public class RandomNeighborhood implements NeighborhoodConstructor {
    static ArrayList collection;
    int size;

    public RandomNeighborhood(int s) {
        size = s;
    }

    public LinkedList getNeighborhood(int i,
                                      int j,
                                      int width,
                                      int height,
                                      Agent[][] world,
                                      boolean wrap)
    {
        int k,l;

        // The following hack lets us know whether we have a new
        // population and thus need to recreate the collection
        if (collection != null && collection.contains(world[0][0])) {
            LinkedList nbhd = new LinkedList();
            for (k=0; k<size; k++) {
                nbhd.add( collection.get(Parameters.random.nextInt(collection.size())) );
            }
            return nbhd;
        }
        else {
            collection = new ArrayList(width*height);
            for (k=0; k<width; k++) {
                for (l=0; l<height; l++) {
                    collection.add(world[k][l]);
                }
            }
        }

        LinkedList nbhd = new LinkedList();
        for (k=0; k<size; k++) {
            nbhd.add( collection.get(Parameters.random.nextInt(collection.size())) );
        }
        return nbhd;
    }
}
