package gameoflife3;public class GameOfLife { private int height; private int width; private Colony grid; private GameRule ruleSet; /* * Constructor * * @param height The height of our grid. * @param width The width of our grid. */ public GameOfLife(int height, int width) { this.height = height; this.width = width; this.grid = new BooleanArrayColony(height, width); } public int getWidth() { return width; } public int getHeight() { return height; } /* * Used to set the dimensions (height, width) of our Colony. * * @param height The height of our colony. * @param width The width of our colony. */ public void setDimensions(int height, int width){ this.width = width; this.height = height; this.grid = new BooleanArrayColony(height, width); } /* * Used for changing the rules of the game to the provided GameRule instance. * * @param ruleSet A set of rules defined as a GameRule instance. */ public void setGameRule(GameRule ruleSet) { this.ruleSet = ruleSet; } /* * @returns the state of a cell at the given position. * * @param y The width of the cell. * @param x The height of the cell. * @return The state of the given cell. */ public boolean getCell(int y, int x) { return this.grid.getCell(y, x); } /* * Copies cells from this Colony into another Colony at a certain position. * * @param copyTo * @param y y position * @param x */ public void copyTo(Colony copyTo, int y, int x) { int n = 0; int m = 0; for (int i = y; i-y < this.grid.getWidth(); i++) { for (int j = x; j-x < this.grid.getHeight(); j++) { copyTo.setCell(i, j, getCell(n, m)); m++; } n++; m = 0; } // End for } /* * Copies the state of cells from the given Colony, to the current GameOfLife simulation. * * @param copyFrom The Colony-instance which we will be copying cells from. * @param y The width position of the current GameOfLife simulation to start copying from. * @param x The height position of the current GameOfLife simulation to start copying from. */ public void copyFrom(Colony copyFrom, int y, int x) { int n = 0; int m = 0; for (int i = y; i-y < copyFrom.getWidth(); i++) { for (int j = x; j-x < copyFrom.getHeight(); j++) { this.grid.setCell(i, j, copyFrom.getCell(n, m)); m++; } n++; m = 0; } // End for } public int stepColony() { int changedCells = 0; BooleanArrayColony nextState = new BooleanArrayColony(this.height, this.width); for (int i = 0; i < this.getHeight(); i++) { for (int j = 0; j < this.getWidth(); j++) { nextState.setCell(j, i, ruleSet.nextState(this.grid, j, i)); if (nextState.getCell(j, i) != this.grid.getCell(j, i)) { changedCells++; } } } this.grid = nextState; return changedCells; }}