Wednesday, April 25, 2012

More Game of life code




package gameoflifeiversion;


import java.util.Collection;


/**
 *
 * @author noel
 */
public interface ICellColl {
    public ICell findCell(int x, int y);
    public ICell findCell (ICell cell);
    public Collection<ICell> getNeighbours(ICell cellToFind);   
    public Collection<ICell> getNeighbours(int x, int y);   
    public void addCell(ICell cell);
    
    public static class Extent{
        private int leastx = 1;
        private int greatestx = 1;
        private int leasty = 1;
        private int greatesty = 1;


        /**
         * @return the leastx
         */
        public int getLeastx() {
            return leastx;
        }


        /**
         * @param leastx the leastx to set
         */
        public void setLeastx(int in) {
            leastx = in;
        }


        /**
         * @return the greatestx
         */
        public  int getGreatestx() {
            return greatestx;
        }


        /**
         * @param greatestx the greatestx to set
         */
        public void setGreatestx(int in) {
            greatestx = in;
        }


        /**
         * @return the leasty
         */
        public  int getLeasty() {
            return leasty;
        }


        /**
         * @param leasty the leasty to set
         */
        public void setLeasty(int in) {
            leasty = in;
        }


        /**
         * @return the greatesty
         */
        public int getGreatesty() {
            return greatesty;
        }


        /**
         * @param greatesty the greatesty to set
         */
        public void setGreatesty(int in) {
            greatesty = in;
        }
    }
}






/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;


/**
 *
 * @author noel
 */
public abstract class AbstractICellColl  implements ICellColl {


    public AbstractICellColl() {
    }
    private ICellColl.Extent extent = new ICellColl.Extent(); 
    @Override
    public void addCell(ICell cell){
       
        if (cell.getX() < extent.getLeastx()) {
            extent.setLeastx(cell.getX());
        }
        if (cell.getX() > extent.getGreatestx()) {
            extent.setGreatestx(cell.getX());
        }
        if (cell.getY() < extent.getLeasty()) {
            extent.setLeasty(cell.getY());
        }
        if (cell.getY() > extent.getGreatesty()) {
            extent.setGreatesty(cell.getY());
        }
    }


    /**
     * @return the extent
     */
    public ICellColl.Extent getExtent() {
        return extent;
    }


    
    
}


package gameoflifeiversion;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

/**
 *
 * @author noel
 */
public class ArrayListCellColl extends AbstractICellColl{

    private ArrayList<ICell> coll = new ArrayList<ICell>();
    private boolean hasNewCell = false; 
    
    @Override
    public ICell findCell(int x, int y) {
        AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return findCell(ac);
        
    }

    @Override
    public ICell findCell(ICell cell) {
        if (hasNewCell){
           Collections.sort(coll);
           hasNewCell = false;
        }
        if (cell == null) return null;
        int index = Collections.binarySearch(this.coll, cell);
        if (index < 0 )return null;
        return coll.get(index);
        
        
    }

    @Override
    public Collection<ICell> getNeighbours(ICell cellToFind) {
         ArrayList<ICell> neighbours = new ArrayList<ICell>();
        ICell addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        return neighbours;

    }

    @Override
    public Collection<ICell> getNeighbours(int x, int y) {
        AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return getNeighbours(ac);
    }

    @Override
    public void addCell(ICell cell) {
        hasNewCell = true;
        coll.add(cell);
       super.addCell(cell);
    }
    
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;


/**
 *
 * @author noel
 */
public class HashMapCellColl extends AbstractICellColl {
private HashMap<ICell, ICell> coll = new HashMap<ICell,ICell>();
    
    @Override
    public ICell findCell(int x, int y) {
       AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return findCell(ac);
    }


    @Override
    public ICell findCell(ICell cell) {
       return coll.get(cell);
    }


   
    @Override
    public Collection<ICell> getNeighbours(ICell cellToFind) {
         ArrayList<ICell> neighbours = new ArrayList<ICell>();
        ICell addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() - 1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY());
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() - 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX(), cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        addCell = findCell(cellToFind.getX() + 1, cellToFind.getY() +1);
        if (addCell != null) {
            neighbours.add(addCell);
        }
        return neighbours;


    }


    @Override
    public Collection<ICell> getNeighbours(int x, int y) {
        AliveCell ac = new AliveCell();
        ac.setX(x);
        ac.setY(y);
        return getNeighbours(ac);
    }


    @Override
    public void addCell(ICell cell){
        if (cell==null) return;
        super.addCell(cell);
        coll.put(cell, cell);
    }
    
}




No comments:

Post a Comment