Wednesday, April 25, 2012

The Following blog posts the code for The Game of life
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gameoflifeiversion;

/**
 *
 * @author noel
 */
public interface ICell extends Comparable{
    @Override
    public int hashCode();
    @Override
    public String toString(); 
    public void setX(int x);
    public void setY(int y);
    public int getX();
    public int getY();
    
    @Override
    public boolean equals(Object c);
    
    @Override
    public int compareTo(ICell cell);
    
}

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

/**
 *
 * @author noel
 */
public abstract class AbstractCell implements ICell{
  private int x;
  private int y;
@Override
    public int compareTo(ICell comparison) {
        
        if (this.getY()comparison.getY()) return 1;
        if (this.getY() == comparison.getY()
                && this.getX() < comparison.getX()) return -1;
        if (this.getY() == comparison.getY()
                && this.getX() > comparison.getX()) return 1;
        return 0;
 
    }
 @Override
 public boolean equals(Object obj){
     if (!(obj instanceof AbstractCell)) return false;
    AbstractCell ac = (AbstractCell)obj;
    return ac.getX() == this.getX() && ac.getY() == this.getY();

 }
  
  
  
  
    @Override
  public int hashCode() {
      int returnValue = 0;
        if (x != 0){
            returnValue = x * 31;
            
        }
        if (y!=0){
         returnValue *= y;   
        }
        return returnValue;
        }
    
    /**
     * @return the x
     */
    public int getX() {
        return x;
    }

    /**
     * @param x the x to set
     */
    public void setX(int x) {
        this.x = x;
    }

    /**
     * @return the y
     */
    public int getY() {
        return y;
    }

    /**
     * @param y the y to set
     */
    public void setY(int y) {
        this.y = y;
    }
    public String toCellString(){
        return getX() + "_" + getY();
    }
    
    
}


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

/**
 *
 * @author noel
 */
public class AliveCell extends AbstractCell{
    @Override
    public String toCellString(){
        return "A_"+super.toCellString();
    }

    
    
}

No comments:

Post a Comment