import java.awt.Color; import java.util.Enumeration; /* * Created on Aug 25, 2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ /** * @author mike * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class Cell extends SimObject { /*@non_null@*/ private CellState state; public Cell() // EFFECTS: Initializes this to a dead cell. { super(); state = CellState.createDead(); } public /*@non_null@*/ Color getColor () // EFFECTS: Returns green if the cell is alive, otherwise returns white. { if(isAlive()) { return Color.green; } else { return Color.white; } } //@nowarn NonNullResult public boolean setState (/*@non_null@*/ CellState s) // MODIFIES: this // EFFECTS: Sets the cell's current state to s. Returns true iff the new state is different from the current state. { if (state.equals (s)) { return false; } else { state = s; return true; } } public /*@non_null@*/ CellState getState () // EFFECTS: Returns the cell's current state. { return state; } boolean isAlive() // EFFECTS: Returns true if the cell is alive, false otherwise. { return state.isAlive(); } public int countAliveNeighbors() // REQUIRES: All neighbors are of type Cell. // EFFECTS: Returns the number of neighbors. { int count = 0; Enumeration neighbors = getNeighbors(); while(neighbors.hasMoreElements()) { Cell cell = (Cell) neighbors.nextElement(); if(cell.isAlive()) { count++; } } return count; } public /*@non_null@*/ CellState getNextState() // REQUIRES: init has previously been called for this. // EFFECTS: Returns next state value for this. // For the underlying Cell class, the next state is always the // current state. // This method will be overloaded by cell implementations // for various cell automata rules. { return state; } }