All pastes #894911 Raw Edit

Stuff

public text v1 · immutable
·published 2008-02-07 12:18 UTC
rendered paste body
package gameoflife3;

public class BooleanArrayColony implements Colony {

	private int width;		// y
	private int height;		// x
	private boolean[][] grid;
	
	/*
	 * Create a constructor to help create new instances of a Colony.
	 */
	public BooleanArrayColony(int y, int x) {
		this.width = x;
		this.height = y;
		grid = new boolean[width][height];
	}
	
	public void copyInto(Colony copyTo, int y, int x, boolean deadKillLive) {
		int n = 0;
		int m = 0;
		
		for (int i = y; i-y < this.getWidth(); i++) {
			for (int j = x; j-x < this.getHeight(); j++) {
				if (deadKillLive) {
					copyTo.setCell(i, j, this.getCell(n, m));
				} else {
					if (this.getCell(n, m)) {
						copyTo.setCell(i, j, this.getCell(n, m));
					}
				}
			n++;
			} // End for (height)
		m++;
		n = 0;			
		} // End for (width)

	}
	
	/*
	 * toString() reads the grid-array, and returns a String where "*" represents a live cell, 
	 * " " represents a dead cell and "\n" represents a new column. Copied from gameoflife2.Colony.
	 */
	public String toString() {
		
		String drawGrid = ""; // Create an empty string, which we later use to fill up with cell-values.
		
		for (int i = 0; i < getHeight(); i++) {
			for (int j = 0; j < getWidth(); j++) {
				if (getCell(i, j)) {
					drawGrid += "*";	// Found a Live cell, add "*" to the String.
				} else {
					drawGrid += " ";	// Found a Dead cell, add " " to the String.
				}
			}
			drawGrid += "\n";
		}	
		return drawGrid;
	}
	
	/*
	 * Returns the boolean value of the boolean Colony at grid[y][x]
	 */
	public boolean getCell(int y, int x) {
		if(y < getWidth() && y >= 0 && x < getWidth() && x >= 0) {
			return grid[y][x];
		}
		return false;
	}

	/*
 	 * getHeight() returns the height of this.grid[][]
	 */
	public int getHeight() {
		return this.height;
	}

	/*
 	 * getWidth() returns the width of this.grid[][]
	 */
	public int getWidth() {
		return this.width;
	}

	public boolean isEmpty() {
		for (int i = 0; i < this.getWidth(); i++) {
			for (int j = 0; j < this.getHeight(); j++) {
				if (grid[i][j]) return false;
			}
		}
		return true;
	}

	public void setCell(int y, int x, boolean state) {
		if(y < getWidth() && y >= 0 && x < getHeight() && x >= 0) {
			grid[y][x] = state;
		}
	}

}