All pastes #903641 Raw Edit

Anonymous

public java v1 · immutable
·published 2008-02-14 10:37 UTC
rendered paste body
package gameoflife3;public class GenericRule implements GameRule {	private boolean[][] grid;	private boolean[] birthLogic = new boolean[] {false, false, false, true, false, false, false, false, false};	private boolean[] surviveLogic = new boolean[] {false, false, true, true, false, false ,false ,false, false};	private int liveNeighboursNeededToSurvive; 	private int liveNeighboursNeededToBeBorn;	private boolean survived;	private boolean isBorn;		/*	 * Constructor method that create a new instance of the object.	 */	public GenericRule() {		grid = new boolean[][] {				{true, true, true},				{true, false, true},				{true, true, true}		};	}		/*	 * This method is used for deciding if a potential neighbors is counted such.	 * Meaning: Can the neighbor given be a neighbor according to our rules?	 * @param verticalAlign		Vertical alignment of the neighboring cell. 	 * @param horizontalAlign	Horizontal alignment of the neighboring cell.	 * @param isNeighbour	 */	public void setNeighbour(int verticalAlign, int horizontalAlign, boolean isNeighbour) {		/*		 * verticalAlign can be either -1, 0 or 1, meaning above, same row or below.		 * horizontalAlign can be either -1, 0 or 1, meaning left, same column or right.		 */		if (verticalAlign != 0 && horizontalAlign != 0) {						grid[1+horizontalAlign][1+verticalAlign] = isNeighbour;		}	}		/*	 * @param y		The column.	 * @param x		The row.	 * @return		If a cell can be a potential neighbor or not.	 */	public boolean isNeighbour(int y, int x) {		if (y+1 < grid.length && y+1 >= 0 && x+1 < grid[0].length && x+1 >= 0) {			return grid[1+y][1+x];		}		return false;	}		public void setSurvive(int neigbourCount, boolean nextState) {		liveNeighboursNeededToSurvive = neigbourCount;		survived = nextState;	}		public void setBirth(int neighbourCount, boolean nextState) {		liveNeighboursNeededToBeBorn = neighbourCount;		isBorn = nextState;	}		public int countNeighbours(Colony temp, int y, int x) {		int neighbours = 0;				for (int i = -1; i <= 1; i++) {			for (int j = -1; j <= 1; j++) {				if (isNeighbour(i, j) && temp.getCell(i+y, j+x)) {					neighbours++;				} // End if			} // End for		} // End for		return neighbours;	}		/*	 * This method returns the next state of the given cell at position y, x	 * where y is the width and x is the height.	 */	public boolean nextState(Colony currentState, int y, int x) {		int n = this.countNeighbours(currentState, y, x);		boolean lives = currentState.getCell(y, x);				if(lives) return surviveLogic[n];		if(!lives) return birthLogic[n];				return false;	}}