Public feed
#894151
package gameoflife2;public class Colony { private int width; private int height; private boolean[][] grid; /* * Colony constructor to create new grid-arrays. */ public Colony(int width, int height) { this.width = width; this.height = height; grid = new boolean[height][width]; } /* * getWidth() gets the width of the Colony grid[][] array. */ public int getWidth() { return width; } /* * getHeight() gets the height of the Colony grid[][] array. */ public int getHeight() { return height; } /* * getCell() get the boolean value of the specified cell at grid[x][y]. */ public boolean getCell(int x, int y) { if(x < getHeight() && x >= 0 && y < getWidth() && y >= 0) return grid[x][y]; return false; } /* * setCell() sets a boolean value into grid[x][y]. X = Height, Y = Width. */ public void setCell(int x, int y, boolean isLive) { if(x < getHeight() && x >= 0 && y < getWidth() && y >= 0) { grid[x][y] = isLive; } } /* * countNeighbours counts the number of Live neighbours around grid[x][y]. */ public int countNeighbours(int x, int y) { int liveNeighbours = 0; for(int i = x-1; i <= x+1; i++) { for(int j = y-1; j <= y+1; j++) { if(i >= 0 && i < getWidth() && j>=0 && j < getHeight()) { if(!(i==x && j==y)) { if(grid[i][j] == true) { liveNeighbours += 1; } } } } } return liveNeighbours; } /* * toString() reads the grid-array, and returns a String where "*" represents a live cell, * " " represents a dead cell and "\n" represents a new column. */ 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; } /* * copyCells() */ public void copyCells(int x, int y, Colony tempColony) { int a = 0; int b = 0; for (int i = x; i-x < tempColony.getHeight(); i++) { for (int j = y; j-y < tempColony.getWidth(); j++) { if (tempColony.getCell(a, b)) { setCell(i, j, tempColony.getCell(a, b)); } b++; } a++; b = 0; } } } // END CLASS Colony